-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreprocess.py
91 lines (58 loc) · 2.65 KB
/
preprocess.py
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
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.ERROR)
import json
import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)
import nltk
from builtins import str
import re
from nltk.stem.wordnet import WordNetLemmatizer
import os
def preprocessTweets(tweetdate,file,storeFolder):
tweets_file = open(os.path.join('Temp', file), "r")
processed = open(os.path.join(storeFolder, tweetdate), 'w',encoding='utf-8')
with open('stopwords.txt') as myfile:
for tweet in tweets_file:
try:
tweet = json.loads(tweet)
# print(datetime.(tweet['created_at']).date)
if 'text' in tweet:
line = tweet['text']
# Remove @xxxx and #xxxxx
content = [str(word.lower()) for word in line.split() if
word.find('@') == -1 and word.find('#') == -1 and word.find('http') == -1]
# join words list to one string
content = ' '.join(content)
# remove symbols
content = re.sub(r'[^\w]', ' ', content)
# remove stop words
content = [word for word in content.split() if
word not in myfile.read() and len(word) > 3 and not any(
c.isdigit() for c in word)]
# join words list to one string
content = ' '.join(content)
# Stemming and lemmatization
lmtzr = WordNetLemmatizer()
content = lmtzr.lemmatize(content)
# Filter only nouns and adjectives
tokenized = nltk.word_tokenize(content)
classified = nltk.pos_tag(tokenized)
content = [word for (word, clas) in classified if
clas == 'NN' or clas == 'NNS' or clas == 'NNP' or clas == 'NNPS' or clas == 'JJ' or clas == 'JJR' or clas == 'JJS']
# join words list to one string
content = ' '.join(content)
if len(content) > 0:
# tweets.append([line[0], content, line[2]])
# total_tweets += 1
processed.write(content)
processed.write('\n')
except:
continue
processed.close()
tweets_file.close()
path=os.path.join('Temp', file)
try:
if os.path.isfile(path):
os.unlink(path)
except Exception as e:
print(e)