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