【发布时间】:2017-04-27 18:57:41
【问题描述】:
我有一个字符串列表(10,000 个)。一些字符串构成多个单词。我有另一个列表,其中包含一些句子。我正在尝试计算列表中每个字符串在每个句子中出现的次数。
目前我正在使用 sklearn 的特征提取工具,因为当我们要查找 10,000 条字符串和 10,000 条句子时,它的工作速度非常快。
以下是我的代码的简化版本。
import numpy as np
from sklearn import feature_extraction
sentences = ["hi brown cow", "red ants", "fierce fish"]
listOfStrings = ["brown cow", "ants", "fish"]
cv = feature_extraction.text.CountVectorizer(vocabulary=listOfStrings)
taggedSentences = cv.fit_transform(sentences).toarray()
taggedSentencesCutDown = taggedSentences > 0
# Here we get an array of tuples <sentenceIndex, stringIndexfromStringList>
taggedSentencesCutDown = np.column_stack(np.where(taggedSentencesCutDown))
目前,如果你运行它,输出如下:
In [2]: taggedSentencesCutDown
Out[2]: array([[1, 1], [2, 2]])
我想要的是:
In [2]: taggedSentencesCutDown
Out[2]: array([[0,0], [1, 1], [2, 2]])
我当前对 CountVectorizer 的使用表明它不是在寻找多个单词字符串。有没有其他方法可以做到这一点而无需进入长 for 循环。效率和时间对我的应用非常重要,因为我的列表有 10,000 多个。
谢谢
【问题讨论】:
-
看一下参数
analyzer。我认为用 sklearn 的CountVectorizer做你想做的事情是不可能的,因为它只支持单词或 n-gram 字符,而不支持多个单词。你可以通过传递你自己的callable函数来覆盖它,但是对于每个句子,你不仅要返回单词,还要返回它们之间的所有组合。除非您对listOfStrings中的字数或字数有更具体的限制,否则问题不会很快解决。
标签: python numpy scikit-learn nltk