Files
tilde-projects/Code/python/chatcloud.py
T

65 lines
2.5 KiB
Python
Raw Normal View History

2015-08-24 10:04:57 -05:00
#!/usr/bin/python
import fileinput
import json
import time
import calendar
import re
import shutil
2018-01-24 20:51:54 +00:00
logfile = "/home/archangelic/irc/log"
2018-10-05 16:02:38 -04:00
# logfile = "/home/jumblesale/Code/irc/log"
2015-08-24 10:04:57 -05:00
outfile = "/home/krowbar/logs/chatcloud.json"
2018-10-05 16:02:38 -04:00
# outfile = "/home/krowbar/logs/chatcloud_2016_10.json"
2017-03-30 21:39:44 +00:00
2015-10-13 08:23:17 -05:00
bannedUsersFile = "/home/krowbar/Code/python/bannedUsers"
2015-08-24 10:04:57 -05:00
bannedWordsFile = "/home/krowbar/Code/python/bannedWords"
2018-10-05 16:02:38 -04:00
wordData = {} # keyed by "word" that contains a count
# we only care about recent chats, let's say for the past sixteen hours
2015-10-13 08:23:17 -05:00
timeTo = calendar.timegm(time.gmtime())
2018-10-05 16:02:38 -04:00
# timeTo = calendar.timegm(time.strptime("1 Nov 16", "%d %b %y"))
2017-03-30 21:39:44 +00:00
2015-10-13 08:23:17 -05:00
timeCutoff = timeTo - (16 * 60 * 60)
2018-10-05 16:02:38 -04:00
# timeCutoff = calendar.timegm(time.strptime("1 Oct 16", "%d %b %y"))
print(
"Generating word cloud based off words from "
+ str(timeCutoff)
+ " to "
+ str(timeTo)
)
minOccurance = 3 # we'll have to reduce the minOccurances if we reduce the timeCutoff
minLength = 3 # number of letters long
2015-08-24 10:04:57 -05:00
bannedWords = open(bannedWordsFile).read().splitlines()
2015-10-13 08:23:17 -05:00
bannedUsers = open(bannedUsersFile).read().splitlines()
2015-08-24 10:04:57 -05:00
with open(logfile, "r") as log:
for line in log:
try:
time, user, message = line.split("\t", 3)
time = int(time)
except ValueError:
2018-10-05 16:02:38 -04:00
continue # There are some bad lines in the log file that we'll ignore if we can't parse
2015-10-13 08:23:17 -05:00
if user in bannedUsers:
2018-10-05 16:02:38 -04:00
continue # We don't care what they say
2015-10-13 08:23:17 -05:00
if time >= timeCutoff and time <= timeTo:
2018-10-05 16:02:38 -04:00
# print "Processing line from " + user + " at " + str(time)
for word in (
re.sub("['\"\`\/\\;:,.?!*&^\-()<>\{\}|_\[\]0-9]", " ", message)
.lower()
.split()
):
# changing symbols into spaces instead of stripping them avoids compounded words
2015-10-13 08:23:17 -05:00
if len(word) < minLength or word in bannedWords:
2018-10-05 16:02:38 -04:00
# print "Rejecting " + word
2015-08-24 10:04:57 -05:00
continue
2018-10-05 16:02:38 -04:00
# if the word already exists in the list
2015-08-24 10:04:57 -05:00
if word in wordData:
wordData[word] += 1
2018-10-05 16:02:38 -04:00
else: # if they are new
2015-08-24 10:04:57 -05:00
wordData[word] = 1
2018-10-05 16:02:38 -04:00
# print "Added word: " + word
wordData = {i: wordData[i] for i in wordData if wordData[i] >= minOccurance}
if len(wordData) == 0:
2015-10-13 08:23:17 -05:00
wordData = {"NOTHING": 1, "INTERESTING": 1, "TODAY": 1}
2015-08-24 10:04:57 -05:00
with open(outfile + ".tmp", "w") as tmpFile:
tmpFile.write(json.dumps(wordData))
shutil.move(outfile + ".tmp", outfile)