'''
File "heatindex.py" by KWR for CSE199, Fall 2024.  Activity support file.
Requires: "heatlib.py" in same directory or peer project file.
Runs within the Python 3 Trinket web app provided this is "main.py" and the
library file is alongside it under the required name "heatlib.py".

Standalone Usage: python3 heatindex.py [optional URL]   

Within python3 environment, can call loadDict, heatScore, and
processUserInput at will.  Can skip "https://" 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
(NCRAIL, see 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   #.###  (pre-2024 version has: word #.### category)

NRCAIL's original four categories 'anger', 'fear', 'sadness', 'joy' are not treated
specially, so that clients can define any categories they wish.
This is in fact done with the 'disgust' category.
Dictionary file can be local or loaded from a URL.

Python 3 Trinket seems fully functional with version 3 of the Python URL Library
(urllib3).  Commented-out lines in "heatlib.py" were used with earlier versions.
This handling was sometimes inconsistent as the Trinket web app evolved.
In case of glitches with the urllib3-based code, various commented-out lines
might be swapped in to revert to earlier working versions.
'''

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,"', number of times occurring, and score:", sep='')
            for word in sorted (catDict[category]):  
               print(word, ": ", catDict[category][word], " x ", heatDict[(word,category)], sep='')
   
         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)

