nlp-insight-engine / utils /helpers.py
wasifch2
Initial commit: NLP Insight Engine
f72e9b7
Raw
History Blame Contribute Delete
5.38 kB
"""
Helper utilities for the NLP Insight Engine.
"""
from collections import Counter
# ── Entity display helpers ───────────────────────────────────────────────────
ENTITY_COLORS = {
"PER": "entity-PER",
"ORG": "entity-ORG",
"LOC": "entity-LOC",
"MISC": "entity-MISC",
"DATE": "entity-DATE",
}
def format_entities(entities: list[dict]) -> str:
"""Render entities as styled HTML tags."""
tags = []
for ent in entities:
css_class = ENTITY_COLORS.get(ent["entity_group"], "entity-default")
tags.append(
f'<span class="entity-tag {css_class}">'
f'{ent["word"]} '
f'<small>({ent["entity_group"]})</small>'
f"</span>"
)
return " ".join(tags)
def build_entity_summary(entities: list[dict]) -> dict:
"""Count entities by type for pie chart."""
return dict(Counter(e["entity_group"] for e in entities))
# ── File helpers ─────────────────────────────────────────────────────────────
def extract_text_from_file(uploaded_file) -> str:
"""Extract text from uploaded .txt or .csv files."""
name = uploaded_file.name.lower()
try:
if name.endswith(".txt"):
return uploaded_file.read().decode("utf-8", errors="replace")
elif name.endswith(".csv"):
import pandas as pd
df = pd.read_csv(uploaded_file)
# Concatenate all string columns
text_cols = df.select_dtypes(include="object")
return " ".join(text_cols.astype(str).values.flatten())
else:
return ""
except Exception as e:
return f"Error reading file: {e}"
# ── Example texts ────────────────────────────────────────────────────────────
EXAMPLE_TEXTS = {
"πŸ“° Tech News Article": (
"Artificial intelligence is transforming industries at an unprecedented pace. "
"Companies like Google, Microsoft, and OpenAI are investing billions in developing "
"large language models that can understand and generate human-like text. Meanwhile, "
"the European Union has introduced the AI Act, the world's first comprehensive "
"regulation framework for artificial intelligence. Critics argue the regulations "
"could stifle innovation in Europe, while supporters believe they are necessary to "
"protect citizens from potential harms. The debate continues as AI capabilities "
"advance rapidly, with new breakthroughs in multimodal understanding, reasoning, "
"and code generation emerging every month."
),
"⭐ Product Review": (
"I purchased this laptop three weeks ago and I'm extremely disappointed. The build "
"quality feels cheap despite the premium price tag. The keyboard is mushy, the "
"trackpad is unresponsive half the time, and the battery barely lasts four hours. "
"Customer support was unhelpful β€” they kept transferring me between departments. "
"The only positive is the screen quality, which is genuinely excellent. However, "
"a good display doesn't make up for all the other issues. I would not recommend "
"this product to anyone. Save your money and look at alternatives from Dell or "
"Lenovo instead."
),
"🏒 Business Report": (
"The quarterly earnings report for Acme Corporation shows a 15 percent increase "
"in revenue compared to the same period last year. The company's expansion into "
"the Asian market, particularly Japan and South Korea, has been a major growth "
"driver. CEO Sarah Johnson noted that the new product line launched in March "
"exceeded expectations, generating over 50 million dollars in its first quarter. "
"The board of directors approved an additional investment of 200 million dollars "
"in research and development for 2025. Chief Financial Officer Michael Chen "
"highlighted that operating margins improved by 3.2 percentage points, reaching "
"22.8 percent. The company plans to open new offices in London and Singapore by "
"the end of the fiscal year."
),
"🌍 Climate Report": (
"The latest report from the United Nations Environment Programme warns that global "
"temperatures could rise by 2.8 degrees Celsius above pre-industrial levels by "
"the end of the century without significant policy changes. Dr. Maria Rodriguez, "
"lead author from the University of Oxford, stated that immediate action is needed "
"across all sectors. The energy transition is accelerating in some regions, with "
"renewable energy capacity growing by 50 percent in 2024 according to the "
"International Energy Agency. However, fossil fuel consumption continues to rise "
"in developing nations. The report recommends tripling investment in clean energy "
"infrastructure and implementing carbon pricing mechanisms across all major "
"economies by 2030."
),
}