Created
January 21, 2014 15:51
-
-
Save davidfauth/8542633 to your computer and use it in GitHub Desktop.
Python utility to Tokenize data and write out the top-5 bigrams
This file contains 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
@outputSchema("top_five:bag{t:(bigram:chararray)}") | |
def top5_bigrams(textDescription): | |
sentences = nltk.tokenize.sent_tokenize(textDescription) | |
tokens = [nltk.tokenize.word_tokenize(s) for s in sentences] | |
bgm = nltk.collocations.BigramAssocMeasures() | |
finder = nltk.collocations.BigramCollocationFinder.from_documents(tokens) | |
top_5 = finder.nbest(bgm.likelihood_ratio, 5) | |
return [ ("%s %s" % (s[0], s[1]),) for s in top_5 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment