Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def extract_context_words(text, high_information_words):
|
2 |
words = nltk.word_tokenize(text)
|
3 |
context_words = []
|
4 |
-
|
5 |
for index, word in enumerate(words):
|
6 |
if word.lower() in high_information_words:
|
7 |
before_word = words[index - 1] if index > 0 else None
|
8 |
after_word = words[index + 1] if index < len(words) - 1 else None
|
9 |
context_words.append((before_word, word, after_word))
|
10 |
-
|
11 |
return context_words
|
12 |
|
13 |
def create_context_graph(context_words):
|
14 |
graph = Digraph()
|
15 |
-
|
16 |
for index, (before_word, high_info_word, after_word) in enumerate(context_words):
|
17 |
graph.node(f'before{index}', before_word, shape='box') if before_word else None
|
18 |
graph.node(f'high{index}', high_info_word, shape='ellipse')
|
19 |
graph.node(f'after{index}', after_word, shape='diamond') if after_word else None
|
20 |
-
|
21 |
if before_word:
|
22 |
graph.edge(f'before{index}', f'high{index}')
|
23 |
if after_word:
|
24 |
graph.edge(f'high{index}', f'after{index}')
|
25 |
-
|
26 |
return graph
|
27 |
|
28 |
def display_context_graph(context_words):
|
@@ -35,8 +63,7 @@ def display_context_table(context_words):
|
|
35 |
table += f"| {before if before else ''} | {high} | {after if after else ''} |\n"
|
36 |
st.markdown(table)
|
37 |
|
38 |
-
|
39 |
-
# ...
|
40 |
|
41 |
if uploaded_file:
|
42 |
file_text = uploaded_file.read().decode("utf-8")
|
@@ -46,10 +73,10 @@ if uploaded_file:
|
|
46 |
st.markdown("**Top 10 High Information Words:**")
|
47 |
st.write(top_words)
|
48 |
|
49 |
-
context_words = extract_context_words(text_without_timestamps, top_words)
|
50 |
st.markdown("**Relationship Graph:**")
|
51 |
display_relationship_graph(top_words)
|
52 |
|
|
|
53 |
st.markdown("**Context Graph:**")
|
54 |
display_context_graph(context_words)
|
55 |
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import re
|
3 |
+
import nltk
|
4 |
+
from nltk.corpus import stopwords
|
5 |
+
from nltk import FreqDist
|
6 |
+
from graphviz import Digraph
|
7 |
+
|
8 |
+
nltk.download('punkt')
|
9 |
+
nltk.download('stopwords')
|
10 |
+
|
11 |
+
def remove_timestamps(text):
|
12 |
+
return re.sub(r'\d{1,2}:\d{2}\n.*\n', '', text)
|
13 |
+
|
14 |
+
def extract_high_information_words(text, top_n=10):
|
15 |
+
words = nltk.word_tokenize(text)
|
16 |
+
words = [word.lower() for word in words if word.isalpha()]
|
17 |
+
stop_words = set(stopwords.words('english'))
|
18 |
+
filtered_words = [word for word in words if word not in stop_words]
|
19 |
+
freq_dist = FreqDist(filtered_words)
|
20 |
+
return [word for word, _ in freq_dist.most_common(top_n)]
|
21 |
+
|
22 |
+
def create_relationship_graph(words):
|
23 |
+
graph = Digraph()
|
24 |
+
for index, word in enumerate(words):
|
25 |
+
graph.node(str(index), word)
|
26 |
+
if index > 0:
|
27 |
+
graph.edge(str(index - 1), str(index), label=str(index))
|
28 |
+
return graph
|
29 |
+
|
30 |
+
def display_relationship_graph(words):
|
31 |
+
graph = create_relationship_graph(words)
|
32 |
+
st.graphviz_chart(graph)
|
33 |
+
|
34 |
def extract_context_words(text, high_information_words):
|
35 |
words = nltk.word_tokenize(text)
|
36 |
context_words = []
|
|
|
37 |
for index, word in enumerate(words):
|
38 |
if word.lower() in high_information_words:
|
39 |
before_word = words[index - 1] if index > 0 else None
|
40 |
after_word = words[index + 1] if index < len(words) - 1 else None
|
41 |
context_words.append((before_word, word, after_word))
|
|
|
42 |
return context_words
|
43 |
|
44 |
def create_context_graph(context_words):
|
45 |
graph = Digraph()
|
|
|
46 |
for index, (before_word, high_info_word, after_word) in enumerate(context_words):
|
47 |
graph.node(f'before{index}', before_word, shape='box') if before_word else None
|
48 |
graph.node(f'high{index}', high_info_word, shape='ellipse')
|
49 |
graph.node(f'after{index}', after_word, shape='diamond') if after_word else None
|
|
|
50 |
if before_word:
|
51 |
graph.edge(f'before{index}', f'high{index}')
|
52 |
if after_word:
|
53 |
graph.edge(f'high{index}', f'after{index}')
|
|
|
54 |
return graph
|
55 |
|
56 |
def display_context_graph(context_words):
|
|
|
63 |
table += f"| {before if before else ''} | {high} | {after if after else ''} |\n"
|
64 |
st.markdown(table)
|
65 |
|
66 |
+
uploaded_file = st.file_uploader("Choose a .txt file", type=['txt'])
|
|
|
67 |
|
68 |
if uploaded_file:
|
69 |
file_text = uploaded_file.read().decode("utf-8")
|
|
|
73 |
st.markdown("**Top 10 High Information Words:**")
|
74 |
st.write(top_words)
|
75 |
|
|
|
76 |
st.markdown("**Relationship Graph:**")
|
77 |
display_relationship_graph(top_words)
|
78 |
|
79 |
+
context_words = extract_context_words(text_without_timestamps, top_words)
|
80 |
st.markdown("**Context Graph:**")
|
81 |
display_context_graph(context_words)
|
82 |
|