Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

20.4 KB
Raw Blame
"""
News NLP Pipeline — entity extraction, geolocation, batch sentiment.
Called by NewsNlpService / EquitySentimentService via PythonRunner.

Commands:
  enrich_batch <json_articles>             NER + geolocation per article, one process
  extract_entities <json_headlines>
  analyze_sentiment_batch <json_headlines>

<json_...> may be "@<path>" (PythonRunner spills large payloads to a temp file).
"""
import sys
import json
import re
import math
from collections import Counter

# ── Country/Region database ──────────────────────────────────────────────────

COUNTRIES = {
    "united states": "US", "usa": "US", "u.s.": "US", "america": "US",
    "china": "CN", "chinese": "CN", "beijing": "CN", "shanghai": "CN",
    "russia": "RU", "russian": "RU", "moscow": "RU", "kremlin": "RU",
    "ukraine": "UA", "ukrainian": "UA", "kyiv": "UA", "kiev": "UA",
    "united kingdom": "GB", "britain": "GB", "british": "GB", "london": "GB", "uk": "GB",
    "germany": "DE", "german": "DE", "berlin": "DE",
    "france": "FR", "french": "FR", "paris": "FR",
    "japan": "JP", "japanese": "JP", "tokyo": "JP",
    "india": "IN", "indian": "IN", "mumbai": "IN", "delhi": "IN",
    "brazil": "BR", "brazilian": "BR",
    "canada": "CA", "canadian": "CA",
    "australia": "AU", "australian": "AU",
    "south korea": "KR", "korean": "KR", "seoul": "KR",
    "north korea": "KP", "pyongyang": "KP",
    "iran": "IR", "iranian": "IR", "tehran": "IR",
    "israel": "IL", "israeli": "IL", "tel aviv": "IL",
    "palestine": "PS", "palestinian": "PS", "gaza": "PS", "west bank": "PS",
    "saudi arabia": "SA", "saudi": "SA", "riyadh": "SA",
    "turkey": "TR", "turkish": "TR", "ankara": "TR", "istanbul": "TR",
    "egypt": "EG", "egyptian": "EG", "cairo": "EG",
    "italy": "IT", "italian": "IT", "rome": "IT",
    "spain": "ES", "spanish": "ES", "madrid": "ES",
    "mexico": "MX", "mexican": "MX",
    "indonesia": "ID", "indonesian": "ID", "jakarta": "ID",
    "netherlands": "NL", "dutch": "NL",
    "switzerland": "CH", "swiss": "CH",
    "sweden": "SE", "swedish": "SE",
    "norway": "NO", "norwegian": "NO",
    "poland": "PL", "polish": "PL", "warsaw": "PL",
    "taiwan": "TW", "taiwanese": "TW", "taipei": "TW",
    "singapore": "SG",
    "hong kong": "HK",
    "south africa": "ZA",
    "nigeria": "NG", "nigerian": "NG", "lagos": "NG",
    "argentina": "AR",
    "colombia": "CO",
    "pakistan": "PK", "pakistani": "PK",
    "thailand": "TH", "thai": "TH", "bangkok": "TH",
    "vietnam": "VN", "vietnamese": "VN",
    "philippines": "PH", "filipino": "PH",
    "malaysia": "MY", "malaysian": "MY",
    "iraq": "IQ", "iraqi": "IQ", "baghdad": "IQ",
    "syria": "SY", "syrian": "SY", "damascus": "SY",
    "yemen": "YE", "yemeni": "YE",
    "libya": "LY", "libyan": "LY",
    "lebanon": "LB", "lebanese": "LB", "beirut": "LB",
    "afghanistan": "AF", "afghan": "AF", "kabul": "AF",
    "myanmar": "MM", "burmese": "MM",
    "ethiopia": "ET", "ethiopian": "ET",
    "sudan": "SD", "sudanese": "SD", "khartoum": "SD",
    "somalia": "SO", "somali": "SO",
    "venezuela": "VE", "venezuelan": "VE",
    "cuba": "CU", "cuban": "CU", "havana": "CU",
    "european union": "EU", "eu": "EU", "brussels": "EU",
}

# ── Organization patterns ────────────────────────────────────────────────────

ORGANIZATIONS = {
    "nato": "NATO", "united nations": "UN", "un": "UN",
    "imf": "IMF", "world bank": "World Bank",
    "federal reserve": "Federal Reserve", "the fed": "Federal Reserve",
    "ecb": "ECB", "european central bank": "ECB",
    "bank of england": "Bank of England", "boe": "Bank of England",
    "bank of japan": "Bank of Japan", "boj": "Bank of Japan",
    "peoples bank of china": "PBOC", "pboc": "PBOC",
    "sec": "SEC", "securities and exchange": "SEC",
    "opec": "OPEC", "opec+": "OPEC+",
    "world health organization": "WHO", "who": "WHO",
    "world trade organization": "WTO", "wto": "WTO",
    "pentagon": "Pentagon", "white house": "White House",
    "congress": "US Congress", "senate": "US Senate",
    "european commission": "European Commission",
    "iaea": "IAEA", "g7": "G7", "g20": "G20", "brics": "BRICS",
    "reuters": "Reuters", "bloomberg": "Bloomberg",
    "apple": "Apple", "google": "Google", "alphabet": "Alphabet",
    "microsoft": "Microsoft", "amazon": "Amazon", "meta": "Meta",
    "nvidia": "NVIDIA", "tesla": "Tesla", "openai": "OpenAI",
    "goldman sachs": "Goldman Sachs", "jpmorgan": "JPMorgan",
    "blackrock": "BlackRock", "berkshire hathaway": "Berkshire Hathaway",
    "hsbc": "HSBC", "ubs": "UBS", "credit suisse": "Credit Suisse",
    "deutsche bank": "Deutsche Bank", "barclays": "Barclays",
    "citigroup": "Citigroup", "morgan stanley": "Morgan Stanley",
}

# ── Person title patterns ────────────────────────────────────────────────────

PERSON_TITLES = [
    r"\b(president|prime minister|chancellor|king|queen|prince|princess)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)",
    r"\b(ceo|cfo|chairman|director|minister|secretary|governor|general)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)",
    r"\b(mr|mrs|ms|dr|prof)\.\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)",
]

# Known leaders
KNOWN_PERSONS = {
    "biden": "Joe Biden", "trump": "Donald Trump", "obama": "Barack Obama",
    "putin": "Vladimir Putin", "xi jinping": "Xi Jinping", "xi": "Xi Jinping",
    "modi": "Narendra Modi", "macron": "Emmanuel Macron",
    "scholz": "Olaf Scholz", "sunak": "Rishi Sunak", "starmer": "Keir Starmer",
    "kishida": "Fumio Kishida", "trudeau": "Justin Trudeau",
    "zelensky": "Volodymyr Zelensky", "zelenskyy": "Volodymyr Zelensky",
    "netanyahu": "Benjamin Netanyahu",
    "erdogan": "Recep Tayyip Erdogan",
    "musk": "Elon Musk", "bezos": "Jeff Bezos", "zuckerberg": "Mark Zuckerberg",
    "cook": "Tim Cook", "nadella": "Satya Nadella", "altman": "Sam Altman",
    "dimon": "Jamie Dimon", "powell": "Jerome Powell", "lagarde": "Christine Lagarde",
    "yellen": "Janet Yellen",
}


def _boundary_regex(term):
    """Whole-word matcher for a lowercase term (applied to lowercased text).

    Uses look-arounds instead of ``\\b`` so terms that end in punctuation
    ("u.s.", "opec+") still match. Bare substring tests used to hit inside other
    words — "un" in "fund", "sec" in "second", "boe" in "boeing", "xi" in "taxi".
    """
    return re.compile(r'(?<![a-z0-9])' + re.escape(term) + r'(?![a-z0-9])')


def _proper_noun_regex(term):
    """Matcher for short/ambiguous terms: only the Capitalised or UPPER form in
    the original-case text counts ("UN", "SEC", "WHO", "Xi" — not "who", "sec")."""
    return re.compile(r'(?<![A-Za-z0-9])(?:' + re.escape(term.capitalize()) + '|' +
                      re.escape(term.upper()) + r')(?![A-Za-z0-9])')


# Canonical display name per country code = the first alias listed for it.
_COUNTRY_CANON = {}
for _p, _c in COUNTRIES.items():
    _COUNTRY_CANON.setdefault(_c, _p)

# (regex, uses_original_case, canonical name) — compiled once per process.
_COUNTRY_MATCHERS = [(_boundary_regex(p) if " " not in p else None, p, c) for p, c in COUNTRIES.items()]
_ORG_MATCHERS = [((_proper_noun_regex(p), True) if len(p) <= 3 else (_boundary_regex(p), False), n)
                 for p, n in ORGANIZATIONS.items()]
_PERSON_MATCHERS = [((_proper_noun_regex(p), True) if len(p) <= 4 else (_boundary_regex(p), False), n)
                    for p, n in KNOWN_PERSONS.items()]

# Words / news acronyms that look like tickers but are not. Kept in step with
# NewsService::enrich_article() on the C++ side.
_TICKER_STOPWORDS = {
    "THE", "FOR", "AND", "BUT", "NOT", "FROM", "WITH", "THIS", "THAT", "HAVE", "WILL", "BEEN",
    "THEY", "WERE", "SAID", "HAS", "ITS", "NEW", "ARE", "WAS", "WHO", "HOW", "WHY", "ALL", "CAN",
    "MAY", "NOW", "OUT", "ONE", "TWO", "OUR", "YOU", "HER", "HIS", "SAYS", "SAY", "GET", "GOT",
    "LET", "TOP", "BIG", "NEXT", "OVER", "INTO", "AFTER", "WHAT", "WHEN", "ABOUT", "AM", "PM",
    "TV", "PC", "OK", "VS", "NO", "OR", "IN", "IS", "OF", "ON", "TO", "AT", "BY", "AS", "AN",
    "IF", "SO", "UP", "DO", "GO", "BE", "WE", "HE", "ME", "MY", "US", "UK", "EU", "UN", "UAE",
    "USA", "AI", "CEO", "CFO", "COO", "CTO", "IPO", "ETF", "GDP", "CPI", "PPI", "PMI", "FED",
    "FOMC", "ECB", "BOE", "BOJ", "PBOC", "IMF", "WTO", "NATO", "OPEC", "SEC", "FDA", "DOJ", "FBI",
    "CIA", "NSA", "IRS", "FTC", "FCC", "EPA", "NYSE", "DOW", "USD", "EUR", "GBP", "JPY", "CNY",
    "INR", "AUD", "CAD", "CHF", "GOP", "ESG", "COVID", "UPDATE", "LIVE", "VIDEO", "WATCH",
    "EXCLUSIVE", "ALERT", "BREAKING", "URGENT", "NEWS", "TIMES", "POST",
}
_CASHTAG_RE = re.compile(r'\$([A-Z]{1,5})\b')
_TICKER_RE = re.compile(r'\b[A-Z]{2,5}\b')


def _extract_tickers(text_orig):
    """Ordered ticker candidates: cashtags first, then bare uppercase words.
    Skips bare words entirely when the text is shouted in capitals."""
    found = [m.group(1) for m in _CASHTAG_RE.finditer(text_orig)]
    letters = sum(1 for ch in text_orig if ch.isalpha())
    upper = sum(1 for ch in text_orig if ch.isalpha() and ch.isupper())
    if not (letters >= 24 and upper * 10 >= letters * 6):
        found.extend(t for t in _TICKER_RE.findall(text_orig) if t not in _TICKER_STOPWORDS)
    return found


def extract_entities(headlines_json):
    """Extract countries, organizations, people, and tickers from headlines."""
    try:
        articles = json.loads(headlines_json) if isinstance(headlines_json, str) else headlines_json
    except json.JSONDecodeError:
        return {"success": False, "error": "Invalid JSON"}

    all_countries = Counter()
    all_orgs = Counter()
    all_people = Counter()
    all_tickers = Counter()
    per_article = []

    for article in articles:
        headline = article.get("headline", "")
        summary = article.get("summary", "")
        text = (headline + " " + summary).lower()
        text_orig = headline + " " + summary

        countries = []
        orgs = []
        people = []
        tickers = []

        # Country extraction — whole-word matching for single-word patterns to
        # avoid substring hits (e.g. "rome" inside "jerome", "uk" inside "truck",
        # "thai" inside "thailand"); multi-word patterns are specific enough for
        # a plain substring test.
        for regex, pattern, code in _COUNTRY_MATCHERS:
            hit = regex.search(text) if regex is not None else (pattern in text)
            if hit:
                countries.append({"name": _COUNTRY_CANON.get(code, pattern).title(), "code": code})

        # Organization extraction
        for (regex, orig_case), name in _ORG_MATCHERS:
            if regex.search(text_orig if orig_case else text):
                orgs.append(name)

        # Person extraction — known persons
        for (regex, orig_case), name in _PERSON_MATCHERS:
            if regex.search(text_orig if orig_case else text):
                people.append(name)

        # Person extraction — title patterns
        for pat in PERSON_TITLES:
            for match in re.finditer(pat, text_orig):
                name = match.group(2).strip()
                if len(name) > 3 and name not in people:
                    people.append(name)

        # Ticker extraction (cashtags + uppercase 2-5 chars, minus common words)
        tickers = _extract_tickers(text_orig)

        # Deduplicate (first match wins, so a country keeps its canonical name —
        # "China", not the last alias in the table such as "Shanghai") and count
        # each entity once per article.
        by_code = {}
        for c in countries:
            by_code.setdefault(c["code"], c)
        countries = list(by_code.values())[:5]
        orgs = list(dict.fromkeys(orgs))[:5]
        people = list(dict.fromkeys(people))[:5]
        tickers = list(dict.fromkeys(tickers))[:5]
        for c in countries:
            all_countries[c["code"]] += 1
        for o in orgs:
            all_orgs[o] += 1
        for p in people:
            all_people[p] += 1
        for t in tickers:
            all_tickers[t] += 1

        per_article.append({
            "id": article.get("id", ""),
            "countries": countries,
            "organizations": orgs,
            "people": people,
            "tickers": tickers,
        })

    return {
        "success": True,
        "entities": per_article,
        "top_countries": [{"code": c, "count": n} for c, n in all_countries.most_common(10)],
        "top_organizations": [{"name": o, "count": n} for o, n in all_orgs.most_common(10)],
        "top_people": [{"name": p, "count": n} for p, n in all_people.most_common(10)],
        "top_tickers": [{"symbol": t, "count": n} for t, n in all_tickers.most_common(10)],
    }


def enrich_batch(articles_json):
    """NER + geolocation for a batch of articles in ONE Python process.

    Only per-article results are returned: the terminal caches them by article id
    and sends just the articles it has not enriched yet, so batch aggregates (top
    countries / organisations / people) are computed on the C++ side over whatever
    subset is on screen. Articles without a recognisable place are simply absent
    from ``geolocated_articles``.
    """
    try:
        articles = json.loads(articles_json) if isinstance(articles_json, str) else articles_json
    except json.JSONDecodeError:
        return {"success": False, "error": "Invalid JSON"}
    if not isinstance(articles, list):
        return {"success": False, "error": "Expected a JSON array of articles"}

    ner = extract_entities(articles)
    if not ner.get("success"):
        return ner

    try:
        import news_geolocation  # sibling script in the same directory
        geo = news_geolocation.extract_and_geocode(articles)
    except Exception as exc:  # report, never traceback onto stdout
        return {"success": False, "error": f"geolocation failed: {type(exc).__name__}: {exc}"}
    if not geo.get("success"):
        return {"success": False, "error": geo.get("error", "geolocation failed")}

    return {
        "success": True,
        "entities": ner["entities"],
        "geolocated_articles": geo["geolocated_articles"],
        "count": len(articles),
    }


# Finance keyword lexicon — doubles as the VADER lexicon nudge and the offline
# fallback scorer. Weights are valences on VADER's [-4, 4] scale.
_FINANCE_POSITIVE = {
    "surge": 3.0, "soar": 3.2, "skyrocket": 3.5, "breakthrough": 3.0, "boom": 2.8,
    "rally": 2.5, "gain": 2.0, "gains": 2.0, "rise": 1.8, "jump": 2.5,
    "jumps": 2.5, "climb": 2.0, "rebound": 2.2, "boost": 2.0, "beat": 2.2,
    "beats": 2.2, "exceed": 2.0, "outperform": 2.5, "upgrade": 2.5, "profit": 1.8,
    "growth": 1.8, "recover": 2.0, "record": 1.5, "strong": 1.5, "robust": 1.5,
    "bullish": 3.0, "optimism": 1.8, "milestone": 1.5, "approval": 1.8, "deal": 1.2,
}
_FINANCE_NEGATIVE = {
    "crash": -3.5, "plunge": -3.2, "collapse": -3.5, "meltdown": -3.5, "bankruptcy": -3.8,
    "fall": -1.8, "drop": -2.0, "decline": -2.0, "tumble": -2.5, "slump": -2.5,
    "miss": -2.0, "misses": -2.0, "fail": -2.2, "recession": -2.8, "crisis": -2.5,
    "sanction": -2.0, "tariff": -1.8, "escalat": -1.5, "layoff": -2.2, "layoffs": -2.2,
    "downgrade": -2.5, "fraud": -3.5, "scandal": -2.8, "selloff": -2.5, "default": -2.5,
    "lawsuit": -1.8, "probe": -1.5, "weak": -1.5, "loss": -1.5, "deficit": -1.2,
    "fear": -1.5, "threat": -1.5, "warning": -1.5, "bearish": -3.0, "halt": -1.5,
}

# Integer-weighted view used by the keyword fallback scorer (and confidence).
_KW_POSITIVE = {k: max(1, int(round(abs(v) / 1.6))) for k, v in _FINANCE_POSITIVE.items()}
_KW_NEGATIVE = {k: max(1, int(round(abs(v) / 1.6))) for k, v in _FINANCE_NEGATIVE.items()}

_VADER = None
_VADER_TRIED = False


def _get_vader():
    """Lazily build a finance-tuned VADER analyzer; None if unavailable."""
    global _VADER, _VADER_TRIED
    if _VADER_TRIED:
        return _VADER
    _VADER_TRIED = True
    try:
        from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
        analyzer = SentimentIntensityAnalyzer()
        analyzer.lexicon.update(_FINANCE_POSITIVE)
        analyzer.lexicon.update(_FINANCE_NEGATIVE)
        _VADER = analyzer
    except Exception:
        _VADER = None
    return _VADER


def _keyword_signals(text):
    """Finance keyword hit counts (used for fallback scoring and confidence)."""
    pos = sum(w for p, w in _KW_POSITIVE.items() if p in text)
    neg = sum(w for p, w in _KW_NEGATIVE.items() if p in text)
    return pos, neg


def _label_from_score(score):
    if score >= 0.15:
        return "BULLISH"
    if score <= -0.15:
        return "BEARISH"
    return "NEUTRAL"


def analyze_sentiment_batch(headlines_json):
    """Batch sentiment analysis with confidence scores.

    Prefers VADER (finance-tuned) when ``vaderSentiment`` is installed; otherwise
    falls back to the offline keyword scorer. Output schema is stable across both
    engines; ``engine`` reports which path ran."""
    try:
        articles = json.loads(headlines_json) if isinstance(headlines_json, str) else headlines_json
    except json.JSONDecodeError:
        return {"success": False, "error": "Invalid JSON"}

    analyzer = _get_vader()
    engine = "vader" if analyzer else "lexicon"

    results = []
    for article in articles:
        text = (article.get("headline", "") + " " + article.get("summary", "")).lower()
        pos_score, neg_score = _keyword_signals(text)
        total = pos_score + neg_score

        if analyzer is not None:
            score = analyzer.polarity_scores(text)["compound"]  # -1..1
            sentiment = _label_from_score(score)
            if sentiment == "NEUTRAL":
                confidence = round(min(0.2 + abs(score) * 0.8, 0.4), 3)
            else:
                confidence = round(min(0.45 + abs(score) * 0.45 + total * 0.02, 0.97), 3)
        else:
            net = pos_score - neg_score
            if total == 0:
                sentiment, score, confidence = "NEUTRAL", 0.0, 0.2
            elif net >= 2:
                sentiment = "BULLISH"
                score = min(net / max(total, 1), 1.0)
                confidence = min(0.4 + total * 0.05, 0.95)
            elif net <= -2:
                sentiment = "BEARISH"
                score = max(net / max(total, 1), -1.0)
                confidence = min(0.4 + total * 0.05, 0.95)
            else:
                sentiment = "NEUTRAL"
                score = net / max(total, 1)
                confidence = 0.3

        results.append({
            "id": article.get("id", ""),
            "sentiment": sentiment,
            "score": round(score, 3),
            "confidence": round(confidence, 3),
            "positive_signals": pos_score,
            "negative_signals": neg_score,
        })

    # Aggregate
    bull = sum(1 for r in results if r["sentiment"] == "BULLISH")
    bear = sum(1 for r in results if r["sentiment"] == "BEARISH")
    neut = sum(1 for r in results if r["sentiment"] == "NEUTRAL")

    return {
        "success": True,
        "engine": engine,
        "results": results,
        "aggregate": {"bullish": bull, "bearish": bear, "neutral": neut},
        "overall_score": round(sum(r["score"] for r in results) / max(len(results), 1), 3),
    }


def resolve_arg(arg):
    """If arg starts with '@', read content from that file path and delete it."""
    if arg and arg.startswith("@"):
        path = arg[1:]
        try:
            with open(path, "r", encoding="utf-8") as f:
                data = f.read()
            try:
                import os
                os.remove(path)
            except Exception:
                pass
            return data
        except Exception as e:
            return arg  # fallback: return as-is
    return arg


def main(args=None):
    if args is None:
        args = sys.argv[1:]

    if len(args) < 2:
        print(json.dumps({"success": False, "error": "Usage: news_nlp.py <command> <json_data>"}))
        return

    command = args[0]
    data = resolve_arg(args[1])

    try:
        if command == "extract_entities":
            result = extract_entities(data)
        elif command == "enrich_batch":
            result = enrich_batch(data)
        elif command == "analyze_sentiment_batch":
            result = analyze_sentiment_batch(data)
        else:
            result = {"success": False, "error": f"Unknown command: {command}"}
    except Exception as exc:  # malformed payload (e.g. not an array) — report, don't traceback
        result = {"success": False, "error": f"{type(exc).__name__}: {exc}"}

    print(json.dumps(result))


if __name__ == "__main__":
    main()