我正在寻找一种将文本拆分成n-gram的方法。通常我会这样做:
import nltk
from nltk import bigrams
string = "I really like python, it's pretty awesome."
string_bigrams = bigrams(string)
print string_bigrams
我知道nltk只提供二元和三元,但是有没有办法把我的文本分成四个、五个甚至一百个呢?
谢谢!
发布于 2013-07-09 20:10:39
其他用户给出的基于原生python的很好的答案。但这里是nltk
方法(以防万一,OP会因为重新发明nltk
库中已有的东西而受到惩罚)。
在nltk
中有一个人们很少使用的ngram module。这并不是因为ngram很难阅读,而是基于ngram训练模型,其中n>3将导致大量数据稀疏。
from nltk import ngrams
sentence = 'this is a foo bar sentences and i want to ngramize it'
n = 6
sixgrams = ngrams(sentence.split(), n)
for grams in sixgrams:
print grams
发布于 2013-07-09 00:54:06
我很惊讶这一点还没有出现:
In [34]: sentence = "I really like python, it's pretty awesome.".split()
In [35]: N = 4
In [36]: grams = [sentence[i:i+N] for i in xrange(len(sentence)-N+1)]
In [37]: for gram in grams: print gram
['I', 'really', 'like', 'python,']
['really', 'like', 'python,', "it's"]
['like', 'python,', "it's", 'pretty']
['python,', "it's", 'pretty', 'awesome.']
发布于 2015-08-31 17:28:46
仅使用nltk工具
from nltk.tokenize import word_tokenize
from nltk.util import ngrams
def get_ngrams(text, n ):
n_grams = ngrams(word_tokenize(text), n)
return [ ' '.join(grams) for grams in n_grams]
输出示例
get_ngrams('This is the simplest text i could think of', 3 )
['This is the', 'is the simplest', 'the simplest text', 'simplest text i', 'text i could', 'i could think', 'could think of']
为了使ngram保持数组格式,只需删除' '.join
https://stackoverflow.com/questions/17531684
复制相似问题