-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_preprocessing.py
More file actions
118 lines (91 loc) · 3.39 KB
/
utils_preprocessing.py
File metadata and controls
118 lines (91 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# utility functions for preprocessing file
# imports
import random
import pandas as pd
from nltk.corpus import wordnet
stop_words = ['i', 'me', 'my', 'myself', 'we', 'our',
'ours', 'ourselves', 'you', 'your', 'yours',
'yourself', 'yourselves', 'he', 'him', 'his',
'himself', 'she', 'her', 'hers', 'herself',
'it', 'its', 'itself', 'they', 'them', 'their',
'theirs', 'themselves', 'what', 'which', 'who',
'whom', 'this', 'that', 'these', 'those', 'am',
'is', 'are', 'was', 'were', 'be', 'been', 'being',
'have', 'has', 'had', 'having', 'do', 'does', 'did',
'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or',
'because', 'as', 'until', 'while', 'of', 'at',
'by', 'for', 'with', 'about', 'against', 'between',
'into', 'through', 'during', 'before', 'after',
'above', 'below', 'to', 'from', 'up', 'down', 'in',
'out', 'on', 'off', 'over', 'under', 'again',
'further', 'then', 'once', 'here', 'there', 'when',
'where', 'why', 'how', 'all', 'any', 'both', 'each',
'few', 'more', 'most', 'other', 'some', 'such', 'no',
'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too',
'very', 's', 't', 'can', 'will', 'just', 'don',
'should', 'now', '']
def load_raw_data(path: str) -> pd.DataFrame:
"""Create a Dataframe containing each tweet
Args:
path (str): The path of the file (.txt) to load tweets from
Returns:
DataFrame: a Dataframe with one row per tweet
"""
data = []
with open(path) as file:
for line in file:
data.append(line)
data_df = pd.DataFrame(data, columns = {'tweet'})
return data_df
def get_synonyms(word):
"""
Get synonyms of a word
Args:
word to get synonyms of
Returns:
list of synonyms of word
Author:
Maël Fabien
Date:
2019
Availability:
maelfabien.github.io/machinelearning/NLP_8/#
"""
synonyms = set()
for syn in wordnet.synsets(word):
for l in syn.lemmas():
synonym = l.name().replace("_", " ").replace("-", " ").lower()
synonym = "".join([char for char in synonym if char in ' qwertyuiopasdfghjklzxcvbnm'])
synonyms.add(synonym)
if word in synonyms:
synonyms.remove(word)
return list(synonyms)
def synonym_replacement(words, n):
"""
replaces up to n synonyms of words in a string
Args:
word to get synonyms of
Returns:
list of synonyms of word
Author:
Maël Fabien
Date:
2019
Availability:
maelfabien.github.io/machinelearning/NLP_8/#
"""
words = words.split()
new_words = words.copy()
random_word_list = list(set([word for word in words if word not in stop_words]))
random.shuffle(random_word_list)
num_replaced = 0
for random_word in random_word_list:
synonyms = get_synonyms(random_word)
if len(synonyms) >= 1:
synonym = random.choice(list(synonyms))
new_words = [synonym if word == random_word else word for word in new_words]
num_replaced += 1
if num_replaced >= n: # only replace up to n words
break
sentence = ' '.join(new_words)
return sentence