[英]How do I use CountVectorizer to get the count of a phrase without counting words in the phrase?
我正在研究NLP項目,我希望對句子進行標記並獲得不同標記的計數。 有時候我希望用幾句話作為一個短語而不要計算短語中的單詞。
我發現Scikit-Learn中的CountVectorizer對計算短語有用,但我無法弄清楚如何刪除短語中的單詞。
例如:
words = ['cat', 'dog', 'walking', 'my dog']
example = ['I was walking my dog and cat in the park']
vect = CountVectorizer(vocabulary=words, ngram_range=(1,2))
dtm = vect.fit_transform(example)
print(dtm)
我有:
>>> vect.get_feature_names()
['cat', 'dog', 'walking', 'my dog']
>>> print(dtm)
(0, 0) 1
(0, 1) 1
(0, 2) 1
(0, 3) 1
我想要的是:
>>> print(dtm)
(0, 0) 1
(0, 2) 1
(0, 3) 1
但我想在字典中保留'dog'
,因為它可能在其他文本中單獨出現。
CountVectorizer
沒有任何特定的配置CountVectorizer
應用較長的字符串並將其從字符串中刪除以防止計算較短的子字符串。
因此,一個解決方案可以使用CountVectorzier
作為您所做的。 Aftwerwards,迭代單詞以查找較長短語中包含的單詞,然后在CountVectorizer
的第一個結果中CountVectorizer
包含的較短短語中較長短語的數量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.