teven commited on
Commit
f924b14
1 Parent(s): 21fafec

TVN update

Browse files
Files changed (2) hide show
  1. app.py +53 -81
  2. en_examples_with_stats_no_small_docs.json +3 -0
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import streamlit as st
2
  import json
3
  import pandas as pd
 
4
  import numpy as np
5
  import matplotlib.pyplot as plt
6
 
@@ -15,7 +16,7 @@ def visualization(path_data, lang, num_docs, num_docs_for_words):
15
  st.title(f"{num_docs} {lang} documents from Oscar with their stats.")
16
 
17
  sentences = [doc["text"].split(" ") for doc in data[:num_docs_for_words]]
18
- words = [word for sentence in sentences for word in sentence]
19
  words_data = [{"len_word": len(word), "word": word} for word in words]
20
  words_data = pd.DataFrame(words_data)
21
 
@@ -24,39 +25,46 @@ def visualization(path_data, lang, num_docs, num_docs_for_words):
24
 
25
  columns = list(data)
26
  keys = []
 
27
 
28
- st.header("Parameters of the filtering")
29
 
30
- if "special_characters_ratio" in columns:
31
- cutoff_special_characters_ratio = st.slider(
32
- "Max cutoff special characters ratio", 0.0, 1.0, 1.0, step=0.01
33
- )
34
- keys.append(("special_characters_ratio", cutoff_special_characters_ratio, True))
35
 
36
- if "stopwords_ratio" in columns:
37
- cutoff_stopwords_ratio = st.slider(
38
- "Min cutoff stopwords ratio", 0.0, 1.0, 0.0, step=0.01
39
  )
40
- keys.append(("stopwords_ratio", cutoff_stopwords_ratio, False))
41
-
42
- if "badwords_ratio" in columns:
43
- cutoff_badwords_ratio = st.slider(
44
- "Max cutoff badwords ratio", 0.0, 1.0, 1.0, step=0.001
 
 
 
45
  )
46
- keys.append(("badwords_ratio", cutoff_badwords_ratio, True))
47
-
48
- if "lang_id_score" in columns:
49
- cutoff_lang_id_score = st.slider(
50
- "Min cutoff lang id score", 0.0, 1.0, 0.0, step=0.01
 
 
 
51
  )
52
- keys.append(("lang_id_score", cutoff_lang_id_score, False))
53
-
54
- if "perplexity_score" in columns:
55
- max_pp = int(np.max(data["perplexity_score"])) + 1
56
- cutoff_perplexity_score = st.slider(
57
- "Perplexity cutoff perplexity score", 0, max_pp, max_pp
 
 
58
  )
59
- keys.append(("perplexity_score", cutoff_perplexity_score, True))
 
 
 
60
 
61
  cond = [
62
  (data[key] <= cutoff) if max_cutoff else (data[key] >= cutoff)
@@ -64,78 +72,42 @@ def visualization(path_data, lang, num_docs, num_docs_for_words):
64
  ]
65
  cond = np.all(cond, axis=0)
66
 
67
- data_keep = data.loc[cond]
68
- st.header("Data that we keep")
69
- st.markdown("Click on a column to sort by it.")
70
- st.markdown("Place the cursor on the text to display it.")
71
- st.dataframe(data_keep)
72
-
73
  data_not_keep = data.loc[np.invert(cond)]
74
- st.header("Data that is thrown away")
75
- st.markdown("Click on a column to sort by it.")
76
- st.markdown("Place the cursor on the text to display it.")
77
  st.dataframe(data_not_keep)
78
 
 
 
 
 
 
79
  def plot_hist(dataframe, key, num_bins=50):
80
- st.header(" ".join(key.split("_")))
81
  hist_values = dataframe[key].values
82
  max_range = np.max(hist_values)
83
  hist_values = np.histogram(hist_values, bins=num_bins, range=(0, max_range))[0]
84
  st.bar_chart(hist_values)
85
  st.markdown(f"Each bin is of size: {max_range/num_bins}.")
86
 
87
- for key, _, _ in keys:
88
- plot_hist(data, key)
89
-
90
- st.header("Zipf's Law")
91
-
92
- def get_frequency_words(data):
93
- freq_words = {}
94
- for index, row in data.iterrows():
95
- for word in row["text"].split(" "):
96
- if word in freq_words:
97
- freq_words[word] += 1
98
- else:
99
- freq_words[word] = 1
100
- freq_words = np.array(list(freq_words.values()))
101
- freq_words = -np.sort(-freq_words)
102
- return freq_words
103
-
104
- freq_words_data = get_frequency_words(data)
105
- freq_words_data_keep = get_frequency_words(data_keep)
106
- freq_words_data_not_keep = get_frequency_words(data_not_keep)
107
-
108
- fig, ax = plt.subplots()
109
- ax.loglog(freq_words_data)
110
- ax.loglog(freq_words_data_keep)
111
- ax.loglog(freq_words_data_not_keep)
112
- ax.set_title("Zipf's Law")
113
- ax.set_xlabel("$i$-th most frequent word")
114
- ax.set_ylabel("frequency in the documents")
115
- ax.legend(["All data", "Data that we keep", "Data that is thrown away"])
116
- st.pyplot(fig)
117
-
118
- st.markdown("If less than three curves are displayed, it means that there are overlaps.")
119
-
120
- st.header("Parameter of the filtering for words")
121
  max_len_word = int(np.max(words_data["len_word"])) + 1
122
- cutoff_word = st.slider("Max cutoff length word", 0, max_len_word, max_len_word)
123
  cond_words = words_data["len_word"] <= cutoff_word
124
 
125
  words_keep = words_data.loc[cond_words]
126
- st.header(f"Words that we keep (for {num_docs_for_words} documents)")
127
- st.markdown("Click on a column to sort by it.")
128
- st.markdown("Place the cursor on the text to display it.")
129
  st.dataframe(words_keep)
130
 
131
  words_not_keep = words_data.loc[np.invert(cond_words)]
132
- st.header(f"Words that are thrown away (for {num_docs_for_words} documents)")
133
- st.markdown("Click on a column to sort by it.")
134
- st.markdown("Place the cursor on the text to display it.")
135
  st.dataframe(words_not_keep)
136
 
137
- plot_hist(words_data, "len_word")
138
-
139
  st.header("Download data")
140
 
141
  with open(path_data) as json_file:
@@ -146,7 +118,7 @@ def visualization(path_data, lang, num_docs, num_docs_for_words):
146
  )
147
 
148
 
149
- path_data = "./en_examples_with_stats.json"
150
  lang = "English"
151
  num_docs = 5000
152
  num_docs_for_words = 500
 
1
  import streamlit as st
2
  import json
3
  import pandas as pd
4
+ import math
5
  import numpy as np
6
  import matplotlib.pyplot as plt
7
 
 
16
  st.title(f"{num_docs} {lang} documents from Oscar with their stats.")
17
 
18
  sentences = [doc["text"].split(" ") for doc in data[:num_docs_for_words]]
19
+ words = set([word for sentence in sentences for word in sentence])
20
  words_data = [{"len_word": len(word), "word": word} for word in words]
21
  words_data = pd.DataFrame(words_data)
22
 
 
25
 
26
  columns = list(data)
27
  keys = []
28
+ values = {}
29
 
30
+ st.header("Filtering based on document content")
31
 
 
 
 
 
 
32
 
33
+ if "special_%" in columns:
34
+ special_ratio = st.sidebar.slider(
35
+ "% filtered by special characters ratio", 0.0, 100.0, 0.0, step=1.0
36
  )
37
+ cutoff_index = max(0, math.floor((100 - special_ratio) * len(data.index) / 100) - 1)
38
+ special_cutoff = np.partition(data["special_%"], cutoff_index)[cutoff_index]
39
+ st.sidebar.text(f"Kept text with <{special_cutoff:.1f}% special chars")
40
+ keys.append(("special_%", special_cutoff, True))
41
+
42
+ if "stop_%" in columns:
43
+ stop_ratio = st.sidebar.slider(
44
+ "% filtered by stop word ratio", 0.0, 100.0, 0.0, step=1.0
45
  )
46
+ cutoff_index = max(0, math.floor(stop_ratio * len(data.index) / 100) - 1)
47
+ stop_cutoff = np.partition(data["stop_%"], cutoff_index)[cutoff_index]
48
+ st.sidebar.text(f"Kept text with >{stop_cutoff:.1f}% stop words")
49
+ keys.append(("stop_%", stop_cutoff, False))
50
+
51
+ if "bad_%" in columns:
52
+ bad_ratio = st.sidebar.slider(
53
+ "% filtered by badwords ratio", 0.0, 100.0, 0.0, step=1.0
54
  )
55
+ bad_index = max(0, math.floor((100 - bad_ratio) * len(data.index) / 100) - 1)
56
+ bad_cutoff = np.partition(data["bad_%"], bad_index)[bad_index]
57
+ st.sidebar.text(f"Kept text with <{bad_cutoff:.1f}% bad words")
58
+ keys.append(("bad_%", bad_cutoff, True))
59
+
60
+ if "perplexity" in columns:
61
+ ppl_ratio = st.sidebar.slider(
62
+ "% filtered by perplexity", 0.0, 100.0, 0.0, step=1.0
63
  )
64
+ ppl_index = max(0, math.floor((100 - ppl_ratio) * len(data.index) / 100) - 1)
65
+ ppl_cutoff = np.partition(data["perplexity"], ppl_index)[ppl_index]
66
+ st.sidebar.text(f"Kept text with <{ppl_cutoff:.0f} perplexity")
67
+ keys.append(("perplexity", ppl_cutoff, True))
68
 
69
  cond = [
70
  (data[key] <= cutoff) if max_cutoff else (data[key] >= cutoff)
 
72
  ]
73
  cond = np.all(cond, axis=0)
74
 
 
 
 
 
 
 
75
  data_not_keep = data.loc[np.invert(cond)]
76
+ st.subheader("Filtered data")
77
+ st.markdown("Click on a column to sort by it, place the cursor on the text to display it.")
 
78
  st.dataframe(data_not_keep)
79
 
80
+ data_keep = data.loc[cond]
81
+ st.subheader("Kept data")
82
+ st.markdown("Click on a column to sort by it, place the cursor on the text to display it.")
83
+ st.dataframe(data_keep)
84
+
85
  def plot_hist(dataframe, key, num_bins=50):
86
+ st.subheader(" ".join(key.split("_")))
87
  hist_values = dataframe[key].values
88
  max_range = np.max(hist_values)
89
  hist_values = np.histogram(hist_values, bins=num_bins, range=(0, max_range))[0]
90
  st.bar_chart(hist_values)
91
  st.markdown(f"Each bin is of size: {max_range/num_bins}.")
92
 
93
+ # for key, _, _ in keys:
94
+ # plot_hist(data, key)
95
+
96
+ st.header("Filtering links and concatenated words")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  max_len_word = int(np.max(words_data["len_word"])) + 1
98
+ cutoff_word = st.sidebar.slider("Word length cutoff", 0, max_len_word, max_len_word)
99
  cond_words = words_data["len_word"] <= cutoff_word
100
 
101
  words_keep = words_data.loc[cond_words]
102
+ st.subheader(f"Words that we keep (for {num_docs_for_words} documents)")
103
+ st.markdown("Click on a column to sort by it, place the cursor on the text to display it.")
 
104
  st.dataframe(words_keep)
105
 
106
  words_not_keep = words_data.loc[np.invert(cond_words)]
107
+ st.subheader(f"Words that are thrown away (for {num_docs_for_words} documents)")
108
+ st.markdown("Click on a column to sort by it, place the cursor on the text to display it.")
 
109
  st.dataframe(words_not_keep)
110
 
 
 
111
  st.header("Download data")
112
 
113
  with open(path_data) as json_file:
 
118
  )
119
 
120
 
121
+ path_data = "./en_examples_with_stats_no_small_docs.json"
122
  lang = "English"
123
  num_docs = 5000
124
  num_docs_for_words = 500
en_examples_with_stats_no_small_docs.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:42de045d52e16b4c96ec03b332c12f406e52b22b442234eea4845f5b5598784c
3
+ size 21200705