File size: 3,441 Bytes
3ee4a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9a33161
 
3ee4a68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# %load apps/slider.py
from cgitb import html
import time
import streamlit as st
from annotated_text import annotated_text
import json
import random
import corrector
import classifier
from difflib import SequenceMatcher

def underline_diff(a,b):
    # source: https://stackoverflow.com/questions/774316/python-difflib-highlighting-differences-inline
    seqm = SequenceMatcher(None, a.split(),b.split())
    output= []
    for opcode, a0, a1, b0, b1 in seqm.get_opcodes():
        if opcode == 'equal':
            output.append(' '.join(seqm.a[a0:a1]))
        elif opcode == 'insert':
            output.append("<ins>" + ' '.join(seqm.b[b0:b1]) + "</ins>")
        elif opcode == 'delete':
            output.append("<del>" + ' '.join(seqm.a[a0:a1]) + "</del>")
        elif opcode == 'replace':
            output.append("<ins>" + ' '.join(seqm.b[b0:b1]) + "</ins>") # + " <del>" + ' '.join(seqm.a[a0:a1]) + "</del>")
    return ' '.join(output)

with open("examples.txt", "r") as f:
    example_sentences = f.readlines()

st.title("Virtual tutor - Proof of concept")

with st.sidebar:
    st.image("seedlang_logo.png", width=60)
    st.write("You can test here the Grammatical Error Correction and Classification models I developed as part of my internship at Seedlang.")

    ## include 5 error types
    st.subheader("Error types")
    st.write("This PoC corrects and detects 5 type of French beginner mistakes:")

    error_descriptions = {
        "ADJ": """adjective has wrong gender agreement (<i>*la grand table</i>)""",
        "ART": """article has wrong gender agreement (<i>*le table</i>)""",
        "ELI": """elision is missing (<i>*je adore</i>)""",
        "FIN": """non-finite form of the verb instead of a finite form (<i>*je adorer</i>)""",
        "NEG": """problem with the position of the negation (<i>*je ne pas adore</i>)"""
    }

    for error_type, colour in classifier.COLOURS.items():
        label = f"""<span style="background: {colour}; border-radius: 0.33rem; padding: 0.125rem 0.5rem; overflow: hidden;"><span style="padding-left: 0.5rem; text-transform: uppercase;"><span style="font-size: 0.67em; opacity: 0.8;">{error_type}</span></span></span> - {error_descriptions[error_type]}"""
        st.markdown(label, unsafe_allow_html=True)

    st.caption("The example sentences are taken from the development dataset. The errors were artificially generated, and many of the original sentences were too.")

if "value" not in st.session_state:
    st.session_state["value"] = random.choice(example_sentences)


random_ex = st.button("Get another random example")

if random_ex:
    st.session_state["value"] = random.choice(example_sentences)


sentence = st.text_input("Write a French sentence to correct and label", value=st.session_state["value"])
submit = st.button("Correct")

if submit:
    col1, col2 = st.columns(2)
    with col1:
        st.subheader("Labelled errors")
        with st.spinner("Labelling errors"):
            annotated_text(*classifier.annotate(sentence))
    with col2:
        st.subheader("Corrected sentence")
        with st.spinner("Correcting the sentence"):
            corrected_sent = corrector.correct(sentence)
            st.markdown(underline_diff(a=sentence, b=corrected_sent), unsafe_allow_html=True)
    st.caption("Those two models are completely independent, but I eventually want to only correct the character spans where an error was detected and classified.")