Skip to content
Snippets Groups Projects
Commit ae852bc7 authored by Tarek Shah's avatar Tarek Shah
Browse files

added nltk and T5 summarizers

parent 82f5fcf1
No related branches found
No related tags found
No related merge requests found
import torch
from transformers import AutoTokenizer, AutoModelWithLMHead
tokenizer = AutoTokenizer.from_pretrained('t5-base')
model = AutoModelWithLMHead.from_pretrained('t5-base', return_dict=True)
inputs = tokenizer.encode("summarize: " + """The Saturday night shooting came as the city's majority Asian community was marking the Lunar New Year, transforming one of the most auspicious days of the calendar into a tragedy.
“There is a lot of fear and anxiety out there. People are fearful of this kind of situation where our joyous Lunar New Year celebration was turned utterly upside down into tragedy and fear,” Rep. Judy Chu, who represents Monterey Park, said Monday at a candlelight vigil for the victims.
California Gov. Gavin Newsom was meeting with victims of the shooting Monday when he was pulled away to be briefed on another fatal mass shooting in Half Moon Bay - the state's second mass shooting in three days. “Tragedy upon tragedy,” Newsom said in a tweet.
Authorities revealed new details Monday from their search of the home of the Monterey Park shooting suspect, 72-year-old Huu Can Tran, in Hemet, about 80 miles east of Monterey Park.
Detectives executed a search warrant and found “hundreds of rounds” of ammunition, a .308-caliber rifle, various electronic devices and evidence leading officials to believe he was “manufacturing homemade firearm suppressors,” Los Angeles County Sheriff Robert Luna said.""",
return_tensors='pt',
max_length=512,
truncation=True)
summary_ids = model.generate(inputs, max_length=150, min_length=80, length_penalty=5., num_beams=2)
summary = tokenizer.decode(summary_ids[0])
print(summary)
\ No newline at end of file
import nltk
from nltk.corpus import stopwords
nltk.download('stopwords')
nltk.download('punkt')
from nltk.tokenize import word_tokenize, sent_tokenize
text = """The Saturday night shooting came as the city's majority Asian community was marking the Lunar New Year, transforming one of the most auspicious days of the calendar into a tragedy.
“There is a lot of fear and anxiety out there. People are fearful of this kind of situation where our joyous Lunar New Year celebration was turned utterly upside down into tragedy and fear,” Rep. Judy Chu, who represents Monterey Park, said Monday at a candlelight vigil for the victims.
California Gov. Gavin Newsom was meeting with victims of the shooting Monday when he was pulled away to be briefed on another fatal mass shooting in Half Moon Bay - the state's second mass shooting in three days. “Tragedy upon tragedy,” Newsom said in a tweet.
Authorities revealed new details Monday from their search of the home of the Monterey Park shooting suspect, 72-year-old Huu Can Tran, in Hemet, about 80 miles east of Monterey Park.
Detectives executed a search warrant and found “hundreds of rounds” of ammunition, a .308-caliber rifle, various electronic devices and evidence leading officials to believe he was “manufacturing homemade firearm suppressors,” Los Angeles County Sheriff Robert Luna said."""
stopWords = set(stopwords.words("english"))
words = word_tokenize(text)
freqTable = dict()
for word in words:
word = word.lower()
if word in stopWords:
continue
if word in freqTable:
freqTable[word] += 1
else:
freqTable[word] = 1
sentences = sent_tokenize(text)
sentenceValue = dict()
for sentence in sentences:
for word, freq in freqTable.items():
if word in sentence.lower():
if word in sentence.lower():
if sentence in sentenceValue:
sentenceValue[sentence] += freq
else:
sentenceValue[sentence] = freq
sumValues = 0
for sentence in sentenceValue:
sumValues += sentenceValue[sentence]
average = int(sumValues / len(sentenceValue))
summary = ''
for sentence in sentences:
if (sentence in sentenceValue) and (sentenceValue[sentence] > (1.2 * average)):
summary += " " + sentence
print(summary)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment