File size: 1,366 Bytes
d8b68f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import spacy
from spacy import displacy
import json

    
# Initialize spaCy
nlp = spacy.load("en_core_web_md")

# Define sample data
data = {
    "fruit": ["apple", "pear", "orange"],
    "vegetable": ["broccoli", "spinach", "tomato"],
    "meat": ['beef', 'pork', 'turkey', 'duck']
}

# Streamlit app
st.title('Named Entity Recognition with spaCy')

user_input = st.text_area("Enter text:", "")

if st.button("Process"):
    if user_input:
        # Process the text
        doc = nlp(user_input)
        
        # Visualization options
        options = {
            "colors": {"fruit": "darkorange", "vegetable": "limegreen", "meat": "salmon"},
            "ents": ["fruit", "vegetable", "meat"],
        }

        # JSON serialization with only entity and type
        result_dict = {'entities': []}
        
        for ent in doc.ents:
            ent_data = {
                'entity': ent.text,
                'type': ent.label_
            }
            result_dict['entities'].append(ent_data)
        
        result_json = json.dumps(result_dict, indent=4)

        # Display results
        st.subheader("Named Entities")
        html = displacy.render(doc, style="ent", page=True, minify=True)
        st.write(html, unsafe_allow_html=True)
        st.subheader("Entities in JSON format")
        st.json(result_json)