Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Sentiment Counter Write a script called senticounter.py. Define a function run()

ID: 3886294 • Letter: S

Question

Sentiment Counter Write a script called senticounter.py. Define a function run() inside senticounter.py. The function should: Accept as a parameter the path to a text file. The text file has one review per line. Read the list of positive words from the positive-words.txt file. Read the list of negative words from the negative- words.txt file. We define a NO-NEG review to be a review that includes no negative words. Create a dictionary that includes one key for each positive word that appears in the input text file. The dictionary should map each of these positive words to the number of NO-NEG reviews that include it. For example, if the word "great" appears in 5 NO-NEG reviews, then the dictionary should map the key "great" to the value 5. Return the dictionary Notes: ignore case. You can also assume that the input file includes only letters, no punctuation or other special characters

Explanation / Answer

main.py


def loadLexicon(fname):
newLex=set()
lex_conn=open(fname, mode='r', encoding='utf8')
for line in lex_conn:
newLex.add(line.strip())
lex_conn.close()

return newLex

def run(path):
dict = {}
posLex=loadLexicon('positive-words.txt')
  
fin=open(path) # open the file
for line in fin:
posList=[]
line=line.lower().strip() # cleaning the input line
  
words=line.split(' ')
for word in words:
if word in posLex:
if word not in posList:
posList.append(word)
for word in posList: # for every word in the posList
if word in dict:
dict[word] = dict[word] + 1
else:
dict[word] = 1
  
fin.close() # close the file
return dict # returning the dictionary

#main
if __name__ == "__main__":
print(run('textfile')) # running the function

  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote