LinkLinkWu commited on
Commit
7c727fa
·
verified ·
1 Parent(s): d25b499

Update func.py

Browse files
Files changed (1) hide show
  1. func.py +31 -0
func.py CHANGED
@@ -114,6 +114,37 @@ def analyze_sentiment(
114
  except Exception:
115
  return "Unknown", 0.0
116
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
  # ---------------------------------------------------------------------------
118
  # Aggregation logic – turning many headlines into one overall label
119
  # ---------------------------------------------------------------------------
 
114
  except Exception:
115
  return "Unknown", 0.0
116
 
117
+ # ---------------------------------------------------------------------------
118
+ # Organisation‑entity extraction helper (kept for backward compatibility)
119
+ # ---------------------------------------------------------------------------
120
+
121
+ def extract_org_entities(
122
+ text: str,
123
+ pipe=None,
124
+ max_entities: int = 5,
125
+ ) -> List[str]:
126
+ """Extract up to *max_entities* unique organisation tokens from *text*.
127
+
128
+ Uses the pre‑initialised NER pipeline unless an alternative *pipe* is
129
+ supplied. Tokens are upper‑cased and de‑hashed ("##") to make them ticker‑
130
+ friendly. The function is side‑effect free and falls back to an empty list
131
+ on any exception.
132
+ """
133
+ try:
134
+ ner_pipe = pipe or ner_pipeline
135
+ entities = ner_pipe(text)
136
+ orgs: List[str] = []
137
+ for ent in entities:
138
+ if ent.get("entity_group") == "ORG":
139
+ token = ent["word"].replace("##", "").strip().upper()
140
+ if token and token not in orgs:
141
+ orgs.append(token)
142
+ if len(orgs) >= max_entities:
143
+ break
144
+ return orgs
145
+ except Exception:
146
+ return []
147
+
148
  # ---------------------------------------------------------------------------
149
  # Aggregation logic – turning many headlines into one overall label
150
  # ---------------------------------------------------------------------------