-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_parser.py
More file actions
173 lines (155 loc) · 9.56 KB
/
text_parser.py
File metadata and controls
173 lines (155 loc) · 9.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
"""
text_parser.py - NLP Parser for extracting features from natural language input
Renamed from parser.py to avoid shadowing Python's stdlib `parser` module,
which caused ImportError in Python 3.9+ where the stdlib module was removed
and confused imports in earlier versions.
"""
import spacy
from dataclasses import dataclass, field
from typing import List, Dict, Any, Optional
# Keyword maps for heuristic enrichment
BELIEF_KEYWORDS = {"belief", "faith", "religion", "indoctrination", "upbringing", "schooling"}
IDENTITY_KEYWORDS = {"self", "identity", "perception", "fragmented", "stable", "awareness"}
MORALITY_KEYWORDS = {"justice", "fairness", "forgiveness", "dilemma", "moral", "immoral", "law"}
PHILOSOPHY_KEYWORDS = {"yin", "yang", "wu", "wei", "dao", "paradox", "harmony", "goddess", "cosmic"}
PSYCHOLOGY_KEYWORDS = {"fear", "anxiety", "impulse", "coping", "resilience", "stress", "support",
"confidence", "desire", "impress", "validation", "status"}
SOCIAL_KEYWORDS = {"empathy", "trust", "group", "hierarchy", "validation", "status",
"conformity", "social", "proof"}
PHYSICS_KEYWORDS = {"spin", "gravity", "force", "mass", "velocity", "acceleration",
"momentum", "energy", "field", "rotation", "torque"}
MATH_KEYWORDS = {"proof", "optimize", "minimize", "probability", "equation", "function",
"derivative", "integral", "sum"}
# Expanded domains
TECHNOLOGY_KEYWORDS = {"algorithm", "data", "network", "cloud", "AI", "machine", "learning",
"automation", "robotics", "cybersecurity"}
BIOLOGY_KEYWORDS = {"cell", "gene", "DNA", "RNA", "protein", "enzyme", "mutation",
"evolution", "species", "ecosystem"}
CHEMISTRY_KEYWORDS = {"atom", "molecule", "reaction", "bond", "compound", "acid",
"base", "catalyst", "solution", "oxidation"}
MEDICINE_KEYWORDS = {"diagnosis", "therapy", "surgery", "vaccine", "infection", "immune",
"symptom", "treatment", "rehabilitation", "prevention"}
NEUROSCIENCE_KEYWORDS = {"neuron", "synapse", "cortex", "dopamine", "memory", "plasticity",
"signal", "brain", "consciousness", "cognition"}
LINGUISTICS_KEYWORDS = {"syntax", "semantics", "phonetics", "morphology", "grammar",
"dialect", "language", "translation", "discourse", "pragmatics"}
ART_KEYWORDS = {"painting", "sculpture", "music", "dance", "literature", "poetry",
"theater", "cinema", "design", "aesthetics"}
HISTORY_KEYWORDS = {"ancient", "medieval", "renaissance", "revolution", "empire",
"colonial", "industrial", "modern", "war", "civilization"}
GEOGRAPHY_KEYWORDS = {"continent", "country", "city", "mountain", "river", "climate",
"map", "region", "territory", "landscape"}
POLITICS_KEYWORDS = {"government", "policy", "democracy", "dictatorship", "election",
"constitution", "rights", "freedom", "authority", "power"}
ECONOMICS_KEYWORDS = {"market", "trade", "currency", "inflation", "investment",
"capital", "labor", "production", "consumption", "growth"}
BUSINESS_KEYWORDS = {"management", "strategy", "leadership", "innovation", "startup",
"entrepreneurship", "finance", "marketing", "sales", "operations"}
EDUCATION_KEYWORDS = {"learning", "teaching", "curriculum", "assessment", "student",
"teacher", "school", "university", "knowledge", "pedagogy"}
ENVIRONMENT_KEYWORDS = {"climate", "sustainability", "pollution", "conservation",
"biodiversity", "renewable", "ecosystem", "carbon", "green", "recycling"}
LAW_KEYWORDS = {"contract", "court", "judge", "jury", "legislation", "regulation",
"crime", "justice", "rights", "liability"}
ETHICS_KEYWORDS = {"virtue", "duty", "responsibility", "honesty", "integrity",
"fairness", "respect", "values", "principles", "morality"}
MYTHOLOGY_KEYWORDS = {"hero", "legend", "myth", "god", "goddess", "pantheon",
"ritual", "sacrifice", "prophecy", "symbol"}
ASTRONOMY_KEYWORDS = {"planet", "star", "galaxy", "universe", "orbit", "cosmos",
"blackhole", "nebula", "comet", "asteroid"}
ENGINEERING_KEYWORDS = {"design", "structure", "mechanical", "electrical", "civil",
"aerospace", "chemical", "software", "system", "process"}
SPORTS_KEYWORDS = {"team", "player", "coach", "game", "match", "tournament",
"score", "goal", "victory", "competition"}
MUSIC_KEYWORDS = {"melody", "harmony", "rhythm", "tempo", "instrument",
"composition", "performance", "song", "genre", "lyrics"}
FOOD_KEYWORDS = {"cuisine", "recipe", "ingredient", "dish", "meal",
"flavor", "taste", "nutrition", "diet", "cooking"}
TRAVEL_KEYWORDS = {"journey", "trip", "destination", "adventure", "exploration",
"tourism", "vacation", "map", "guide", "culture"}
PSYCHOTHERAPY_KEYWORDS = {"counseling", "therapy", "psychoanalysis", "CBT",
"mindfulness", "support", "healing", "trauma", "growth", "intervention"}
AI_KEYWORDS = {"neural", "deep", "learning", "model", "training", "dataset",
"inference", "optimization", "pattern", "recognition"}
SECURITY_KEYWORDS = {"vulnerability", "exploit", "attack", "defense", "firewall",
"encryption", "authentication", "authorization", "malware", "phishing"}
@dataclass
class ParsedFeatures:
"""Structured representation of parsed text features"""
actors: List[str] = field(default_factory=list)
objects: List[str] = field(default_factory=list)
actions: List[str] = field(default_factory=list)
relations: List[str] = field(default_factory=list)
intents: List[str] = field(default_factory=list)
conditions: List[str] = field(default_factory=list)
environment: Dict[str, Any] = field(default_factory=dict)
uncertainty: float = 0.0
# Extended enrichers
beliefs: List[str] = field(default_factory=list)
identity_signals: List[str] = field(default_factory=list)
morality_signals: List[str] = field(default_factory=list)
philosophy_signals: List[str] = field(default_factory=list)
psychology_signals: List[str] = field(default_factory=list)
social_signals: List[str] = field(default_factory=list)
physics_signals: List[str] = field(default_factory=list)
math_signals: List[str] = field(default_factory=list)
raw_text: str = ""
class TextParser:
"""Parser using spaCy for NLP analysis"""
def __init__(self, model_name: str = "en_core_web_sm"):
"""Initialize parser with spaCy model"""
try:
self.nlp = spacy.load(model_name)
except OSError:
print(f"spaCy model '{model_name}' not found. Downloading...")
import subprocess
subprocess.run(["python", "-m", "spacy", "download", model_name], check=True)
self.nlp = spacy.load(model_name)
def parse(self, text: str) -> ParsedFeatures:
"""Parse text and extract structured features"""
doc = self.nlp(text)
# Basic extraction
actors = [ent.text for ent in doc.ents if ent.label_ in ("PERSON", "ORG")]
objects = [chunk.text for chunk in doc.noun_chunks]
actions = [token.lemma_ for token in doc if token.pos_ == "VERB"]
relations = [f"{tok.head.text} → {tok.text}" for tok in doc if tok.dep_ in ("nsubj", "dobj")]
conditions = [tok.text for tok in doc if tok.dep_ == "advcl"]
intents = [tok.text for tok in doc if tok.text.lower() in ("want", "need", "should", "must")]
# Heuristic enrichers — compare lowercased token text against lowercase keyword sets
beliefs = [tok.text for tok in doc if tok.text.lower() in BELIEF_KEYWORDS]
identity_signals = [tok.text for tok in doc if tok.text.lower() in IDENTITY_KEYWORDS]
morality_signals = [tok.text for tok in doc if tok.text.lower() in MORALITY_KEYWORDS]
philosophy_signals = [tok.text for tok in doc if tok.text.lower() in PHILOSOPHY_KEYWORDS]
psychology_signals = [tok.text for tok in doc if tok.text.lower() in PSYCHOLOGY_KEYWORDS]
social_signals = [tok.text for tok in doc if tok.text.lower() in SOCIAL_KEYWORDS]
physics_signals = [tok.text for tok in doc if tok.text.lower() in PHYSICS_KEYWORDS]
math_signals = [tok.text for tok in doc if tok.text.lower() in MATH_KEYWORDS]
# Uncertainty heuristic: modal verbs relative to sentence length
# FIX: original included ALL verbs (VB tag), inflating the count falsely.
# Only true modals (MD tag) contribute to uncertainty.
modal_count = sum(1 for tok in doc if tok.tag_ == "MD")
uncertainty = min(modal_count / max(len(doc), 1) * 5, 1.0)
return ParsedFeatures(
actors=actors,
objects=objects,
actions=actions,
relations=relations,
intents=intents,
conditions=conditions,
environment={},
uncertainty=uncertainty,
beliefs=beliefs,
identity_signals=identity_signals,
morality_signals=morality_signals,
philosophy_signals=philosophy_signals,
psychology_signals=psychology_signals,
social_signals=social_signals,
physics_signals=physics_signals,
math_signals=math_signals,
raw_text=text,
)
if __name__ == "__main__":
parser = TextParser()
sample = "The goddess Themis weighs justice and forgiveness, while fear and anxiety test identity."
parsed = parser.parse(sample)
print(parsed)