Skip to content

Instantly share code, notes, and snippets.

@N0taN3rd
Created June 24, 2017 22:45
Show Gist options
  • Select an option

  • Save N0taN3rd/8b4119ec6bac523fdbd1bd55afc05cf5 to your computer and use it in GitHub Desktop.

Select an option

Save N0taN3rd/8b4119ec6bac523fdbd1bd55afc05cf5 to your computer and use it in GitHub Desktop.
summarize text
from bs4 import BeautifulSoup
# https://pypi.python.org/pypi/sumy
from sumy.parsers.html import HtmlParser
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer as Summarizer
from sumy.nlp.stemmers import Stemmer
from sumy.utils import get_stop_words
# USE PYTHON3
# what language
LANGUAGE = 'english'
# how many sentences do you want
SENTENCES_COUNT = 2
if __name__ == '__main__':
summarizer = Summarizer(Stemmer(LANGUAGE))
summarizer.stop_words = get_stop_words(LANGUAGE)
tok = Tokenizer(LANGUAGE)
# pretty much you gotta extract the text you want to be summarized
# or use beautiful soup to iterate over the html and when you find the
# tag gets its text or a combination of that
"""
parser = PlaintextParser.from_string('', tok)
for sentence in map(lambda x: str(x), summarizer(parser.document, SENTENCES_COUNT)):
print(sentence)
"""
"""
with open('htmlFile.html' 'r') as iin:
info = BeautifulSoup(iin, 'lxml').find(class_='whatever')
for ul in info.find_all('ul'):
li = ul.find('li')
em = li.find('em')
strong = li.find('strong').text
emt = em.text
parser = HtmlParser.from_string(str(li), None, tok)
for sentence in map(lambda x: str(x), summarizer(parser.document, SENTENCES_COUNT)):
print(sentence)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment