|
|
@@ -3,6 +3,45 @@ import string |
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__) |
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
ALIASES = { |
|
|
|
|
|
'federal reserve': 'fed', |
|
|
|
|
|
'united states': 'us', |
|
|
|
|
|
'united kingdom': 'uk', |
|
|
|
|
|
'european union': 'eu', |
|
|
|
|
|
'north atlantic treaty organization': 'nato', |
|
|
|
|
|
'united nations': 'un', |
|
|
|
|
|
'international monetary fund': 'imf', |
|
|
|
|
|
'world trade organization': 'wto', |
|
|
|
|
|
'central intelligence agency': 'cia', |
|
|
|
|
|
'federal bureau of investigation': 'fbi', |
|
|
|
|
|
'department of justice': 'doj', |
|
|
|
|
|
'department of defense': 'dod', |
|
|
|
|
|
'environmental protection agency': 'epa', |
|
|
|
|
|
'securities and exchange commission': 'sec', |
|
|
|
|
|
'internal revenue service': 'irs', |
|
|
|
|
|
'social security administration': 'ssa', |
|
|
|
|
|
'centers for disease control': 'cdc', |
|
|
|
|
|
'national security agency': 'nsa', |
|
|
|
|
|
'department of homeland security': 'dhs', |
|
|
|
|
|
'supreme court': 'scotus', |
|
|
|
|
|
'republican party': 'gop', |
|
|
|
|
|
'democratic party': 'democrats', |
|
|
|
|
|
'president of the united states': 'potus', |
|
|
|
|
|
'vice president': 'vp', |
|
|
|
|
|
'prime minister': 'pm', |
|
|
|
|
|
'secretary of state': 'secstate', |
|
|
|
|
|
'attorney general': 'ag', |
|
|
|
|
|
'gross domestic product': 'gdp', |
|
|
|
|
|
'consumer price index': 'cpi', |
|
|
|
|
|
'unemployment rate': 'unemployment', |
|
|
|
|
|
'inflation rate': 'inflation', |
|
|
|
|
|
'national debt': 'debt', |
|
|
|
|
|
'budget deficit': 'deficit', |
|
|
|
|
|
'middle east': 'mideast', |
|
|
|
|
|
'asia pacific': 'apac', |
|
|
|
|
|
'latin america': 'latam' |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_stopwords(path): |
|
|
def get_stopwords(path): |
|
|
logger.info("Attempting to load stopwords from file: '%s'", path) |
|
|
logger.info("Attempting to load stopwords from file: '%s'", path) |
|
|
@@ -117,6 +156,15 @@ def normalize_headline(text, stopwords): |
|
|
for char in punctuation: |
|
|
for char in punctuation: |
|
|
headline = headline.replace(char, '') |
|
|
headline = headline.replace(char, '') |
|
|
logger.debug("Headline after lowercasing and punctuation removal: %r", headline) |
|
|
logger.debug("Headline after lowercasing and punctuation removal: %r", headline) |
|
|
|
|
|
headline = replace_common_aliases(headline) |
|
|
|
|
|
logger.debug("Headline after replacing common aliases: %r", headline) |
|
|
normalized = remove_stopwords(headline, stopwords) |
|
|
normalized = remove_stopwords(headline, stopwords) |
|
|
logger.debug("Completed normalization for %r -> %s", text, normalized) |
|
|
logger.debug("Completed normalization for %r -> %s", text, normalized) |
|
|
return normalized |
|
|
|
|
|
|
|
|
return normalized |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def replace_common_aliases(headline): |
|
|
|
|
|
for alias in ALIASES: |
|
|
|
|
|
headline = headline.replace(alias, ALIASES[alias]) |
|
|
|
|
|
|
|
|
|
|
|
return headline |