Created
September 3, 2016 07:38
-
-
Save andriisoldatenko/64900150d3df6299871349025bc08ccb to your computer and use it in GitHub Desktop.
Collocations example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pprint | |
from nltk.corpus import stopwords | |
from nltk.corpus import webtext | |
from nltk.collocations import BigramCollocationFinder | |
from nltk.metrics import BigramAssocMeasures | |
stopset = set(stopwords.words('english')) | |
filter_stops = lambda w: len(w) < 3 or w in stopset | |
words = [w.lower() for w in webtext.words('grail.txt')] | |
bcf = BigramCollocationFinder.from_words(words) | |
bcf.apply_word_filter(filter_stops) | |
results = bcf.nbest(BigramAssocMeasures.likelihood_ratio, 10) | |
pprint.pprint([' '.join(r) for r in results]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment