Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# load dataset
|
| 2 |
+
import pandas as pd
|
| 3 |
+
df = pd.read_csv('sentiment_data.csv')
|
| 4 |
+
texts = df['text'].astype(str)
|
| 5 |
+
# Load NER model
|
| 6 |
+
import spacy
|
| 7 |
+
model = spacy.load('en_core_web_lg')
|
| 8 |
+
# Extract entities
|
| 9 |
+
result = []
|
| 10 |
+
for i in texts:
|
| 11 |
+
doc = model(i)
|
| 12 |
+
entities = [(ent.text,ent.label_) for ent in doc.ents]
|
| 13 |
+
result.append({'Text':i,'Entity':entities})
|
| 14 |
+
result_df = pd.DataFrame(result)
|
| 15 |
+
result_df.to_csv('Ben.csv',index=0)
|
| 16 |
+
# Entity visualization
|
| 17 |
+
from spacy import displacy
|
| 18 |
+
for t in texts[:5]:
|
| 19 |
+
doc = model(t)
|
| 20 |
+
displacy.render(doc,style='ent')
|
| 21 |
+
from collections import Counter
|
| 22 |
+
all_entites = [ent for ents in result_df['Entity'] for ent in ents]
|
| 23 |
+
print(all_entites)
|
| 24 |
+
labels = [label for text, label in all_entites]
|
| 25 |
+
Counter(labels).most_common(1)
|
| 26 |
+
def ext_ent(sentence):
|
| 27 |
+
doc = model(sentence)
|
| 28 |
+
output = ''
|
| 29 |
+
for i in doc.ents:
|
| 30 |
+
output += f'{i.text} - {i.label_}\n'
|
| 31 |
+
return output
|
| 32 |
+
import gradio as gr
|
| 33 |
+
|
| 34 |
+
demo = gr.Interface(fn=ext_ent,inputs='text',outputs='text',title='Extract Entities')
|
| 35 |
+
demo.launch()
|