EagleOfEmpire commited on
Commit
20f7681
·
verified ·
1 Parent(s): 4f6201a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -2
app.py CHANGED
@@ -27,8 +27,42 @@ EMOTIONS = ["neutral", "joy", "sadness", "anger", "fear", "surprise"]
27
  # ВАША ПРЕДОБРАБОТКА ТЕКСТА
28
  # ---------------------------
29
  def preprocess_text(text):
30
- # !!! ВСТАВЬТЕ ВАШ КОД СЮДА !!!
31
- # полностью всю функцию preprocess_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  return text
33
 
34
 
 
27
  # ВАША ПРЕДОБРАБОТКА ТЕКСТА
28
  # ---------------------------
29
  def preprocess_text(text):
30
+ def preprocess_text(text):
31
+
32
+ text = remove_duplicate_emojis(text) #удаление дублирующихся смайликов
33
+ if is_emoji_spam(text):
34
+ text = remove_all_emojis(text)
35
+
36
+ text = str(text).lower()#нижний регистр
37
+ text = re.sub(r'http\S+|www\S+|https\S+', '', text)#меняет всё, что начинается с hhtp, www, https на ''. S+ - один или более непробельных символов, | - или
38
+ text = re.sub(r'@\w+|#\w+', '', text)#удалили упоминания и хэштеги. \w+ - одна или более цифра/буква/нижнее подчёркивание.
39
+ text = text.translate(str.maketrans('', '', string.punctuation))#удалили пунктуацию. str.maketrans(, , <символы для полного удаления>), text.translate - применение maketrans.
40
+
41
+ text = emoji.demojize(text)#замена эмоций на текстовые метки (после удаления пунктуации, чтобы не удалилось выделение меток)
42
+
43
+ text = re.sub(r'\d+', '', text)#удалили цифры
44
+
45
+ try:
46
+ tokens = word_tokenize(text, language="russian")# токенизация
47
+ except:
48
+ tokens = text.split()
49
+
50
+ try:
51
+ stop_words = set(stopwords.words('russian')) # удаление стоп-слов
52
+ except:
53
+ stop_words = set()
54
+
55
+ tokens = [word for word in tokens if (word.isalpha() or (word.startswith(':') and word.endswith(':'))) and word not in stop_words and len(word) > 2]
56
+ #использование isalpha - удаление всего, что имеет в составе цифры, знаки препинания, удаление стоп слова и очень коротких слов
57
+ #но оставить метки смайликов
58
+ try:
59
+ lemmatizer = pymorphy2.MorphAnalyzer()# лемматизация, используя pymorphy
60
+ tokens = [lemmatizer.parse(word)[0].normal_form for word in tokens]
61
+ except:
62
+ pass
63
+
64
+
65
+ return ' '.join(tokens)
66
  return text
67
 
68