Quantcast
Channel: n-grams in python, four, five, six grams? - Stack Overflow
Viewing all articles
Browse latest Browse all 18

Answer by Nik for n-grams in python, four, five, six grams?

$
0
0

I have never dealt with nltk but did N-grams as part of some small class project. If you want to find the frequency of all N-grams occurring in the string, here is a way to do that. D would give you the histogram of your N-words.

D = dict()string = 'whatever string...'strparts = string.split()for i in range(len(strparts)-N): # N-grams    try:        D[tuple(strparts[i:i+N])] += 1    except:        D[tuple(strparts[i:i+N])] = 1

Viewing all articles
Browse latest Browse all 18

Trending Articles