Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,60 @@
|
|
|
|
|
|
|
|
| 1 |
# Positive Wörter laden
|
| 2 |
positive = {}
|
| 3 |
with open("SentiWS_v2.0_Positive.txt", encoding="utf-8") as f:
|
| 4 |
for line in f:
|
| 5 |
-
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Negative Wörter laden
|
| 9 |
negative = {}
|
| 10 |
with open("SentiWS_v2.0_Negative.txt", encoding="utf-8") as f:
|
| 11 |
for line in f:
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
# Positive Wörter laden
|
| 4 |
positive = {}
|
| 5 |
with open("SentiWS_v2.0_Positive.txt", encoding="utf-8") as f:
|
| 6 |
for line in f:
|
| 7 |
+
parts = line.strip().split("\t")
|
| 8 |
+
if len(parts) >= 2:
|
| 9 |
+
wort = parts[0].split("\\")[0].lower()
|
| 10 |
+
score = float(parts[1])
|
| 11 |
+
positive[wort] = score
|
| 12 |
+
# Flexionsformen hinzufügen
|
| 13 |
+
if len(parts) > 2:
|
| 14 |
+
for form in parts[2].split(","):
|
| 15 |
+
positive[form.lower()] = score
|
| 16 |
|
| 17 |
# Negative Wörter laden
|
| 18 |
negative = {}
|
| 19 |
with open("SentiWS_v2.0_Negative.txt", encoding="utf-8") as f:
|
| 20 |
for line in f:
|
| 21 |
+
parts = line.strip().split("\t")
|
| 22 |
+
if len(parts) >= 2:
|
| 23 |
+
wort = parts[0].split("\\")[0].lower()
|
| 24 |
+
score = float(parts[1])
|
| 25 |
+
negative[wort] = score
|
| 26 |
+
if len(parts) > 2:
|
| 27 |
+
for form in parts[2].split(","):
|
| 28 |
+
negative[form.lower()] = score
|
| 29 |
+
# Analysefunktion
|
| 30 |
+
def analyze_sentiment(text):
|
| 31 |
+
words = text.lower().split()
|
| 32 |
+
pos_score = sum(positive.get(w, 0) for w in words)
|
| 33 |
+
neg_score = sum(negative.get(w, 0) for w in words)
|
| 34 |
|
| 35 |
+
if pos_score > abs(neg_score):
|
| 36 |
+
label = "✅ Positiv"
|
| 37 |
+
elif neg_score > abs(pos_score):
|
| 38 |
+
label = "❌ Negativ"
|
| 39 |
+
else:
|
| 40 |
+
label = "➖ Neutral"
|
| 41 |
+
|
| 42 |
+
return {
|
| 43 |
+
"Ergebnis": label,
|
| 44 |
+
"Positiv-Score": round(pos_score, 4),
|
| 45 |
+
"Negativ-Score": round(neg_score, 4)
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
# Gradio-Interface
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("# Textklassifikation mit SentiWS")
|
| 51 |
+
gr.Markdown("Gib einen Satz ein und erfahre, ob er positiv oder negativ klingt.")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
inp = gr.Textbox(label="Dein Text", placeholder="Schreibe hier...")
|
| 55 |
+
out = gr.Label(label="Analyse")
|
| 56 |
+
|
| 57 |
+
btn = gr.Button("Analysieren")
|
| 58 |
+
btn.click(analyze_sentiment, inputs=inp, outputs=out)
|
| 59 |
+
|
| 60 |
+
demo.launch()
|