From ec0dd2796008462270c63e49e457874fc12d0ca1 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Fri, 9 Apr 2004 16:29:16 +0000 Subject: [PATCH] All in the name of case insensitivity. --- plugins/WordStats.py | 30 ++++++++++++++---------------- src/utils.py | 7 +++++++ 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/plugins/WordStats.py b/plugins/WordStats.py index 3e714ee42..a8c8c6fde 100644 --- a/plugins/WordStats.py +++ b/plugins/WordStats.py @@ -59,6 +59,8 @@ conf.registerChannelValue(conf.supybot.plugins.WordStats, nonAlphaNumeric = filter(lambda s: not s.isalnum(), string.ascii) +WordDict = utils.InsensitivePreservingDict + class WordStatsDB(plugins.ChannelUserDB): def __init__(self, *args, **kwargs): self.channelWords = ircutils.IrcDict() @@ -71,19 +73,18 @@ class WordStatsDB(plugins.ChannelUserDB): return L def deserialize(self, channel, id, L): - d = {} + d = WordDict() for s in L: (word, count) = s.split(':') count = int(count) d[word] = count if channel not in self.channelWords: - self.channelWords[channel] = {} + self.channelWords[channel] = WordDict() self.channelWords[channel].setdefault(word, 0) self.channelWords[channel][word] += count return d def getWordCount(self, channel, id, word): - word = word.lower() return self[channel, id][word] def getUserWordCounts(self, channel, id): @@ -102,14 +103,13 @@ class WordStatsDB(plugins.ChannelUserDB): def getNumUsers(self, channel): i = 0 for ((chan, _), _) in self.iteritems(): - if chan == channel: + if ircutils.nickEqual(chan, channel): i += 1 return i def getTopUsers(self, channel, word, n): - word = word.lower() L = [(id, d[word]) for ((chan, id), d) in self.iteritems() - if channel == chan and word in d] + if ircutils.nickEqual(channel, chan) and word in d] utils.sortBy(lambda (_, i): i, L) L = L[-n:] L.reverse() @@ -125,21 +125,19 @@ class WordStatsDB(plugins.ChannelUserDB): raise KeyError def addWord(self, channel, word): - word = word.lower() if channel not in self.channelWords: self.channelWords[channel] = {} self.channelWords[channel][word] = 0 for ((chan, id), d) in self.iteritems(): - if channel == chan: + if ircutils.nickEqual(chan, channel): if word not in d: d[word] = 0 def delWord(self, channel, word): - word = word.lower() if word in self.channelWords[channel]: del self.channelWords[channel][word] for ((chan, id), d) in self.iteritems(): - if channel == chan: + if ircutils.nickEqual(chan, channel): if word in d: del d[word] @@ -157,6 +155,7 @@ class WordStatsDB(plugins.ChannelUserDB): if channel not in self.channelWords: self.channelWords[channel] = {} for word in self.channelWords[channel]: + word = word.lower() for msgword in msgwords: if msgword == word: self.channelWords[channel][word] += 1 @@ -196,7 +195,6 @@ class WordStats(callbacks.Privmsg): if word.strip(nonAlphaNumeric) != word: irc.error(' must not contain non-alphanumeric chars.') return - word = word.lower() self.db.addWord(channel, word) irc.replySuccess() @@ -265,7 +263,7 @@ class WordStats(callbacks.Privmsg): irc.reply(s) else: irc.error('%s has never said %r.' % (user, word)) - elif arg1 in self.db.getWords(channel): + elif arg1 in WordDict.fromkeys(self.db.getWords(channel)): word = arg1 total = self.db.getTotalWordCount(channel, word) n = self.registryValue('rankingDisplay', channel) @@ -283,11 +281,11 @@ class WordStats(callbacks.Privmsg): rank = None try: username = ircdb.users.getUser(userid).name + L.append('%s: %s' % (username, count)) except KeyError: - irc.error('Odd, I have a user in my WordStats database ' - 'that doesn\'t exist in the user database.') - return - L.append('%s: %s' % (username, count)) + self.log.warning('Odd, I have a user in my WordStats ' + 'database that doesn\'t exist in my ' + 'user database: #%s.', userid) ret = 'Top %s (out of a total of %s seen):' % \ (utils.nItems(ers, len(L)), utils.nItems(repr(word), total)) users = self.db.getNumUsers(channel) diff --git a/src/utils.py b/src/utils.py index 5070ea3b7..3f3cf0b3b 100755 --- a/src/utils.py +++ b/src/utils.py @@ -591,6 +591,13 @@ class InsensitivePreservingDict(UserDict.DictMixin, object): if dict is not None: self.update(dict) + def fromkeys(cls, keys, s=None, dict=None, key=None): + d = cls(dict=dict, key=key) + for key in keys: + d[key] = s + return d + fromkeys = classmethod(fromkeys) + def __getitem__(self, k): return self.data[self.key(k)][1]