Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from textblob import TextBlob
- def main():
- filename = 'Frankenstein.txt'
- #open the file and read it
- f = open("Frankenstein.txt","r")
- removed_words = 0
- file_content = f.read().rstrip("\n") #read the file as string
- word_list = file_content.split() #create the word list
- num_words = len(word_list)
- stop_words = create_stopword_dictionary()
- for word in word_list:
- if word in stop_words:
- word_list.remove(word) #remove the word from the list
- removed_words += 1 #count the word in the removed_words
- print('Number of words in the text:', num_words)
- print('Number of stopwords in text:', removed_words)
- #run textblob with first string of frankenstein
- blob = TextBlob(file_content)
- sentiment_value = blob.sentiment.polarity
- print('Sentiment with stopwords:', format(sentiment_value, '.3f'))
- # create a string from this list
- separator = ' '
- text_string_no_stopwords = separator.join(word_list)
- blob = TextBlob(text_string_no_stopwords)
- sentiment_value = blob.sentiment.polarity
- print('Sentiment without stopwords:', format(sentiment_value, '.3f'))
- # TODO: and the number of stopwords that occur > 10 times
- def create_stopword_dictionary():
- filename = 'Smart_Stoplist.txt'
- stopwords = {}
- stop_file = open(filename, 'r')
- value = 0
- for line in stop_file:
- line = line.strip()
- stopwords[line.strip()] = value
- return stopwords
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement