Skip to content

Instantly share code, notes, and snippets.

@andriisoldatenko
Created September 3, 2016 07:38
Show Gist options
  • Save andriisoldatenko/64900150d3df6299871349025bc08ccb to your computer and use it in GitHub Desktop.
Save andriisoldatenko/64900150d3df6299871349025bc08ccb to your computer and use it in GitHub Desktop.
Collocations example
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