File size: 1,007 Bytes
a657a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# load dataset
import pandas as pd
df = pd.read_csv('sentiment_data.csv')
texts = df['text'].astype(str)
# load NER model
import spacy
model = spacy.load('en_core_web_lg')
# Extract entities
result = []
for t in texts:
    doc = model(t)
    entities = [(ent.text, ent.label_) for ent in doc.ents]
    result.append({'Text':t,'Entity':entities})
result_df = pd.DataFrame(result)
# Entities visualization
from spacy import displacy

for t in texts:
    doc = model(t)
    displacy.render(doc,style='ent')
# Count Entities
from collections import Counter
all_entities = [ent for ents in result_df['Entity'] for ent in ents]
labels = [label for text,label in all_entities]
Counter(labels).most_common()
# Extract entities from input
def ext_ent(sentence):
    doc = model(sentence)
    output = ''
    for ent in doc.ents:
        output += f"{ent.text} - {ent.label_}\n"
    return output
import gradio as gr

demo = gr.Interface(fn=ext_ent,inputs='text',outputs='text',title="Extract Entities")
demo.launch()