【问题标题】:Sk Learn CountVectorizer: keeping emojis as wordsSklearn CountVectorizer:将表情符号保留为单词
【发布时间】:2019-12-11 11:27:21
【问题描述】:

我在字符串上使用 Sk Learn CountVectorizer,但 CountVectorizer 丢弃了文本中的所有表情符号。

例如,???? Welcome 应该给我们:["\xf0\x9f\x91\x8b", "welcome"]

但是,运行时:

vect = CountVectorizer()
test.fit_transform(['???? Welcome'])

我只得到:["welcome"]

这与token_pattern 不把编码的表情符号算作一个单词有关,但是有没有自定义的token_pattern 来处理表情符号?

【问题讨论】:

    标签: python scikit-learn nlp countvectorizer


    【解决方案1】:

    尝试使用参数CountVectorizer(analyzer = 'char', binary = True)

    文档说:“token_pattern:表示什么构成“令牌”的正则表达式,仅在分析器 == 'word' 时使用”参见https://scikit-learn.org/dev/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html

    另请参阅此笔记本:https://www.kaggle.com/kmader/toxic-emojis

    【讨论】:

      【解决方案2】:

      还有一个couplepackages 可以直接将表情符号/表情符号转换成文字,例如

      import emot
      >>> text = "I love python ? :-)"
      >>> emot.emoji(text)
      [{'value': '?', 'mean': ':man:', 'location': [14, 14], 'flag': True}]
      
      >> import emoji
      >> print(emoji.demojize('Python is ?'))
      Python is :thumbs_up:
      

      【讨论】:

        【解决方案3】:

        是的,你是对的! token_pattern 必须更改。我们可以将其设为除空格以外的任何字符,而不仅仅是字母数字字符。

        试试这个!

        from sklearn.feature_extraction.text import TfidfVectorizer
        s= ['? Welcome', '? Welcome']
        
        v = TfidfVectorizer(token_pattern=r'[^\s]+')
        v.fit(s)
        v.get_feature_names()
        
        # ['welcome', '?']
        
        
        

        【讨论】:

          猜你喜欢
          • 2021-09-26
          • 2017-09-14
          • 2019-12-18
          • 2019-08-29
          • 2014-06-20
          • 1970-01-01
          • 2018-03-20
          • 2021-09-02
          • 2018-08-29
          相关资源
          最近更新 更多