Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6
Created articles.py with citation check and spell check.#20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
4b8bec69de72a28fe1dd1e8cec113d50802e6f4adc3d725088edaa87File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import nltk | ||
| import newspaper | ||
| import re | ||
| class article_content(): | ||
| def __init__(self, article_object): | ||
| #Whenever a class object is made, the article is tokenized and the dictionary file is opened as a set. | ||
| self.tokenized_words = nltk.word_tokenize(article_object.text) | ||
mphirke marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.article_object = article_object | ||
| with open('words_alpha.txt') as word_file: | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is your dictionary not a module input? That would be significantly faster and easier.
| ||
| self.valid_words = set(word_file.read().split()) | ||
| self.misspells = 0 # Set initial misspells to 0. | ||
| def citation_check(self): | ||
| #Something NLTK stop words. These often aren't real citations. / to remove local files, and ad removal from scraped link. | ||
| self.not_citations = ("facebook.com", "twitter.com", "#", "mailto", "plus.google", "advertising", "advertisement", "ad.", "/", "whatsapp", "quora", "nav.", "instagram") | ||
| self.article_html = self.article_object.html | ||
| http = [self.article_object.url] + re.findall(r'href="[^\"]+', self.article_html) #Regex code for finding links, eg href="http://www.bbc.com" returns http://www.bbc.com. | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Soup would be faster at this. But Im quite sure you could get citations and urls from the newspaper module.
| ||
| self.unique_citations = [] #Initialize unique urls to null. | ||
| #Find all cleaned urls (without http, https or www) | ||
| for http_url in http: | ||
| #Convert https to http, convert www to null, convert \ to /, and convert href to null | ||
| http_url = http_url.replace('https', 'http').replace('www.', '').replace('\\', '/').replace('href="','') | ||
| #Split string into two - http:// part and following URL part, then split again using and use the 0th element to get the general source. | ||
| splitted = http_url.split("http://")[-1].split("/") | ||
| if splitted[0].strip().startswith(self.not_citations) is False and splitted[0].strip() not in self.unique_citations: | ||
| self.unique_citations.append(splitted[0]) | ||
| #split_http stores all the unique websites/citations that the article webpage has linked to. | ||
| self.number_unique_citations = len(self.unique_citations) - 1 #Unique citations = Length of all unique website links minus itself | ||
| return self.number_unique_citations | ||
| def spell_check(self): | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function would be quicker if it was ordered like this;
CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change it. | ||
| for word in self.tokenized_words: #For every word in article | ||
| if word[0].isupper()==False: #Ignore if first letter is uppercase, else | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only necessary when the word is not the first word in a sentence. CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! I will try to fix this using punctuations in tokenized_words.
| ||
| word_lowercase = word.lower() #Convert to lowercase | ||
| asciis = [ord(char) for char in word_lowercase] #Find out ASCIIs of word | ||
| if all(ascii_key >= 97 and ascii_key <= 122 for ascii_key in asciis)==True: #Only accept is ASCII's are alphabetical. Numbers cannot be misspelled. | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is indented one too many time. Can you please confirm ? CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you follow 2 space as tab spacing in Python? I'm not sure because Colab sometimes uses 2 spaces but sometimes uses 4 spaces and it gets a little confusing for me too. If you're talking about it code wise, then no. It's working as indented. The word should only be checked further if the first letter is not uppercase. | ||
| if word_lowercase not in self.valid_words and len(word_lowercase)>1: #Check if greater than 1 length and not in dictionary | ||
| self.misspells +=1 | ||
| return self.misspells | ||
| #For prototyping only - | ||
| if(__name__=="__main__"): | ||
| article = newspaper.Article("http://www.bbc.com/future/story/20190801-tomorrows-gods-what-is-the-future-of-religion") | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lets keep the article initiation in the actual program. This is beneficial since the input in the other function also is an url. CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, agreed. I was thinking we can have a main.py in which we make an article object and then call headline.py, article.py, and others, passing the article as an object. The if(name=="main") is only for trial or if someone runs article.py directly. I should probably replace it with an error message. Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes once we are done creating all the required modules, we will surely have to create a main.py. for now we will stick with name==main in every module Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||
| article.download() | ||
| article.parse() | ||
| ArtCon = article_content(article) #Create object. This opens init and tokenizes article. | ||
| print("Number of citiations, ", ArtCon.citation_check()) #Citation check | ||
Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add this to a main function in the class. Simply to keep the actions inside of the class. CollaboratorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm assuming the downloaded and parsed article will be available because of prior processing in headline, so we shouldn't have to do this inside article again, right? Something like Create article object I'm only using the article.download() and article.parse() here because article and headline aren't linked yet with a main function. Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could do that, but it would be a lot faster if we could do those, and more, functions in parallel. Therefore, it would be better to create the article object separately to prevent conflicts. Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we might also need init file ? so we can do all our initialisation ? Collaborator There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary if we just use init function | ||
| print("Number of misspells, ", ArtCon.spell_check()) #Spell check | ||
Uh oh!
There was an error while loading. Please reload this page.





There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
importing newspaper takes significant amount of time. Do you think its possible we import only required module from it like
from newspaper import Article as articleThis will only import the article module as that's all we are using from it. If yes, we need to find a way to pass url to it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good idea! I'll do that.
Although since our article object was named 'article', I think simply importing
from newspaper import Articlewould be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do we pass the url of the article now ?