-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessing.py
58 lines (35 loc) · 1.31 KB
/
preprocessing.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
#!/usr/bin/env python
#Text Preprocessing Module
from petl import values, fieldnames, look, addcolumn
import nltk
#from nltk.tokenize import TweetTokenizer
def tokenise_data(dataset):
tweets_tokenized = []
# Text of tweet is on field 10
tweets = extract_values(dataset, 10)
for tweet in tweets:
t_tokenised = nltk.word_tokenize(tweet)
tweets_tokenized.append(t_tokenised)
token_dataset = add_columns(dataset, 'tweet_tokenized', tweets_tokenized)
return token_dataset
def word_frequency(dataset,text_field):
tweets = extract_values(dataset,text_field)
bag_words = {}
word_frequency = []
for tweet in tweets:
for word in tweet:
if word in bag_words:
bag_words[word] += 1
else:
bag_words[word] = 1
word_frequency.append(bag_words)
bag_words = {}
wfreq_dataset = add_columns(dataset, 'word_frequency', word_frequency)
return wfreq_dataset
def add_columns(dataset, column_name, column_data):
new_dataset = addcolumn(dataset, column_name, column_data)
return new_dataset
def extract_values(dataset, field_position):
fields = list(fieldnames(dataset))
field_values = values(dataset, field_position)
return field_values