In: Computer Science
HOW WOULD YOU WRITE THIS IN PYTHON? SHOW THIS IN PYTHON CODE PLEASE
# DEFINE 'all_keywords', A LIST COMPRISED OF ALL OUR NEGATIVE SEARCH KEYWORDS AS REQUIRED BY THE PROJECT # FOR EACH ELEMENT IN 'full_list' (THE LIST OF LISTS YOU WOULD HAVE CREATD ABOVE THE PREVIOUS LINE) # INITIALIZE THE SET 'detected_keywords' TO EMPTY # DEFINE VARIABLE 'current_reviewer' FROM CURRENT ELEMENT OF 'full_list' BY EXTRACTING ITS FIRST SUB-ELEMENT,... # ...CONVERTING IT TO A STRING, AND THEN STRIPPING THE SUBSTRING '<Author>' OFF THIS STRING # GENERATE THE LIST 'separated_words' COMPRISED OF EACH SEPARATE WORD IN THIS REVIEWER'S COMMENTS, BY EXTRACTING THE SECOND SUB-ELEMENT... # ...FROM THE CURRENT ELEMENT AND EXECUTING THE '.split' METHOD ON IT # FOR EACH ELEMENT IN 'separated_words' # FOR EACH ELEMENT IN 'all_keywords' # if LOWER-CASE OF 'all_keywords' IS CONTAINED IN LOWER-CASE OF 'separated_words' # ADD THE CURRENT ELEMENT OF 'all_keywords' TO THE SET 'detected_keywords' # IF LENGTH OF 'detected_keywords' IS MORE THAN ZERO # PRINT A LINE SUCH AS -- XYZ USED THE FOLLOWING KEYWORDS: {'bad,'rough'} # ELSE # PRINT A LINE SUCH AS -- XYZ DID NOT USE ANY NEGATIVE KEYWORDS.
SOLUTION:
EXPLANATION: Explanation is done in the code itself Thank you
## negative Words list
all_keywords=["bad","rough","not-good","not-acceptable","irresponsible","not-working","adverse","damage","faulty","evil","damage","adverse","annoying"]
full_list=[["vinu","the producct i have purchased is annoying
and bad"],["abhi","it is annoying and damage even"],["devathi","The
people are irresponsible and it is not-acceptable"]]
## list to store the reviewer and thier comments
for element in full_list:
##pass through every reviewer comment
detected_keywords= set()
##set to store negative words used
current_reviewer=element[0]
##string to store the reviewer name
separated_words=element[1].split(" ")
##split the entire string into words
##check the every word used by reviewer is in the negative words
or not
for word in separated_words:
for badWords in all_keywords:
##for case in sensitive matching convert both of them to lower
case
if(word.lower() == badWords.lower()):
##if any word is found add it to the detected words list
detected_keywords.add(word)
if(len(detected_keywords)>0):
##if there are any detected words print them
print(current_reviewer," USED THE FOLLOWING KEYWORDS :
",detected_keywords)
else:
## if any negative word is not found
print(current_reviewer," DID NOT USE ANY NEGATIVE KEYWORDS ")
CODE IMAGE:
OUTPUT: