Skip to content

Commit

Permalink
added support to save viz
Browse files Browse the repository at this point in the history
  • Loading branch information
HashamUlHaq committed Feb 2, 2022
1 parent 9431475 commit 6676b9f
Show file tree
Hide file tree
Showing 18 changed files with 263 additions and 189 deletions.
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ displayHTML(vis_html)

### Jupyter

To save the visualization as html, provide the export file path: `save_path='./export.html'` for each visualizer.


#### Dependency Parser
```python
from sparknlp_display import DependencyParserVisualizer
Expand All @@ -64,7 +67,8 @@ pipeline_result = dp_pipeline.fullAnnotate(text)
dependency_vis.display(pipeline_result[0], #should be the results of a single example, not the complete dataframe.
pos_col = 'pos', #specify the pos column
dependency_col = 'dependency', #specify the dependency column
dependency_type_col = 'dependency_type' #specify the dependency type column
dependency_type_col = 'dependency_type', #specify the dependency type column
save_path='./export.html' # optional - to save viz as html. (default: None)
)
```

Expand All @@ -82,8 +86,9 @@ pipeline_result = ner_light_pipeline.fullAnnotate(text)

ner_vis.display(pipeline_result[0], #should be the results of a single example, not the complete dataframe
label_col='entities', #specify the entity column
document_col='document' #specify the document column (default: 'document')
labels=['PER'] #only allow these labels to be displayed. (default: [] - all labels will be displayed)
document_col='document', #specify the document column (default: 'document')
labels=['PER'], #only allow these labels to be displayed. (default: [] - all labels will be displayed)
save_path='./export.html' # optional - to save viz as html. (default: None)
)

## To set custom label colors:
Expand All @@ -104,8 +109,9 @@ pipeline_result = er_light_pipeline.fullAnnotate(text)

er_vis.display(pipeline_result[0], #should be the results of a single example, not the complete dataframe
label_col='entities', #specify the ner result column
resolution_col = 'resolution'
document_col='document' #specify the document column (default: 'document')
resolution_col = 'resolution',
document_col='document', #specify the document column (default: 'document')
save_path='./export.html' # optional - to save viz as html. (default: None)
)

## To set custom label colors:
Expand All @@ -126,7 +132,8 @@ pipeline_result = re_light_pipeline.fullAnnotate(text)
re_vis.display(pipeline_result[0], #should be the results of a single example, not the complete dataframe
relation_col = 'relations', #specify relations column
document_col = 'document', #specify document column
show_relations=True #display relation names on arrows (default: True)
show_relations=True, #display relation names on arrows (default: True)
save_path='./export.html' # optional - to save viz as html. (default: None)
)

```
Expand All @@ -143,8 +150,9 @@ pipeline_result = ner_assertion_light_pipeline.fullAnnotate(text)

assertion_vis.display(pipeline_result[0],
label_col = 'entities', #specify the ner result column
assertion_col = 'assertion' #specify assertion column
document_col = 'document' #specify the document column (default: 'document')
assertion_col = 'assertion', #specify assertion column
document_col = 'document', #specify the document column (default: 'document')
save_path='./export.html' # optional - to save viz as html. (default: None)
)

## To set custom label colors:
Expand Down
2 changes: 1 addition & 1 deletion build/lib/sparknlp_display/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.8
1.9
11 changes: 8 additions & 3 deletions build/lib/sparknlp_display/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __display_ner(self, result, label_col, resolution_col, document_col, origina

return html_output

def display(self, result, label_col, assertion_col, document_col='document', raw_text=None, return_html=False):
def display(self, result, label_col, assertion_col, document_col='document', raw_text=None, return_html=False, save_path=None):
"""Displays Assertion visualization.
Inputs:
Expand All @@ -174,9 +174,14 @@ def display(self, result, label_col, assertion_col, document_col='document', raw
#self.__verifyInput(result, label_col, document_col, raw_text)

html_content = self.__display_ner(result, label_col, assertion_col, document_col, raw_text)
html_content_save = style_config.STYLE_CONFIG_ENTITIES+ " "+html_content

if save_path != None:
with open(save_path, 'w') as f_:
f_.write(html_content_save)

if return_html:
return style_config.STYLE_CONFIG_ENTITIES+ " "+html_content
return html_content_save
else:
return display(HTML(style_config.STYLE_CONFIG_ENTITIES+ " "+html_content))
return display(HTML(html_content_save))

7 changes: 6 additions & 1 deletion build/lib/sparknlp_display/dependency_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def __generate_graph(self, result_df):
return dwg.tostring()


def display(self, res, pos_col, dependency_col, dependency_type_col=None, return_html=False):
def display(self, res, pos_col, dependency_col, dependency_type_col=None, return_html=False, save_path=None):
"""Displays NER visualization.
Inputs:
Expand Down Expand Up @@ -254,6 +254,11 @@ def display(self, res, pos_col, dependency_col, dependency_type_col=None, return
df['dependency_type'] = ''

html_content = self.__generate_graph(df)

if save_path != None:
with open(save_path, 'w') as f_:
f_.write(html_content)

if return_html:
return html_content
else:
Expand Down
12 changes: 9 additions & 3 deletions build/lib/sparknlp_display/entity_resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def __display_ner(self, result, label_col, resolution_col, document_col, origina

return html_output

def display(self, result, label_col, resolution_col, document_col='document', raw_text=None, return_html=False):
def display(self, result, label_col, resolution_col, document_col='document', raw_text=None, return_html=False, save_path=None):
"""Displays NER visualization.
Inputs:
Expand All @@ -175,7 +175,13 @@ def display(self, result, label_col, resolution_col, document_col='document', ra

html_content = self.__display_ner(result, label_col, resolution_col, document_col, raw_text)

html_content_save = style_config.STYLE_CONFIG_ENTITIES+ " "+html_content

if save_path != None:
with open(save_path, 'w') as f_:
f_.write(html_content_save)

if return_html:
return style_config.STYLE_CONFIG_ENTITIES+ " "+html_content
return
else:
return display(HTML(style_config.STYLE_CONFIG_ENTITIES+ " "+html_content))
return display(HTML(html_content_save))
13 changes: 10 additions & 3 deletions build/lib/sparknlp_display/ner.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def __display_ner(self, result, label_col, document_col, original_text, labels_l

return html_output

def display(self, result, label_col, document_col='document', raw_text=None, labels=None, return_html=False):
def display(self, result, label_col, document_col='document', raw_text=None, labels=None, return_html=False, save_path=None):
"""Displays NER visualization.
Inputs:
result -- A Dataframe or dictionary.
Expand All @@ -153,8 +153,15 @@ def display(self, result, label_col, document_col='document', raw_text=None, lab

html_content = self.__display_ner(result, label_col, document_col, raw_text, labels)

html_content_save = style_config.STYLE_CONFIG_ENTITIES+ " "+html_content

if save_path != None:
with open(save_path, 'w') as f_:
f_.write(html_content_save)

if return_html:
return style_config.STYLE_CONFIG_ENTITIES+ " "+html_content
return html_content_save
else:
return display(HTML(style_config.STYLE_CONFIG_ENTITIES+ " "+html_content))
return display(HTML(html_content_save))


6 changes: 5 additions & 1 deletion build/lib/sparknlp_display/relation_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def __gen_graph(self, rdf, selected_text, exclude_relations, show_relations):

return dwg.tostring()

def display(self, result, relation_col, document_col='document', exclude_relations=['O'], show_relations=True, return_html=False):
def display(self, result, relation_col, document_col='document', exclude_relations=['O'], show_relations=True, return_html=False, save_path=None):
"""Displays Relation Extraction visualization.
Inputs:
result -- A Dataframe or dictionary.
Expand All @@ -392,6 +392,10 @@ def display(self, result, relation_col, document_col='document', exclude_relatio

html_content = self.__gen_graph(res, original_text, exclude_relations, show_relations)

if save_path != None:
with open(save_path, 'w') as f_:
f_.write(html_content)

if return_html:
return html_content
else:
Expand Down
Binary file removed dist/spark-nlp-display-1.8.tar.gz
Binary file not shown.
Binary file added dist/spark-nlp-display-1.9.tar.gz
Binary file not shown.
Binary file not shown.
Loading

0 comments on commit 6676b9f

Please sign in to comment.