File size: 2,155 Bytes
0d77127
 
 
 
dc4a88c
0d77127
3b5f32b
0d77127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5046b5
 
0d77127
 
 
 
 
 
 
3ffb3f2
0d77127
3ffb3f2
dc4a88c
 
eb5ca81
3ffb3f2
6940aac
a892159
6940aac
 
0d77127
 
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
import spacy
import json
from datetime import datetime
import streamlit as st
import pandas as pd

nlp = spacy.load('la_core_web_lg')

def tokenize_with_spacy(text):
    doc = nlp(text)
    return [token.text for token in doc]

def annotate_text(segmented_text):
    annotated_tokens = []
    for token in segmented_text:
        doc = nlp(token)
        annotated_token = {
            'token': token,
            'pos': str(doc[0].pos_),
            'lemma': str(doc[0].lemma_),
            'aspect': ', '.join(doc[0].morph.get("Aspect", default=[""])),
            'tense': ', '.join(doc[0].morph.get("Tense", default=[""])),
            'verbForm': ', '.join(doc[0].morph.get("VerbForm", default=[""])),
            'voice': ', '.join(doc[0].morph.get("Voice", default=[""])),
            'mood': ', '.join(doc[0].morph.get("Mood", default=[""])),
            'number': ', '.join(doc[0].morph.get("Number", default=[""])),
            'person': ', '.join(doc[0].morph.get("Person", default=[""])),
            'case': ', '.join(doc[0].morph.get("Case", default=[""])),
            'gender': ', '.join(doc[0].morph.get("Gender", default=[""]))
        }
        annotated_tokens.append(annotated_token)
    return annotated_tokens

def save_annotations_as_json(annotated_text, filename):
    with open(filename, 'w', encoding='utf-8') as json_file:
        json.dump(annotated_text, json_file, ensure_ascii=False, indent=4)


st.set_page_config(layout="wide")
st.title("Annotation Tool")
text = st.text_area("Text")
if st.button("Annotate"):
    if text:
        segmented_text = tokenize_with_spacy(text)
        annotated_text = annotate_text(segmented_text)
        st.subheader("Segmented Text:")
        st.write(segmented_text)
        st.subheader("Annotated Text:")

        # Create a DataFrame from the annotated text
        df = pd.DataFrame(annotated_text)
        st.write(df)

        if st.button("Save Modifications as JSON"):
            save_annotations_as_json(df.to_dict(orient='records'), 'annotations.json')
            st.success("Annotations saved as annotations.json")

    else:
        st.warning("Please enter some text.")