from typing import Counter
import re


def identify_issues(text):
    """
    Identify any issues mentioned in a span of text according to a simple lexicon.
    @text:str
    @returns:Counter - containing counts of how many issues occur in each tweet.
    """
    issues = {
        "Energy and Environment": ['energy', 'oil', 'coal', 'miner?s', 'fracking', 'environment', 'greenhouse gas(es)?',],
        "Crime": ['crime', 'arrests?', 'body cameras', 'police brutality', 'prison reform'],
        "Religion": ['religions?', 'religious'],
        "Government Reform and Campaign Finance": ['government reform', "super pac", "pac", "campaign contributions?"],
        "Jobs": ['jobs', 'unemployment'],
        "Drugs": ['drugs'],
        "Gun Control": ['gun control', 'guns?', 'nra'],
        "Immigration": ['immigration', 'border patrol', 'border', 'deportation', 'immigrants?'],
        "Free Trade": ['free trade', 'trade', 'nafta', 'tpp'],
        "Foreign policy": ['foreign policy'],
        "War & Peace": ['iraq war', 'peace'],
        "Infrastructure & Technology": ['infrastructure',],
        # "Tax Reform": ['tax', 'taxes', 'tax reform'],
        # "Civil Rights": ['civil rights', 'equal rights'],
        "Health Care": ['health care', 'healthcare'],
        # "Families & Children": ['families', 'children'],
        "Welfare & Poverty": ['social welfare'],
        "Criminal Justice": ['criminal justice'],
        "Homeland Security": ['homeland security',],
        # "Corporations": ['corporations'],
        # "Education": ['education'],
        "Abortion": ['abortion', 'planned parenthood'],
        "Gay rights": ['lgbt', 'gay rights', 'lesbian', 'bisexual', 'transgender'],
    }

    counts = Counter()
    matches_at_least_one_issue = False
    for issue, words in issues.items():
        regex = r"\b(%s)\b" % ('|'.join(words))
        if re.search(regex, text) is not None:
            counts[issue] += 1
            matches_at_least_one_issue = True
    if not matches_at_least_one_issue:
        counts["None"] += 1
    return counts