import re


def parse_AFINN(afinnfile_name):
    """
    Parse the AFIN-111 sentiment file
    
    Input:  afinnfile_name: the [path/] file name of AFIN-111.txt
    
    Output: dicts of:
              sentiment_words: score
              sentiment_phrases: score
            
    Usage: from twitter_functions import parse_AFINN
           sentiment_words, sentiment_phrases = parse_AFINN("AFINN-111.txt")
    """
    afinnfile = open(afinnfile_name)
    
    sentiment_phrases = {}
    sentiment_words   = {}
    for line in afinnfile:
      key, val  = line.split("\t")        
      if " " in key:
        key = re.sub(r"\s{2,}", " ", key) # strip extra whitespace
        sentiment_phrases[key.lower()] = int(val)
      else:
        sentiment_words[key.lower()] = int(val)
    return (sentiment_words, sentiment_phrases)