-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_text.py
More file actions
54 lines (34 loc) · 1.54 KB
/
process_text.py
File metadata and controls
54 lines (34 loc) · 1.54 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Created on Tue May 5 13:24:27 2020
# @author: miranda (upquark00)
import re
import string
# TODO: add functionality to filter out strings containing only digits.
# Deal with emojis, imbalanced classes, and the many words not in GloVe.
# Enact explicit lemmatization
def clean(tweets_array):
'''
Tokenizes and cleans data. Removes punctuation, Twitter @mentions, URLS,
and extraneous spaces. Lowercases words.
Args:
tweets_array (numpy array): shape (m,), containing m number of Tweets,
which are sequences of strings.
Returns:
list_of_lists (list): list of lists of strings of length n, where n is
equal to the number of examples in the data set.
'''
for i in range(len(tweets_array)):
tweets_array[i] = re.sub(r'@[A-Za-z0-9]+','', re.sub(r'#[A-Za-z0-9]+','', tweets_array[i])).lower()
list_of_lists = [tweet.lower() for tweet in tweets_array]
list_of_lists = [re.sub('https?://[A-Za-z0-9./]+','', tweet) for tweet in list_of_lists]
list_of_lists = [tweet.translate(str.maketrans('', '', string.punctuation)) for tweet in list_of_lists]
list_of_lists = [tweet.split(' ') for tweet in list_of_lists]
for i in range(len(list_of_lists)):
list_of_lists[i] = [word for word in list_of_lists[i] if word != '']
return list_of_lists
def main():
print('Module finished.')
# clean_tweets = clean()
if __name__ == "__main__":
main()