Spaces:
Build error
Build error
feat: Add preprocessing function to improve quality of topic detection
Browse files
app.py
CHANGED
@@ -1,10 +1,13 @@
|
|
1 |
from typing import List
|
2 |
|
3 |
-
import
|
4 |
-
|
5 |
-
import streamlit as st
|
6 |
import tweepy
|
7 |
import hdbscan
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
from bokeh.models import ColumnDataSource, HoverTool, Label
|
10 |
from bokeh.palettes import Colorblind as Pallete
|
@@ -21,6 +24,38 @@ model_to_use = {
|
|
21 |
"Use all the ones you know (~15 lang)": "paraphrase-multilingual-MiniLM-L12-v2"
|
22 |
}
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
# Original implementation from: https://huggingface.co/spaces/edugp/embedding-lenses/blob/main/app.py
|
25 |
SEED = 42
|
26 |
|
@@ -137,6 +172,7 @@ if go_btn and tw_user != '':
|
|
137 |
tweets_objs += tweets_response.data
|
138 |
tweets_txt = [tweet.text for tweet in tweets_objs]
|
139 |
tweets_txt = list(set(tweets_txt))
|
|
|
140 |
# plot = generate_plot(df, text_column, label_column, sample, dimensionality_reduction_function, model)
|
141 |
plot = generate_plot(tweets_txt, model, tw_user)
|
142 |
st.bokeh_chart(plot)
|
|
|
1 |
from typing import List
|
2 |
|
3 |
+
import re
|
|
|
|
|
4 |
import tweepy
|
5 |
import hdbscan
|
6 |
+
import numpy as np
|
7 |
+
import streamlit as st
|
8 |
+
|
9 |
+
|
10 |
+
from gensim.utils import deaccent # gensim==3.8.1
|
11 |
|
12 |
from bokeh.models import ColumnDataSource, HoverTool, Label
|
13 |
from bokeh.palettes import Colorblind as Pallete
|
|
|
24 |
"Use all the ones you know (~15 lang)": "paraphrase-multilingual-MiniLM-L12-v2"
|
25 |
}
|
26 |
|
27 |
+
def remove_unk_chars(txt_list: List[str]):
|
28 |
+
txt_list = [re.sub('\s+', ' ', tweet) for tweet in txt_list]
|
29 |
+
txt_list = [re.sub("\'", "", tweet) for tweet in txt_list]
|
30 |
+
txt_list = [deaccent(tweet).lower() for tweet in txt_list]
|
31 |
+
|
32 |
+
def _remove_urls(txt_list: List[str]):
|
33 |
+
url_regex = re.compile(
|
34 |
+
r'^(?:http|ftp)s?://' # http:// or https://
|
35 |
+
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' #domain...
|
36 |
+
r'localhost|' #localhost...
|
37 |
+
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
|
38 |
+
r'(?::\d+)?' # optional port
|
39 |
+
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
|
40 |
+
txt_list = [tweet.split(' ') for tweet in txt_list]
|
41 |
+
return [' '.join([word for word in tweet if not bool(re.match(url_regex, word))]) for tweet in txt_list]
|
42 |
+
|
43 |
+
def _remove_punctuation(txt_list: List[str]):
|
44 |
+
punctuation = string.punctuation + '¿¡|'
|
45 |
+
txt_list = [tweet.split(' ') for tweet in txt_list]
|
46 |
+
return [' '.join([word.translate(str.maketrans('', '', punctuation)) for word in tweet]) for tweet in txt_list]
|
47 |
+
|
48 |
+
preprocess_pipeline = [
|
49 |
+
_remove_unk_chars,
|
50 |
+
_remove_urls,
|
51 |
+
_remove_punctuation
|
52 |
+
]
|
53 |
+
|
54 |
+
def preprocess(txt_list: str):
|
55 |
+
for op in preprocess_pipeline:
|
56 |
+
txt_list = op(txt_list)
|
57 |
+
return txt_list
|
58 |
+
|
59 |
# Original implementation from: https://huggingface.co/spaces/edugp/embedding-lenses/blob/main/app.py
|
60 |
SEED = 42
|
61 |
|
|
|
172 |
tweets_objs += tweets_response.data
|
173 |
tweets_txt = [tweet.text for tweet in tweets_objs]
|
174 |
tweets_txt = list(set(tweets_txt))
|
175 |
+
tweets_txt = preproces(tweets_txt)
|
176 |
# plot = generate_plot(df, text_column, label_column, sample, dimensionality_reduction_function, model)
|
177 |
plot = generate_plot(tweets_txt, model, tw_user)
|
178 |
st.bokeh_chart(plot)
|