获取 CountVectorizer 以包含“1:1”
Get CountVectorizer to include "1:1"
如果我有一些包含短语“1:1”的文本。如何让 CountVectorizer
将其识别为令牌?
text = ["first ques # 1:1 on Whosebug", "please help"]
vec = CountVectorizer()
vec.fit_transform(text)
vec.get_feature_names()
您可以使用自定义分词器。对于简单的情况替换
vec = CountVectorizer()
来自
vec = CountVectorizer(tokenizer=lambda s: s.split())
可以。通过此修改,您的代码 returns:
[u'#', u'1:1', u'first', u'help', u'on', u'please', u'ques', u'Whosebug']
希望这个建议能让您走上正轨,但请注意,这种解决方法在更复杂的情况下无法正常工作(例如,如果您的文本有标点符号)。
要处理标点符号,您可以传递 CountVectorizer
像这样的标记模式:
text = [u"first ques... # 1:1, on Whosebug", u"please, help!"]
vec = CountVectorizer(token_pattern=u'\w:?\w+')
输出:
[u'1:1', u'first', u'help', u'on', u'please', u'ques', u'Whosebug']
如果我有一些包含短语“1:1”的文本。如何让 CountVectorizer
将其识别为令牌?
text = ["first ques # 1:1 on Whosebug", "please help"]
vec = CountVectorizer()
vec.fit_transform(text)
vec.get_feature_names()
您可以使用自定义分词器。对于简单的情况替换
vec = CountVectorizer()
来自
vec = CountVectorizer(tokenizer=lambda s: s.split())
可以。通过此修改,您的代码 returns:
[u'#', u'1:1', u'first', u'help', u'on', u'please', u'ques', u'Whosebug']
希望这个建议能让您走上正轨,但请注意,这种解决方法在更复杂的情况下无法正常工作(例如,如果您的文本有标点符号)。
要处理标点符号,您可以传递 CountVectorizer
像这样的标记模式:
text = [u"first ques... # 1:1, on Whosebug", u"please, help!"]
vec = CountVectorizer(token_pattern=u'\w:?\w+')
输出:
[u'1:1', u'first', u'help', u'on', u'please', u'ques', u'Whosebug']