Description
In Python, compute the following:
(A) Define a function tokenize to tokenize text and remove stop words
(B) Define a function sentiment_analysis to count the positive and negative words in the tokenized text. [Positive words are either a positive word not preceded by a negation word, or a negative word preceded by a negation word. Negative words are either a negative word not preceded by a negation word or a positive word preceded by a negation word.] Determine the sentiment of the string as follows: 2: number of positive words > number of negative words or 1: number of positive words <= number of negative words. Return sentiment.
(C) Define a function performance_evaluate the accuracy of the sentiment analysis in (B). Takes an input file ("amazon_review_300.csv"), a list of positive words, and a list of negative words as inputs. The input file has a list of reviews in the format of (label, review). Reads the input file to get a list of reviews including review text and label of each review. For each review, predict its sentiment using the function defined in. Return the accuracy as the number of correct sentiment predictions/total review.
# Structure of solution is as follows:
import nltk
# add import statements
def tokenize(text):
tokens = []
# add your code
return tokens
def sentiment_analysis(text, positive_words, negative_words):
negations=["not", "no", "isn't", "wasn't", "aren't", \ "weren't", "don't", "didn't", "cannot", \ "couldn't", "won't", "neither", "nor"]
sentiment = None
# add your code
return sentiment
def performance_evaluate(input_file, positive_words, negative_words):
accuracy = None
# add your code
return accuracy
Unformatted Attachment Preview
Purchase answer to see full attachment
Explanation & Answer
Hi, please check the uploaded files. I have provided as small list of positive and negative words in the txt files. All thee best.
["abandon", "abandoned", "abandoning", "abandonment", "abandonments", "abandons",
"abdicated", "abdicates", "abdicating", "abdication", "abdications", "aberrant",
"aberration", "aberrational", "aberrations", "abetting", "abnormal",
"abnormalities", "abnormality", "abnormally", "abolish", "abolished", "abolishes",
"abolishing", "abrogate", "abrogated", "abrogates", "abrogating", "abrogation",
"abrogations", "abrupt", "abruptly", "abruptness", "absence", "absences",
"absenteeism", "abuse", "abused", "abuses", "abusing", "abusive", "abusively",
"abusiveness", "accident", "accidental", "accidentally", "accidents", "accusation",
"accusations", "accuse", "accused", "accuses", "accusing", "acquiesce",
"acquiesced", "acquiesces", "acquiescing", "acquit", "acquits", "acquittal",
"acquittals", "acquitted", "acquitting", "adulterate", "adulterated",
"adulterating", "adulteration", "adulterations", "adversarial", "adversaries",
"adversary", "adverse", "adversely", "adversities", "adversity", "aftermath",
"aftermaths", "against", "aggravate", "aggravated", "aggravates", "aggravating",
"aggravation", "aggravations", "alerted", "alerting", "alienate", "alienated",
"alienates", "alienating", "alienation", "alienations", "allegation",
"allegations", "allege", "alleged", "allegedly", "alleges", "alleging", "annoy",
"annoyance", "annoyances", "annoyed", "annoying", "annoys", "annul", "annulled",
"annulling", "annulment", "annulments", "annuls", "anomalies", "anomalous",
"anomalously", "anomaly", "anticompetitive", "antitrust", "argue", "argued",
"arguing", "argument", "argumentative", "arguments", "arrearage", "arrearages",
"arrears", "arrest", "arrested", "arrests", "artificially", "assault", "assaulted",
"assaulting", "assaults", "assertions", "attrition", "aversely", "backdating",
"bad", "bail", "bailout", "balk", "balked", "bankrupt", "bankruptcies",
"bankruptcy", "bankrupted", "bankrupting", "bankrupts", "bans", "barred",
"barrier", "barriers", "bottleneck", "bottlenecks", "boycott", "boycotted",
"boycotting", "boycotts", "breach", "breached", "breaches", "breaching", "break",
"breakage", "breakages", "breakdown", "breakdowns", "breaking", "breaks", "bribe",
"bribed", "briberies", "bribery", "bribes", "bribing", "bridge", "broken",
"burden", "burdened", "burdening", "burdens", "burdensome", "burned", "calamities",
"calamitous", "calamity", "cancel", "canceled", "canceling", "cancellation",
"cancellations", "cancelled", "cancelling", "cancels", "careless", "carelessly",
"carelessness", "catastrophe", "catastrophes", "catastrophic", "catastrophically",
"caution", "cautionary", "cautioned", "cautioning", "cautions", "cease", "ceased",
"ceases", "ceasing", "censure", "censured", "censures", "censuring", "challenge",
"challenged", "challenges", "challenging", "chargeoffs", "circumvent",
"circumvented", "circumventing", "circumvention", "circumventions", "circumvents",
"claiming", "claims", "clawback", "closed", "closeout", "closeouts", "closing",
"closings", "closure", "closures", "coerce", "coerced", "coerces", "coercing",
"coercion", "coercive", "collapse", "collapsed", "collapses", "collapsing",
"collision", "collisions", "collude", "colluded", "colludes", "colluding",
"collusion", "collusions", "collusive", "complain", "complained", "complaining",
"complains", "complaint", "complaints", "complicate", "complicated", "complicates",
"complicating", "complication", "complications", "compulsion", "concealed",
"concealing", "concede", "conceded", "concedes", "conceding", "concern",
"concerned", "concerns", "conciliating", "conciliation", "conciliations",
"condemn", "condemnation", "condemnations", "condemned", "condemning", "condemns",
"condone", "condoned", "confess", "confessed", "confesses", "confessing",
"confession", "confine", "confined", "confinement", "confinements", "confines",
"confining", "confiscate", "confiscated", "confiscates", "confiscating",
"confiscation", "confiscations", "conflict", "conflicted", "conflicting",
"conflicts", "confront", "confrontation", "confrontational", "confrontations",
"confronted", "confronting", "confronts", "confuse", "confused", "confuses",
"confusing", "confusingly", "confusion", "conspiracies", "conspiracy",
"conspirator", "conspiratorial", "conspirators", "conspire", "conspired",
"conspires", "conspiring", "contempt", "contend", "contended", "contending",
"contends", "contention", "contentions", "contentious", "contentiously",
"contested", "contesting", "contraction", "contractions", "contradict",
"contradicted", "contradicting", "contradiction", "contradictions",
"contradictory", "contradicts", "contrary", "controversial", "controversies",
"controversy", "convict", "convicted", "convicting", "conviction", "convictions",
"corrected", "correcting", "correction", "corrections", "corrects", "corrupt",
"corrupted", "corrupting", "corruption", "corruptions", "corruptly", "corruptness",
"costly", "counterclaim", "counterclaimed", "counterclaiming", "counterclaims",
"counterfeit", "counterfeited", "counterfeiter", "counterfeiters",
"counterfeiting", "counterfeits", "countermeasure", "countermeasures", "crime",
"crimes", "criminal", "criminally", "criminals", "crises", "crisis", "critical",
"critically", "criticism", "criticisms", "criticize", "criticized", "criticizes",
"criticizing", "crucial", "crucially", "culpability", "culpable", "culpably",
"cumbersome", "curtail", "curtailed", "curtailing", "curtailment", "curtailments",
"curtails", "cut", "cutback", "cutbacks", "cyberattack", "cyberattacks",
"cyberbullying", "cybercrime", "cybercrimes", "cybercriminal", "cybercriminals",
"damage", "damaged", "damages", "damaging", "dampen", "dampened", "danger",
"dangerous", "dangerously", "dangers", "deadlock", "deadlocked", "deadlocking",
"deadlocks", "deadweight", "deadweights", "debarment", "debarments", "debarred",
"deceased", "deceit", "deceitful", "deceitfulness", "deceive", "deceived",
"deceives", "deceiving", "deception", "deceptions", "deceptive", "deceptively",
"decline", "declined", "declines", "declining", "deface", "defaced", "defacement",
"defamation", "defamations", "defamatory", "defame", "defamed", "defames",
"defaming", "default", "defaulted", "defaulting", "defaults", "defeat", "defeated",
"defeating", "defeats", "defect", "defective", "defects", "defend", "defendant",
"defendants", "defended", "defending", "defends", "defensive", "defer",
"deficiencies", "deficiency", "deficient", "deficit", "deficits", "defraud",
"defrauded", "defrauding", "defrauds", "defunct", "degradation", "degradations",
"degrade", "degraded", "degrades", "degrading", "delay", "delayed", "delaying",
"delays", "deleterious", "deliberate", "deliberated", "deliberately",
"delinquencies", "delinquency", "delinquent", "delinquently", "delinquents",
"delist", "delisted", "delisting", "delists", "demise", "demised", "demises",
"demising", "demolish", "demolished", "demolishes", "demolishing", "demolition",
"demolitions", "demote", "demoted", "demotes", "demoting", "demotion", "demotions",
"denial", "denials", "denied", "denies", "denigrate", "denigrated", "denigrates",
"denigrating", "denigration", "deny", "denying", "deplete", "depleted", "depletes",
"depleting", "depletion", "depletions", "deprecation", "depress", "depressed",
"depresses", "depressing", "deprivation", "deprive", "deprived", "deprives",
"depriving", "derelict", "dereliction", "derogatory", "destabilization",
"destabilize", "destabilized", "destabilizing", "destroy", "destroyed",
"destroying", "destroys", "destruction", "destructive", "detain", "detained",
"detention", "detentions", "deter", "deteriorate", "deteriorated", "deteriorates",
"deteriorating", "deterioration", "deteriorations", "deterred", "deterrence",
"deterrences", "deterrent", "deterrents", "deterring", "deters", "detract",
"detracted", "detracting", "detriment", "detrimental", "detrimentally",
"detriments", "devalue", "devalued", "devalues", "devaluing", "devastate",
"devastated", "devastating", "devastation", "deviate", "deviated", "deviates",
"deviating", "deviation", "deviations", "devolve", "devolved", "devolves",
"devolving", "difficult", "difficulties", "difficultly", "difficult...