Skip to content

Instantly share code, notes, and snippets.

@empireshades
Created November 13, 2018 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save empireshades/bf9c10684559e8bb258c90c211b17a61 to your computer and use it in GitHub Desktop.
Save empireshades/bf9c10684559e8bb258c90c211b17a61 to your computer and use it in GitHub Desktop.
from textblob import TextBlob, Word
import sys
import random
from newspaper import Article
from fuzzywuzzy import fuzz
from nltk.stem import WordNetLemmatizer
def nphra(text):
# Extract Noun Phrases from text input
blob = TextBlob(text)
for np in blob.noun_phrases:
print(np)
def summary(text):
# Extract lemmatized (root) of every noun in text input
blob = TextBlob(text)
nouns = list()
for word, tag in blob.tags:
if tag == 'NN':
nouns.append(word.lemmatize())
print(nouns)
def cmp_news(url1,url2):
'''Download, parse, compare two articles (urls).
Lemmatize parsed keywords,
and compare using fuzzy logic to return a numerical score'''
article1 = Article(url1)
article2 = Article(url2)
for i in article1, article2:
i.download()
i.parse()
i.nlp()
wordnet_lemmatizer = WordNetLemmatizer()
key1 = ' '.join([ wordnet_lemmatizer.lemmatize(i) for i in article1.keywords ])
key2 = ' '.join([ wordnet_lemmatizer.lemmatize(i) for i in article2.keywords ])
print(key1,'\n',key2)
print('fuzz.token_set: {}'.format(fuzz.token_set_ratio(key1, key2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment