2016-01-08 32 views

回答

0

這是因爲token_pattern參數默認爲'(?u)\\b\\w\\w+\\b',其過濾的所有單詞(提供的參數analyzer設置爲'word',這是默認值),只有由單個字符(例如「一」或「I」) 。如果您將token_pattern設置爲不同的正則表達式,例如'(?u)\\b\\w+\\b'應保留單個字符的單詞。

實施例:

In [71]: from sklearn.feature_extraction.text import CountVectorizer 
In [72]: corpus = ['I like my coffee with a shot of rum.'] 

In [73]: vec = CountVectorizer() 
In [74]: vec.fit(corpus) 
In [75]: vec.vocabulary_ 

Out[75]: {'coffee': 0, 'like': 1, 'my': 2, 'of': 3, 'rum': 4, 'shot': 5, 'with': 6} 

In [76]: vec = CountVectorizer(token_pattern='(?u)\\b\\w+\\b') 
In [77]: vec.fit(corpus) 
In [78]: vec.vocabulary_ 
Out[78]: {'a': 0, 'coffee': 1, 'i': 2, 'like': 3, 'my': 4, 'of': 5, 'rum': 6, 'shot': 7, 'with': 8}