tanish78 commited on
Commit
087006d
·
verified ·
1 Parent(s): feb0a54

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +97 -2
app.py CHANGED
@@ -11,8 +11,103 @@ import tempfile
11
  import numpy as np
12
 
13
  def preprocess_data(df):
14
- # Your preprocessing code here
15
- pass
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def cluster_data(df, num_clusters):
18
  vectorizer = TfidfVectorizer(stop_words='english')
 
11
  import numpy as np
12
 
13
  def preprocess_data(df):
14
+ df.rename(columns={'Question Asked': 'texts'}, inplace=True)
15
+ df['texts'] = df['texts'].astype(str)
16
+ df['texts'] = df['texts'].str.lower()
17
+ df['texts'] = df['texts'].apply(lambda text: re.sub(r'https?://\S+|www\.\S+', '', text))
18
+
19
+ def remove_emoji(string):
20
+ emoji_pattern = re.compile("["
21
+ u"\U0001F600-\U0001F64F"
22
+ u"\U0001F300-\U0001F5FF"
23
+ u"\U0001F680-\U0001F6FF"
24
+ u"\U0001F1E0-\U0001F1FF"
25
+ u"\U00002702-\U000027B0"
26
+ u"\U000024C2-\U0001F251"
27
+ "]+", flags=re.UNICODE)
28
+ return emoji_pattern.sub(r'', string) if isinstance(string, str) else string
29
+
30
+ df['texts'] = df['texts'].apply(remove_emoji)
31
+
32
+ custom_synonyms = {
33
+ 'application': ['form'],
34
+ 'apply': ['fill', 'applied'],
35
+ 'work': ['job'],
36
+ 'salary': ['stipend', 'pay', 'payment', 'paid'],
37
+ 'test': ['online test', 'amcat test', 'exam', 'assessment'],
38
+ 'pass': ['clear', 'selected', 'pass or not'],
39
+ 'result': ['outcome', 'mark', 'marks'],
40
+ 'thanks': ["thanks a lot to you", "thankyou so much", "thank you so much", "tysm", "thank you",
41
+ "okaythank", "thx", "ty", "thankyou", "thank", "thank u"],
42
+ 'interview': ["pi"]
43
+ }
44
+
45
+ for original_word, synonym_list in custom_synonyms.items():
46
+ for synonym in synonym_list:
47
+ pattern = r"\b" + synonym + r"\b(?!\s*\()"
48
+ df['texts'] = df['texts'].str.replace(pattern, original_word, regex=True)
49
+ pattern = r"\b" + synonym + r"\s+you" + r"\b(?!\s*\()"
50
+ df['texts'] = df['texts'].str.replace(pattern, original_word + ' ', regex=True)
51
+
52
+ spam_list = ["click here", "free", "recharge", "limited", "discount", "money back guarantee", "aaj", "kal", "mein",
53
+ "how can i help you", "how can we help you", "how we can help you", "follow", "king", "contacting", "gar",
54
+ "kirke", "subscribe", "youtube", "jio", "insta", "make money", "b2b","sent using truecaller"]
55
+
56
+ rows_to_remove = set()
57
+ for spam_phrase in spam_list:
58
+ pattern = r"\b" + re.escape(spam_phrase) + r"\b"
59
+ spam_rows = df['texts'].str.contains(pattern)
60
+ rows_to_remove.update(df.index[spam_rows].tolist())
61
+
62
+ df = df.drop(rows_to_remove)
63
+
64
+ greet_variations = ["hello", "hy", "hey", "hii", "hi", "heyyy", "bie", "bye"]
65
+ for greet_var in greet_variations:
66
+ pattern = r"(?<!\S)" + greet_var + r"(?!\S)|\b" + greet_var + r"\b"
67
+ df['texts'] = df['texts'].str.replace(pattern, '', regex=True)
68
+
69
+ okay_variations = ["ok", "k", "kay", "okay", "okie", "kk", "ohhhk","t","r"]
70
+ for okay_var in okay_variations:
71
+ pattern = r"(?<!\S)" + okay_var + r"(?!\S)|\b" + okay_var + r"\b"
72
+ df['texts'] = df['texts'].str.replace(pattern, '', regex=True)
73
+
74
+ yes_variations = ["yes", "yeah", "yep", "yup", "yuh", "ya", "yes got it", "yeah it is", "yesss", "yea","no"]
75
+ for yes_var in yes_variations:
76
+ pattern = r"(?<!\S)" + yes_var + r"(?!\S)|\b" + yes_var + r"\b"
77
+ df['texts'] = df['texts'].str.replace(pattern, '', regex=True)
78
+
79
+ remove_phrases = ["i'm all set","ask a question","apply the survey","videos (2-8 min)","long reads (> 8 min)",
80
+ "short reads (3-8 min)","not a student alumni","mock","share feedback","bite size (< 2 min)",
81
+ "actually no","next steps","i'm a student alumni","i have questions"]
82
+
83
+ for phrase in remove_phrases:
84
+ df['texts'] = df['texts'].str.replace(phrase, '')
85
+
86
+ general_variations = ["good morning", "good evening", "good afternoon", "good night", "done", "sorry", "top", "query",
87
+ "stop", "sir", "sure", "oh", "wow", "aaa", "maam", "mam", "ma&#39;am","i'm all set","ask a question","apply the survey",
88
+ "videos (2-8 min)","long reads (> 8 min)","short reads (3-8 min)","not a student alumni","mock","share feedback","bite size (< 2 min)",
89
+ "actually no","next steps","i'm a student alumni","i have questions"]
90
+ for gen_var in general_variations:
91
+ pattern = r"(?<!\S)" + gen_var + r"(?!\S)|\b" + gen_var + r"\b(?=\W|$)"
92
+ df['texts'] = df['texts'].str.replace(pattern, '', regex=True)
93
+
94
+ def remove_punctuations(text):
95
+ return re.sub(r'[^\w\s]', '', text)
96
+ df['texts'] = df['texts'].apply(remove_punctuations)
97
+
98
+ remove_morephrases = ["short reads 38 min","bite size 2 min","videos 28 min","long reads 8 min"]
99
+
100
+ for phrase in remove_morephrases:
101
+ df['texts'] = df['texts'].str.replace(phrase, '')
102
+
103
+ df = df[~df['texts'].str.contains(r'\b\d{10}\b')]
104
+
105
+ df['texts'] = df['texts'].str.strip()
106
+
107
+ df['texts'] = df['texts'].apply(lambda x: x.strip())
108
+ df = df[df['texts'] != '']
109
+
110
+ return df
111
 
112
  def cluster_data(df, num_clusters):
113
  vectorizer = TfidfVectorizer(stop_words='english')