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