Files
reddit_stock_analyzer/ticker_extractor.py

46 lines
2.0 KiB
Python

# ticker_extractor.py
import re
# A set of common English words and acronyms that look like stock tickers.
# This helps reduce false positives.
COMMON_WORDS_BLACKLIST = {
"A", "I", "DD", "CEO", "CFO", "CTO", "EPS", "IPO", "YOLO", "FOMO",
"TLDR", "EDIT", "THE", "AND", "FOR", "ARE", "BUT", "NOT", "YOU",
"ALL", "ANY", "CAN", "HAS", "NEW", "NOW", "OLD", "SEE", "TWO",
"WAY", "WHO", "WHY", "BIG", "BUY", "SELL", "HOLD", "BE", "GO",
"ON", "AT", "IN", "IS", "IT", "OF", "OR", "TO", "WE", "UP",
"OUT", "SO", "RH", "SEC", "IRS", "USA", "UK", "EU",
"AI", "ML", "AR", "VR", "NFT", "DAO", "WEB3", "ETH", "BTC", "DOGE",
"USD", "EUR", "GBP", "JPY", "CNY", "INR", "AUD", "CAD", "CHF",
"RUB", "ZAR", "BRL", "MXN", "HKD", "SGD", "NZD", "RSD",
"JPY", "KRW", "SEK", "NOK", "DKK", "PLN", "CZK", "HUF", "TRY",
"US", "IRA", "FDA", "SEC", "FBI", "CIA", "NSA", "NATO", "FINRA",
"NASDAQ", "NYSE", "AMEX", "FTSE", "DAX", "WSB", "SPX", "DJIA",
"EDGAR", "GDP", "CPI", "PPI", "PMI", "ISM", "FOMC", "ECB", "BOE",
"BOJ", "RBA", "RBNZ", "BIS", "NFA", "P", "VOO", "CTB", "DR",
"ETF", "EV", "ESG", "REIT", "SPAC", "IPO", "M&A", "LBO",
"Q1", "Q2", "Q3", "Q4", "FY", "FAQ", "ROI", "ROE", "EPS", "P/E", "PEG",
"FRG", "FXAIX", "FXIAX", "FZROX"
}
def extract_tickers(text):
"""
Extracts potential stock tickers from a given piece of text.
A ticker is identified as a 1-5 character uppercase word, or a word prefixed with $.
"""
# Regex to find potential tickers:
# 1. Words prefixed with $: $AAPL, $TSLA
# 2. All-caps words between 1 and 5 characters: GME, AMC
ticker_regex = r"\$[A-Z]{1,5}\b|\b[A-Z]{1,5}\b"
potential_tickers = re.findall(ticker_regex, text)
# Filter out common words and remove the '$' prefix
tickers = []
for ticker in potential_tickers:
cleaned_ticker = ticker.replace("$", "").upper()
if cleaned_ticker not in COMMON_WORDS_BLACKLIST:
tickers.append(cleaned_ticker)
return tickers