Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. from textblob import TextBlob
  2.  
  3. def main():
  4. filename = 'Frankenstein.txt'
  5.  
  6. #open the file and read it
  7. f = open("Frankenstein.txt","r")
  8. removed_words = 0
  9.  
  10. file_content = f.read().rstrip("\n") #read the file as string
  11. word_list = file_content.split() #create the word list
  12.  
  13. num_words = len(word_list)
  14.  
  15. stop_words = create_stopword_dictionary()
  16. for word in word_list:
  17. if word in stop_words:
  18. word_list.remove(word) #remove the word from the list
  19. removed_words += 1 #count the word in the removed_words
  20.  
  21.  
  22. print('Number of words in the text:', num_words)
  23.  
  24. print('Number of stopwords in text:', removed_words)
  25.  
  26. #run textblob with first string of frankenstein
  27. blob = TextBlob(file_content)
  28. sentiment_value = blob.sentiment.polarity
  29. print('Sentiment with stopwords:', format(sentiment_value, '.3f'))
  30.  
  31. # create a string from this list
  32. separator = ' '
  33. text_string_no_stopwords = separator.join(word_list)
  34.  
  35. blob = TextBlob(text_string_no_stopwords)
  36. sentiment_value = blob.sentiment.polarity
  37. print('Sentiment without stopwords:', format(sentiment_value, '.3f'))
  38.  
  39.  
  40. # TODO: and the number of stopwords that occur > 10 times
  41.  
  42.  
  43. def create_stopword_dictionary():
  44. filename = 'Smart_Stoplist.txt'
  45. stopwords = {}
  46. stop_file = open(filename, 'r')
  47.  
  48. value = 0
  49.  
  50.  
  51. for line in stop_file:
  52. line = line.strip()
  53. stopwords[line.strip()] = value
  54.  
  55.  
  56.  
  57.  
  58. return stopwords
  59.  
  60.  
  61. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement