Skip to content

Commit

Permalink
some minimal tests; found and fixed a bias bug in analysis.Analyzer.
Browse files Browse the repository at this point in the history
  • Loading branch information
eastein committed Dec 28, 2011
1 parent 52302f7 commit 3a14e80
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -38,3 +38,10 @@ The arduino sketch listens on the serial input and also operates the blinkers.
# TODO

* usage for both programs

<A name="toc1-41" title="Tests" />
# Tests

To run unit tests, use:

nosetests -vv tests
6 changes: 6 additions & 0 deletions README.txt
Expand Up @@ -30,3 +30,9 @@ The arduino sketch listens on the serial input and also operates the blinkers.
# TODO

* usage for both programs

# Tests

To run unit tests, use:

nosetests -vv tests
4 changes: 4 additions & 0 deletions analysis.py
@@ -1,5 +1,6 @@
import wordlists
import re
import random

MATCH_CAT = re.compile('^L_(.*)$')

Expand All @@ -19,6 +20,9 @@ def __init__(self) :

def categorize(self, line) :
words = line.lower().split(' ')
# in order to not bias towards the beginning of the sentence in
# tie situations, we should randomize the word analysis order.
random.shuffle(words)
catcounts = {}
maxc = 0
selected_cat = None
Expand Down
30 changes: 30 additions & 0 deletions tests/analysis_tests.py
@@ -0,0 +1,30 @@
import analysis
import unittest

class SubjectTests(unittest.TestCase) :
def setUp(self) :
self.analyzer = analysis.Analyzer()

def test_nosubject(self) :
self.assertEqual(None, self.analyzer.categorize("I the and then. The. Yes."))

def test_specific_subject(self) :
self.assertEqual("COMPUTERS", self.analyzer.categorize("Use python. Problem solved."))

def test_random_subject(self) :
n_outdoors = 0
n_computers = 0
for i in xrange(10000) :
cat = self.analyzer.categorize("Use python. Ski later.")
if cat == "COMPUTERS" :
n_computers += 1
elif cat == "OUTDOORS" :
n_outdoors += 1
else :
self.fail("did not expect category %s" % cat)

if n_outdoors > 0 and n_computers > 0 :
break

self.assertTrue(n_outdoors > 0)
self.assertTrue(n_computers > 0)
4 changes: 3 additions & 1 deletion trending.py
Expand Up @@ -53,7 +53,9 @@ def report(self, sec) :
max_cat = 0
category = None
catcnt = {}
for chatter in chatters.values() :
randomized_chatters = chatters.values()
random.shuffle(randomized_chatters)
for chatter in randomized_chatters :
for cat in chatter.categories :
catcnt.setdefault(cat, 0)
catcnt[cat] += 1
Expand Down

0 comments on commit 3a14e80

Please sign in to comment.