From cf841e74d2c83bf5598c833a6260fd6f1bcc22d2 Mon Sep 17 00:00:00 2001 From: riseandshine0 Date: Tue, 3 Sep 2019 09:58:36 +0200 Subject: [PATCH] fixed authors error beautified code (PEP8) --- modules/headline.py | 55 +++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/modules/headline.py b/modules/headline.py index df05332..586996d 100644 --- a/modules/headline.py +++ b/modules/headline.py @@ -3,56 +3,53 @@ from nltk.classify import NaiveBayesClassifier -class title: - #Initialisations - def __init__(self): - self.news_url=input("\nEnter The URL : ") - self.pos=[] #Variable to store all positive tokens from positive_headlines.csv file - self.neg=[] #Variable to store all negative tokens from negative_headlines.csv file - - +class Title: + # Initialisations + def __init__(self): + self.news_url = input("\nEnter The URL : ") + self.pos = [] # Variable to store all positive tokens from positive_headlines.csv file + self.neg = [] # Variable to store all negative tokens from negative_headlines.csv file + # self.article = newspaper.Article(news_url) + # extract headline def extract_headline(self): try: article = newspaper.Article(self.news_url) article.download() article.parse() - return article.title.strip() - except newspaper.article.ArticleException: #List possible errors in case of any exception + return article + except newspaper.article.ArticleException: # List possible errors in case of any exception print("\nCONNECTION/URL ERROR: Article could not be retrieved.") - - #Adding Training/Testing Data - def train(self,headline): + # Adding Training/Testing Data + def train(self, headline): with open("positive_headlines.csv") as file: for sentence in file: - self.pos.append([{word: True for word in nltk.word_tokenize(sentence)},'Positive']) + self.pos.append([{word: True for word in nltk.word_tokenize(sentence)}, 'Positive']) with open("negative_headlines.csv") as file: for sentence in file: - self.neg.append([{word: True for word in nltk.word_tokenize(sentence)},'Negative']) + self.neg.append([{word: True for word in nltk.word_tokenize(sentence)}, 'Negative']) - training=self.pos[:int(len(self.pos))] + self.neg[:int(len(self.neg))] + training = self.pos[:int(len(self.pos))] + self.neg[:int(len(self.neg))] - classifier = NaiveBayesClassifier.train(training) #Training - sentiment=classifier.classify({word: True for word in nltk.word_tokenize(headline)}) + classifier = NaiveBayesClassifier.train(training) # Training + sentiment = classifier.classify({word: True for word in nltk.word_tokenize(headline)}) return sentiment # categorize headline - def headline_category(self,headline,sentiment): - print("\nHEADLINE :",headline.upper()) - print("SENTIMENT :",sentiment) - print("AUTHOR(S) :",*self.article.authors,'\n') + def headline_category(self, headline, sentiment): + print("\nHEADLINE :", headline.upper()) + print("SENTIMENT :", sentiment) + print("AUTHOR(S) :", self.extract_headline().authors, '\n') - # main of class def main(self): - hdln=self.extract_headline() - sntmnt=self.train(hdln) + hdln = self.extract_headline().title.strip() + sntmnt = self.train(hdln) self.train(hdln) - self.headline_category(hdln,sntmnt) + self.headline_category(hdln, sntmnt) -if __name__=='__main__': - title().main() - +if __name__ == '__main__': + Title().main()