【问题标题】:tf-idf sickitlearn separate "word" from wordtf-idf sickitlearn 将“单词”与单词分开
【发布时间】:2019-03-09 15:22:07
【问题描述】:

我正在处理文本分类中的一个问题,如果以这种格式 "word" 找到一个单词,它的重要性将不同于以这种格式 word 找到的单词> 所以我尝试了这段代码

    import re
    from sklearn.feature_extraction.text import CountVectorizer
    sent1 = "The cat sat on my \"face\" face"
    sent2 = "The dog sat on my bed"
    content = [sent1,sent2]
    vectorizer = CountVectorizer(token_pattern=r"(?u)\b\w\w+\b|!|\?|\"|\'")
    vectorizer.fit(content)
    print (vectorizer.get_feature_names()) 

结果是

    ['"', 'bed', 'cat', 'dog', 'face', 'my', 'on', 'sat', 'the']

我希望它在哪里

    ['bed', 'cat', 'dog', 'face','"face"' 'my', 'on', 'sat', 'the']

【问题讨论】:

  • 这是一个标记化问题。修复token_pattern 以捕获双引号的情况,或提供tokeniser 可调用CountVectorizer

标签: python text-classification tf-idf


【解决方案1】:

你的令牌模式是

token_pattern=r"(?u)\b\w\w+\b|!|\?|\"|\'"

正在查找单词 (\b\w\w+\b) 或感叹号、问号或引号。尝试类似

token_pattern=r"(?u)\b\w\w+\b|\"\b\w\w+\b\"|!|\?|\'"

注意部分

\"\b\w\w+\b\"

查找被引号包围的单词。

【讨论】:

    【解决方案2】:

    您需要根据需要调整token_pattern 参数。以下应该适用于提供的示例:

    pattern = r"\S+[^!?.\s]"
    vectorizer = CountVectorizer(token_pattern=pattern)
    

    但是,您可能需要进一步完善该模式。 https://regex101.com 可能有助于使您的正则表达式恰到好处。

    【讨论】:

      猜你喜欢
      • 2019-04-17
      • 2016-08-07
      • 2018-01-30
      • 2021-08-19
      • 2019-11-16
      • 2019-06-12
      • 1970-01-01
      • 2021-02-24
      • 2017-07-05
      相关资源
      最近更新 更多