Created
August 11, 2015 21:23
-
-
Save napsternxg/49e186a4cec4fd1682fe to your computer and use it in GitHub Desktop.
Extract Phrases Counts from Corpus taken from http://www.markhneedham.com/blog/2015/01/19/pythonnltk-finding-the-most-common-phrases-in-how-i-met-your-mother/
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
import nltk | |
import string | |
from collections import Counter | |
def untokenize(ngram): | |
tokens = list(ngram) | |
return "".join([" "+i if not i.startswith("'") and \ | |
i not in string.punctuation and \ | |
i != "n't" | |
else i for i in tokens]).strip() | |
def extract_phrases(text, phrase_counter, length): | |
for sent in nltk.sent_tokenize(text): | |
words = nltk.word_tokenize(sent) | |
for phrase in nltk.util.ngrams(words, length): | |
if all(word not in string.punctuation for word in phrase): | |
phrase_counter[untokenize(phrase)] += 1 | |
if __name__ == "__main__": | |
phrase_counter = Counter() | |
sent = ["This is good", "This is awesome", "Weather is good"] | |
for s in sent: | |
extract_phrases(s,phrase_counter, 2) | |
print phrase_counter.most_common(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment