ElijahDi commited on
Commit
76005f9
1 Parent(s): 71f43bd

Upload functions.py

Browse files
Files changed (1) hide show
  1. resources/functions.py +42 -0
resources/functions.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import string
3
+ import pandas as pd
4
+ import nltk
5
+ import pymorphy2
6
+ from nltk.corpus import stopwords
7
+ nltk.download('stopwords')
8
+ from sentence_transformers import util
9
+
10
+ stop_words = set(stopwords.words('russian'))
11
+ morph = pymorphy2.MorphAnalyzer()
12
+
13
+ def data_preprocessing_hard(text: str) -> str:
14
+ text = str(text)
15
+ text = text.lower()
16
+ text = re.sub('<.*?>', '', text)
17
+ text = re.sub(r'[^а-яА-Я\s]', '', text)
18
+ text = ''.join([c for c in text if c not in string.punctuation])
19
+ text = ' '.join([word for word in text.split() if word not in stop_words])
20
+ # text = ''.join([char for char in text if not char.isdigit()])
21
+ text = ' '.join([morph.parse(word)[0].normal_form for word in text.split()])
22
+ return text
23
+
24
+ def find_rows_with_genres(df, genres_list):
25
+ # df['ganres'].fillna('', inplace=True)
26
+ genres_pattern = '|'.join(genres_list)
27
+ mask = df['ganres'].str.contains(genres_pattern, regex=True)
28
+ return mask
29
+
30
+ def get_mask_in_range(df, range_values):
31
+ min_year, max_year = range_values
32
+ return (df['year'] >= min_year) & (df['year'] <= max_year)
33
+
34
+ def recommend(model, text: str, embeddings, top_k):
35
+ query_embeddings = model.encode([text], convert_to_tensor=True)
36
+ embeddings = embeddings.to("cpu")
37
+ # embeddings = util.normalize_embeddings(embeddings)
38
+
39
+ query_embeddings = query_embeddings.to("cpu")
40
+ # query_embeddings = util.normalize_embeddings(query_embeddings)
41
+ hits = util.semantic_search(query_embeddings, embeddings, top_k=top_k)#, score_function=util.dot_score)
42
+ return hits