-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputHandler.py
136 lines (115 loc) · 4.26 KB
/
InputHandler.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""Usage:
wppAnalysis.py run <textfile> [--freqAnalysis=<freqAnalysis>] [--stopwords=<stopWords>] [--Iramuteq=<Iramuteq>]
"""
from docopt import docopt
from util import load_stop_words, load_Iramuteq
import abc
import re
import sys
import csv
from nltk.util import ngrams
class AbstractClass(metaclass=abc.ABCMeta):
def execution_steps(self, conversationPath):
self.conversation_body_of_text = self._load_input(conversationPath)
self.conversation_body_of_text = self._clean_data(
self.conversation_body_of_text)
self.conversation_body_of_text = self._remove_stop_words(
self.conversation_body_of_text)
self.conversation_body_of_text = self._apply_Iramuteq(
self.conversation_body_of_text)
self.freq_analysis = self._freq_analysis(self.conversation_body_of_text)
self._save_freq_to_csv(self.freq_analysis)
print(self._generate_ngrams(self.conversation_body_of_text, 2))
return self.freq_analysis
@abc.abstractmethod
def _load_input(conversationPath):
pass
@abc.abstractmethod
def _clean_data(conversationPath):
pass
@abc.abstractmethod
def _remove_stop_words(conversationBodyOfText):
pass
@abc.abstractmethod
def _apply_Iramuteq(conversationBodyOfText):
pass
@abc.abstractmethod
def _freq_analysis(conversationBodyOfText):
pass
@abc.abstractmethod
def _generate_ngrams(conversationBodyOfText, n):
pass
@abc.abstractmethod
def _count_words(conversationBodyOfText):
pass
class WhatsappConversationAnalysis(AbstractClass):
def _load_input(self, conversationPath):
return open(conversationPath, 'r').read()
def _clean_data(self, conversation):
regex_info_message = r"\[.*?\].{3,4}\d{2}.\d{2}.\d{4,5}.\d{4}.:*"
lines = conversation.splitlines()
result = ""
for line in lines:
line = line.strip()
if line:
result += re.sub(regex_info_message, "", line.lower()) + "\n"
return result
def _remove_stop_words(self, conversation):
stop_words = load_stop_words()
result = ""
for word in conversation.split():
if word not in stop_words:
result += word + " "
return result
def _apply_Iramuteq(self, conversation):
if not conversation:
return "please input a body of text in order to apply the Iramuteq filter"
sys.exit(0)
iramuteq_map = load_Iramuteq("word-equivalent")
new_body_text = ""
for word in conversation.split():
if word in iramuteq_map:
previous_value_of_word = word
word = iramuteq_map[word]
new_body_text += word + " "
return new_body_text
def _freq_analysis(self, conversation):
if not conversation:
return "please input a body of text in order to run the frequency analysis"
sys.exit(0)
freq_map = dict()
for word in conversation.split():
if word in freq_map:
freq_map[word] += 1
else:
freq_map[word] = 1
return freq_map
def _save_freq_to_csv(self, freqDict):
sorted_freqDict = sorted(freqDict.items(), key=lambda kv: -kv[1])
with open('freq.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in sorted_freqDict:
writer.writerow([key, value])
def _generate_ngrams(self, s, n):
# Convert to lowercases
s = s.lower()
# Replace all none alphanumeric characters with spaces
s = re.sub(r'[^a-zA-Z0-9\s]', ' ', s)
# Break sentence in the token, remove empty tokens
tokens = [token for token in s.split(" ") if token != ""]
n_grams = list(ngrams(tokens, 2))
return n_grams
def _count_words(self, conversation):
counter = 0
for words in conversation.split():
counter += 1
return counter
def Init(arguments):
Wpp = WhatsappConversationAnalysis()
Wpp.execution_steps(arguments['<textfile>'])
if __name__ == '__main__':
arguments = docopt(__doc__, version='0.1.1rc')
if arguments['<textfile>']:
Init(arguments)
else:
print("You should input a textfile")