This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
generated from databricks-industry-solutions/industry-solutions-blueprints
-
Notifications
You must be signed in to change notification settings - Fork 1
/
06_Understanding_Entities_in_Context.py
232 lines (145 loc) · 6.64 KB
/
06_Understanding_Entities_in_Context.py
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Databricks notebook source
# MAGIC %md You may find this series of notebooks at https://github.com/databricks-industry-solutions/jsl-financial-nlp
# COMMAND ----------
# MAGIC %md
# MAGIC # Preinstallation
# MAGIC `johnsnowlabs` should come installed in your cluster. Just in case it is not, we install it.
# MAGIC We also install visualization libraries for rendering the graph.
# COMMAND ----------
# MAGIC %pip install networkx==2.5 decorator==5.0.9 plotly==5.1.0
# COMMAND ----------
# MAGIC %md
# MAGIC # Understanding the context of mentioned companies to identify COMPETITORS
# MAGIC Many Companies may be mentioned in the report. Most of them are just organizations in the ecosystem of the Cadence. Others, may be competitors.
# MAGIC
# MAGIC We can analyze the surrounding context of the extracted `ORG` to check if they are competitors or not.
# COMMAND ----------
# MAGIC %md
# MAGIC ## Let's resume the Graph creation, loading it from disk from previous step
# COMMAND ----------
from johnsnowlabs import nlp, finance, viz
import pickle
# COMMAND ----------
# MAGIC %run "./aux_visualization_functions"
# COMMAND ----------
# MAGIC %run "./aux_pipeline_functions"
# COMMAND ----------
generic_base_pipeline = get_generic_base_pipeline()
# COMMAND ----------
# load graph object from file
G = pickle.load(open('/databricks/driver/cadence.pickle', 'rb'))
# COMMAND ----------
from johnsnowlabs.nlp import LightPipeline
ner = finance.NerModel.pretrained("finner_orgs_prods_alias", "en", "finance/models")\
.setInputCols(["sentence", "token", "embeddings"]) \
.setOutputCol("ner")
ner_converter = nlp.NerConverter()\
.setInputCols(["sentence", "token", "ner"]) \
.setOutputCol("ner_chunk")\
.setWhiteList(['ORG', 'PRODUCT'])
assertion = finance.AssertionDLModel.pretrained("finassertion_competitors", "en", "finance/models")\
.setInputCols(["sentence", "ner_chunk", "embeddings"])\
.setOutputCol("assertion")
nlpPipeline = nlp.Pipeline(stages=[
generic_base_pipeline,
ner,
ner_converter,
assertion
])
empty_data = spark.createDataFrame([[""]]).toDF("text")
model = nlpPipeline.fit(empty_data)
light_model = LightPipeline(model)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Get Results
# COMMAND ----------
sample_text = ["""In the rapidly evolving market, certain elements of our application compete with Microsoft, Google, InFocus, Bluescape, Mersive, Barco, Nureva and Prysm. But, Oracle and IBM are out of our league."""]
chunks=[]
entities=[]
status=[]
light_result = light_model.fullAnnotate(sample_text)[0]
for n,m in zip(light_result['ner_chunk'],light_result['assertion']):
chunks.append(n.result)
entities.append(n.metadata['entity'])
status.append(m.result)
df = pd.DataFrame({'chunks':chunks, 'entities':entities, 'assertion':status})
# COMMAND ----------
# MAGIC %md
# MAGIC ### Visualize Assertion Result
# COMMAND ----------
vis = viz.AssertionVisualizer()
vis.set_label_colors({'COMPETITOR':'#008080', 'NO_COMPETITOR':'#800080'})
light_result = light_model.fullAnnotate(sample_text)[0]
displayHTML(vis.display(light_result, 'ner_chunk', 'assertion', return_html=True))
# COMMAND ----------
# MAGIC %md
# MAGIC ### Adding it to the graph
# COMMAND ----------
ORG = [x for x in G.nodes()][0]
ORG
# COMMAND ----------
for t in df.itertuples():
chunks = t.chunks
entities = t.entities
assertion = t.assertion
G.add_node(chunks, attr_dict={'entity': entities})
G.add_edge(ORG, chunks, attr_dict={'relation': 'is_' + assertion.lower()})
# COMMAND ----------
show_graph_in_plotly(G)
# COMMAND ----------
import pickle
# save graph object to file
pickle.dump(G, open('/databricks/driver/cadence.pickle', 'wb'))
# COMMAND ----------
# MAGIC %md
# MAGIC # Additional: Detecting Temporality and Certainty in Affirmations
# COMMAND ----------
# MAGIC %md
# MAGIC As an additional, extra step, let's explore the temporality and certainty of some entities using, again, Assertion Status.
# COMMAND ----------
ner_model_role = finance.NerModel.pretrained("finner_org_per_role_date", "en", "finance/models")\
.setInputCols(["sentence", "token", "embeddings"])\
.setOutputCol("ner_role")
ner_converter_role = nlp.NerConverter()\
.setInputCols(["sentence","token","ner_role"])\
.setOutputCol("ner_chunk")
assertion = finance.AssertionDLModel.pretrained("finassertion_time", "en", "finance/models")\
.setInputCols(["sentence", "ner_chunk", "embeddings"]) \
.setOutputCol("assertion")\
.setMaxSentLen(1200)
assertion_pipeline = nlp.Pipeline(stages=[
generic_base_pipeline,
ner_model_role,
ner_converter_role,
assertion
])
empty_data = spark.createDataFrame([[""]]).toDF("text")
assertion_model = assertion_pipeline.fit(empty_data)
light_model_assertion = LightPipeline(assertion_model)
# COMMAND ----------
# MAGIC %md
# MAGIC ### Get Result
# COMMAND ----------
sample_text = ["""Joseph Costello was the CEO of the company since founded in 1988 until 1997. He was followed by Lip-Bu Tan for the 2009–2021 period. Currently, Anirudh Devgan is the CEO since 2021""",
"""In 2007, Cadence was rumored to be in talks with Kohlberg Kravis Roberts and Blackstone Group regarding a possible sale of the company.""",
"""In 2008, Cadence withdrew a $1.6 billion offer to purchase rival Mentor Graphics.""",
""" The Cadence Giving Foundation will also support critical needs in areas such as diversity, equity and inclusion, environmental sustainability and STEM education.""",
"""This stand-alone, non-profit foundation will partner with other charitable initiatives to support critical needs in areas such as diversity, equity and inclusion, environmental sustainability and science, technology, engineering, and mathematics (“STEM”) education""",
"""Cadence employees could purchase common stock at a price equal to 85% of the lower of the fair market value at the beginning or the end of the applicable offering period"""]
chunks=[]
entities=[]
status=[]
light_results = light_model_assertion.fullAnnotate(sample_text)
for light_result in light_results:
for n,m in zip(light_result['ner_chunk'], light_result['assertion']):
chunks.append(n.result)
entities.append(n.metadata['entity'])
status.append(m.result)
df = pd.DataFrame({'chunks':chunks, 'entities':entities, 'assertion':status})
# COMMAND ----------
# MAGIC %md
# MAGIC ### Visualize Assertion Result
# COMMAND ----------
vis = viz.AssertionVisualizer()
for light_result in light_results:
displayHTML(vis.display(light_result, 'ner_chunk', 'assertion', return_html=True))