228k views
0 votes
Next, copy in your strip_punctuation function and define a function called get_pos which takes one parameter, a string which represents one or more sentences, and calculates how many words in the string are considered positive words. Use the list, positive_words to determine what words will count as positive. The function should return a positive integer - how many occurrences there are of positive words in the text. Note that all of the words in positive_words are lower cased, so you’ll need to convert all the words in the input string to lower case as well.

punctuation_chars = ["", ","," 3 # list of positive words to use 4 positive_words = 0 5 with open ("positive_words.txt") as pos_f: for lin in pos_f: if lin[0] != ';' and lin[0] != '\\': positive_words.append(lin.strip) 6 7 8 9 10

1 Answer

2 votes

Answer:

Here is the get_pos function:

def get_post(string): #function that takes string as parameter

string=string.lower() #converts the string to lower case

string=strip_punctuation(string) #calls strip_punctuation to remove punctuation from string

string=string.split() #split the string into a list

count_positive=0 #counts the occurrences of positive words

for word in string: #loops through the string

if word in positive_words: #if a word in a string is a positive words

count_positive=count_positive+1 #adds 1 to the count of count_positive each time a positive word appears

return count_positive #returns the positive integer - how many occurrences there are of positive words in the string

Step-by-step explanation:

In order to explain the working of this function lets suppose the punctuation_chars list contains the following punctuation characters:

punctuation_chars = ["'", '"', ",", ".", "!", ":","?", ";", '#']

Lets suppose the positive_words.txt file contains the following two words i.e. yes okay

Lets suppose the string is Hi! I am okay, see you later.

string = " Hi! I am okay, see you later. "

string=string.lower() statement converts the string to lower case:

string = hi! i am okay, see you later.

string=strip_punctuation(string) this statement calls strip_punctuation method to remove the punctuation from string.

string = hi i am okay see you later

string=string.split() this statement splits the sentence into list

['hi', 'i', 'am', 'okay', 'see', 'you', 'later']

for word in string: this loop iterates through the string

At first iteration if condition checks if word "hi" is in positive_words. This is not true because positive_words only contain the words okay and yes.

At next iteration if condition checks if word "i" is in positive_words. This is not true because positive_words only contain the words okay and yes.

At next iteration if condition checks if word "am" is in positive_words. This is also not true.

At next iteration if condition checks if word "okay" is in positive_words. This is true because positive_words contains the words okay. So count_positive=count_positive+1 statement executes which adds 1 to the count of count_positive so

count_positive = 1

At next iteration if condition if word in positive_words checks if word "see" is in positive_words. This is not true.

At next iteration if condition checks if word "you" is in positive_words. This is also not true.

At next iteration if condition checks if word "later" is in positive_words. This is also not true.

Now the statement: return count_positive returns the occurrence of positive words in the string which is 1. So the output of this is 1.

Here is the strip_punctuation method:

def strip_punctuation(word): #removes punctuation from words

New_word="" #initializes new word string to empty

print("Word is: ",word) #prints the word

for w in word: #iterates through words

if w not in punctuation_chars: #if word is not in punctuation_char list

New_word=New_word+w #adds that word to new word string

return New_word #returns word after removing punctuation

User Morechilli
by
4.8k points