''' File "heatindex.py" by KWR for CSE199, Fall 2022. Activity support file. Requires: "heatlib.py" in same directory or peer project file. Usage: python3 heatindex.py [optional URL] Or within python3 environment, can call loadDict, heatScore, and processUserInput at will. Can skip "http://" in URL if next comes "www". Computes a "heat index" for a given webpage from a given dictionary of "intense" words and their intensity values in several categories. Tailored to the Canadian National Research Council Affect Intensity Lexicon http://saifmohammad.com/WebPages/AffectIntensity.htm by Saif Mohammad, used with his permission (and request not to redistribute). However, works with any file of lines of the form word #.### category where NRCAIL's categories 'anger', 'fear', 'sadness', 'joy' are not treated specially, so that clients can define any categories they wish. Dictionary file can be local or loaded from a URL. 2022 NOTE: Python 3 Trinket, in various pages of it, seems inconsistent in its handling of urllib.request and the get method, versus using the urlopen method. I have changed the default commented-in lines for heatlib.py lines 105-107 (or so) which define "source", and lines 155-157 (or so) which define "page". But in case of that not working, try reverting to commentin-in the lines after them with "get" ''' from __future__ import division import sys import re from heatlib import loadDict, heatScore def processUserInput(url,heatDict,mulDict): while url != "quit": if url == "mul": mulDict = {} angerMul = float(input("Anger multiplier: ")) fearMul = float(input("Fear multiplier: ")) sadnessMul = float(input("Sadness multiplier: ")) joyMul = float(input("Joy multiplier: ")) mulDict['anger'] = angerMul mulDict['fear'] = fearMul mulDict['sadness'] = sadnessMul mulDict['joy'] = joyMul else: if url.startswith("www"): url = 'http://' + url heatIndex, intenseCountUnique, intenseCount, wordCount, catDict \ = heatScore(url, heatDict, mulDict) for category in sorted (catDict): # parens needed here print("\nWords under '",category,"' and number of times occurring:", sep='') for word in sorted (catDict[category]): print(word, catDict[category][word]) print("\nHeat index with multipliers ",repr(mulDict),"\n" \ "of ", url+" :\n", \ round(heatIndex,3), " from ", intenseCountUnique, \ " intense words (", intenseCount, " category hits) out of ", \ wordCount, " words read.", sep='') url = input("\nPaste a URL, or enter 'mul' to change multipliers," \ + " 'quit' to quit: ") # main [optional URL] numArgs = len(sys.argv) - 1 mulDict = {'anger' : 1, 'fear' : 1, 'sadness' : 1, 'joy' : -1} #location = "NRC-AffectIntensity-Lexicon.txt" location = "https://www.cse.buffalo.edu/~regan/cse199/deepweb/NRC-AffectIntensity-Lexicon.txt" (heatDict,mulDict) = loadDict(location, mulDict) url = "quit" if numArgs >= 1: url = sys.argv[1] else: url = input("Paste a URL: ") processUserInput(url,heatDict,mulDict)