repl.it
@NityeshAgarwal/

WhatsappChatAnalyser

Python

No description

fork
loading
Files
  • main.py
  • WhatsApp Chat with Paridhi.txt
main.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import time

TIMEGAP = (24*3600)

def getWordCount(file, users):
    '''
    Implements TASK #1 - Get the word counts of both the lovers
    '''
    file.seek(0)
    count = {users[0]: 0, users[1]: 0}
    responder = None
    for line in file:
        if '- ' + users[0] + ':' in line:
            responder = users[0]
            words = line.split()
            count[responder]+= (len(words)-4)
        elif '- ' + users[1] + ':' in line:
            responder = users[1]
            words = line.split()
            count[responder]+= (len(words)-4)
        else:
            if not responder == None:
                words = line.split()
                count[responder]+= (len(words))
    return count.values()


def getLineCount(file, users):
    '''
    Implements TASK #2 - Get the line counts of both the lovers
    '''
    file.seek(0)
    count = {users[0]: 0, users[1]: 0}
    responder = None
    for line in file:
        if '- ' + users[0] + ':' in line:
            responder = users[0]
            count[responder]+=1
        elif '- ' + users[1] + ':' in line:
            responder = users[1]
            count[responder]+=1
        else:
            if not responder == None:
                count[responder]+= 1
    return count.values()


def getAvgWordCount(file, users):
    '''
    Implements TASK #3 - Get the average word count of both the lovers.
    > Average word count = (Total word count)/(Total line count)
    '''
    wordCount = getWordCount(file, users)
    lineCount = getLineCount(file, users)
    avgWordCount = [nWords/nLines for nWords,nLines in zip(wordCount, lineCount) ]
    return avgWordCount


def addToFirstText(line, firstText_dict, users):
    '''
    Helper function to
    '''
    if '- ' + users[0] + ':' in line:
        text = line.split('- ' + users[0] + ':')[1]
        date = line.split('- ' + users[0] + ':')[0]
        firstText_dict[users[0]].append(date+text)
    elif '- ' + users[1] + ':' in line:
        text = line.split('- ' + users[1] + ':')[1]
        date = line.split('- ' + users[1] + ':')[0]
        firstText_dict[users[1]].append(date+text)


def prettyPrint(firstText_dict, users):
    '''
    Helper function to print pretty
    '''
    print('Paridhi:\n')
    for x in range(len(firstText_dict[users[0]])):
        print(firstText_dict[users[0]][x])
    print('\nNityesh:\n')
    for x in range(len(firstText_dict[users[1]])):
        print(firstText_dict[users[1]][x])


def firstTextCount(file, users):
    '''
    Implements TASK #4 - Get the frequency of first texts for both the lovers
    and show the first texts
    '''
    file.seek(0)
    firstText_dict = {users[0]: [], users[1]: []}
    
    timePrevStr = '07/12/1998, 00:00'
    
    for line in file:
        if '- ' + users[0] + ':' in line or '- ' + users[1] + ':' in line:
            time_str = line.split(' -')[0]
            timePrev = time.strptime(timePrevStr, '%d/%m/%Y, %H:%M')
            timePrev = time.mktime(timePrev)
            timeCurr = time.strptime(time_str, '%d/%m/%Y, %H:%M')
            timeCurr = time.mktime(timeCurr)
            if (timeCurr - timePrev) >= TIMEGAP:
                addToFirstText(line, firstText_dict, users)
            timePrevStr = time_str  
    return firstText_dict


def chatHourPattern(file, users):
    '''
    Implements TASK #5 - Get the chat hour pattern for the two lovers
    '''
    file.seek(0)
    chatHourCount = [0]*24
    for line in file:
        if '- ' + users[0] + ':' in line or '- ' + users[1] + ':' in line:
            time_str = line.split(" -")[0]
            time_curr = time.strptime(time_str, "%d/%m/%Y, %H:%M")
            hour = time_curr.tm_hour
            chatHourCount[hour] += 1
    max_value = max(chatHourCount)
    max_index = chatHourCount.index(max_value)
    return chatHourCount, max_value, max_index


def chatMonthPattern(file, users):
    '''
    Implements TASK #6 - Get the chat month pattern for the two lovers
    '''
    file.seek(0)
    #chatMonthCount = {2017: [0]*12, 2018: [0]*12}
    firstyear, lastyear = getYears(file, users)
    chatMonthCount = {yr: [0]*12 for yr in range(firstyear, lastyear+1)}
    for line in file:
        if '- ' + users[0] + ':' in line or '- ' + users[1] + ':' in line:
            time_str = line.split(" -")[0]
            time_curr = time.strptime(time_str, "%d/%m/%Y, %H:%M")
            chatMonthCount[time_curr.tm_year][(time_curr.tm_mon - 1)] += 1
    print(chatMonthCount)
    max_val = 0
    for yr in chatMonthCount:
	      if max_val<max(chatMonthCount[yr]):
        max_val = max(chatMonthCount[yr])
            max_yr = yr
	  max_month = chatMonthCount[max_yr].index(max_val)
    print(str(max_yr) + ', ' + str(max_month+1) + ':' + str(max_val))


def getTexters(file):

    '''
    Helper function to extract texter names from text file
    '''
    file.seek(0)
    users = []
    for line in file:
      if "-" in line:
        text = line.split(" - ")[1]
        if ":" in text:
          user = text.split(":")[0]
          if user not in users:
            users.append(user)
            if len(users)==2:
              break
    return users


def getYears(file, users):

    '''
    Helper function to extract years from text file
    '''
    file.seek(0)
    years = []
    firstline = file.readline()
    time_str = firstline.split(" -")[0]
    firstyear = time.strptime(time_str, "%d/%m/%Y, %H:%M").tm_year
    file.seek(0)
    for line in reversed(list(file)):
        if '- ' + users[0] + ':' in line or '- ' + users[1] + ':' in line:
            time_str = line.split(" -")[0]
            break
    lastyear = time.strptime(time_str, "%d/%m/%Y, %H:%M").tm_year
    return (firstyear, lastyear)


if __name__ == "__main__":
    with open("WhatsApp Chat with Paridhi.txt", "rt") as file:
        users = getTexters(file)
        print("Word counts: " + str(getWordCount(file, users)))
        print("Line counts: " + str(getLineCount(file, users)))
        print("Average word counts: " + str(getAvgWordCount(file, users)))
        firstText_dict = firstTextCount(file, users)
        print("No. of first texts: ", len(firstText_dict[users[0]]), 
                len(firstText_dict[users[1]]))
        #print(firstText_dict)
        prettyPrint(firstText_dict, users)
        print("Chat hour pattern: " + str(chatHourPattern(file, users)))
        chatMonthPattern(file, users)
?