具有频率的 Ngram 的 Python 列表

标签 python nltk scikit-learn

我需要从文本中获取最流行的 ngram。 Ngram 的长度必须在 1 到 5 个单词之间。

我知道如何得到二元组和三元组。例如:

bigram_measures = nltk.collocations.BigramAssocMeasures()
finder = nltk.collocations.BigramCollocationFinder.from_words(words)
finder.apply_freq_filter(3)
finder.apply_word_filter(filter_stops)
matches1 = finder.nbest(bigram_measures.pmi, 20)

但是,我发现 scikit-learn 可以获取不同长度的 ngram。例如,我可以获得长度为 1 到 5 的 ngram。

v = CountVectorizer(analyzer=WordNGramAnalyzer(min_n=1, max_n=5))

但 WordNGramAnalyzer 现在已弃用。我的问题是:如何从我的文本中获得 N 个最佳单词搭配,搭配长度从 1 到 5。我还需要获取此搭配/ngram 的 FreqList。

我可以用 nltk/scikit 做到这一点吗?我需要从一个文本中获取具有不同长度的 ngram 组合?

例如使用 NLTK 双字母组和三字母组,在许多情况下我的三字母组包含我的位字母组,或者我的三字母组是更大的 4-grams 的一部分。例如:

位图:你好我的 三卦:你好我的名字

我知道如何从三元组中排除二元组,但我需要更好的解决方案。

最佳答案

更新

自 scikit-learn 0.14 以来,格式已更改为:

n_grams = CountVectorizer(ngram_range=(1, 5))

完整示例:

test_str1 = "I need to get most popular ngrams from text. Ngrams length must be from 1 to 5 words."
test_str2 = "I know how to exclude bigrams from trigrams, but i need better solutions."

from sklearn.feature_extraction.text import CountVectorizer

c_vec = CountVectorizer(ngram_range=(1, 5))

# input to fit_transform() should be an iterable with strings
ngrams = c_vec.fit_transform([test_str1, test_str2])

# needs to happen after fit_transform()
vocab = c_vec.vocabulary_

count_values = ngrams.toarray().sum(axis=0)

# output n-grams
for ng_count, ng_text in sorted([(count_values[i],k) for k,i in vocab.items()], reverse=True):
    print(ng_count, ng_text)

输出以下内容(请注意,单词 I 被删除不是因为它是停用词(不是),而是因为它的长度:https://stackoverflow.com/a/20743758/):

> (3, u'to')
> (3, u'from')
> (2, u'ngrams')
> (2, u'need')
> (1, u'words')
> (1, u'trigrams but need better solutions')
> (1, u'trigrams but need better')
...

现在这应该/可能会简单得多,imo。您可以尝试类似 textacy 的操作,但这有时会带来其自身的复杂性,例如初始化 Doc,目前不适用于 v.0.6.2 as shown on their docs . If doc initialization worked as promised , 理论上以下将起作用(但它不起作用):

test_str1 = "I need to get most popular ngrams from text. Ngrams length must be from 1 to 5 words."
test_str2 = "I know how to exclude bigrams from trigrams, but i need better solutions."

import textacy

# some version of the following line
doc = textacy.Doc([test_str1, test_str2])

ngrams = doc.to_bag_of_terms(ngrams={1, 5}, as_strings=True)
print(ngrams)

旧答案

WordNGramAnalyzer 自 scikit-learn 0.11 以来确实已弃用。创建 n-gram 和获取词频现在合并在 sklearn.feature_extraction.text.CountVectorizer 中.您可以创建从 1 到 5 的所有 n-gram,如下所示:

n_grams = CountVectorizer(min_n=1, max_n=5)

更多示例和信息可以在 scikit-learn 关于 text feature extraction 的文档中找到.

关于具有频率的 Ngram 的 Python 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11763613/

相关文章:

scikit-learn - 如何在 RandomForest 实现中对类进行加权?

python - 如何管理登录 curses

python - 在 Folium map 的循环中设置 map 边界并停止无限平移

python - 找不到 Django 模块库

Python:如何将字数列表转换为适合 CountVectorizer 的格式

python - 在 pandas 中执行 nltk.stem.SnowballStemmer

python - 有没有办法使用 scikit 或任何其他 python 包仅获取单词的 IDF 值?

python - python中的线性回归严重错误,回归线完全错误

python - 启动 PySpark 以在 Eclipse 中使用 Python 和 Spark

python - 有没有更快的方法可以通过 python 的 nltk 从单词列表中进行检查?