问题:使用 scikit-learn 查找特定词汇的可变 n-gram 的命中数。
解释。我从这里得到了例子。
想象一下,我有一个语料库,我想找出有多少命中(计数)具有如下词汇:
myvocabulary = [(window=4, words=['tin', 'tan']),
(window=3, words=['electrical', 'car'])
(window=3, words=['elephant','banana'])
我在这里所说的窗口是单词可以出现的单词跨度的长度。如下:
'tin tan' 被击中(4 个字以内)
'tin dog tan' 被击中(4 个字以内)
'tin dog cat tan被击中(4个字以内)
'tin car sun eclipse tan' 没有被击中。tin 和 tan 相距超过 4 个单词。
我只想计算 (window=4, words=['tin', 'tan']) 出现在文本中的次数,所有其他的都相同,然后将结果添加到 pandas 以计算tf-idf 算法。我只能找到这样的东西:
from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(vocabulary = myvocabulary, stop_words = 'english')
tfs = tfidf.fit_transform(corpus.values())
其中词汇表是一个简单的字符串列表,可以是单个单词或多个单词。
除了来自 scikitlearn:
class sklearn.feature_extraction.text.CountVectorizer
ngram_range : tuple (min_n, max_n)
要提取的不同 n-gram 的 n 值范围的下边界和上边界。将使用所有满足 min_n <= n <= max_n 的 n 值。
也无济于事。
有任何想法吗?谢谢。
呼如林
一只斗牛犬
随时随地看视频慕课网APP
相关分类