Skip to content
Snippets Groups Projects
nltk_summarizer.py 2.68 KiB
Newer Older
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)