Created
November 2, 2015 23:56
-
-
Save michael-erasmus/9b221fcedbe8a4a7cce6 to your computer and use it in GitHub Desktop.
TF-IDF keywords using Graphlab
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 re | |
import graphlab | |
#remove html tags | |
docs['words'] = docs['body'].apply(lambda doc: re.sub("<[^>]*>", "", doc)) | |
#remove punctuation, whitespace and lowercase it all | |
docs['words'] = docs['words'].apply(lambda doc: re.sub("[\W\d]", " ", doc.lower().strip())) | |
docs = graphlab.SFrame(docs) | |
docs['word_counts'] = graphlab.text_analytics.count_words(docs['words']) | |
docs_tfidf = graphlab.text_analytics.tf_idf(docs['words']) | |
docs['top10'] = docs_tfidf['docs'].apply(lambda t: " ".join(sorted(t, key=t.get, reverse=True)[1:10])) | |
docs.save("billing_key_words.csv", format='csv') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment