Skip to content

Commit 38fce72

Browse files
committed
default count in increment_count to 1
fixes #26
1 parent e0499f0 commit 38fce72

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ And here is an example with a two-level hierarchy:
8686
>>> c.increment_count("2.1", "foo", 2)
8787
>>> c.increment_count("2.2", "foo", 2)
8888
>>> c.increment_count("2.1", "bar", 1)
89-
>>> c.increment_count("2.2", "bar", 1)
89+
>>> c.increment_count("2.2", "bar")
9090
>>> c.get_counts()["foo"]
9191
10
9292
>>> c.get_counts()["bar"]
@@ -96,6 +96,8 @@ And here is an example with a two-level hierarchy:
9696

9797
```
9898

99+
Note that if the `count` is `1` you can omit it.
100+
99101
You can **prune** a HTDM to just `n` levels with the method `prune(n)`.
100102

101103
You can iterate over the document-term counts at the leaves of the HTDM with the method `leaf_entries()` (this returns a generator yielding `(document_address, term, count)` tuples). This is effectively a traditional TDM (the document IDs will still reflect the hierarchy but the aggregate counts aren't present).

termdoc/htdm.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_or_create_counter(self, depth):
2626
self.counters.append(collections.defaultdict(collections.Counter))
2727
return self.counters[depth]
2828

29-
def increment_count(self, address, term, count):
29+
def increment_count(self, address, term, count=1):
3030
first = True
3131
while True:
3232
depth = self.depth(address)

tests.py

+13
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,18 @@ def test_multi_level_error(self):
324324
self.assertEqual(c.get_counts("2")["foo"], 4)
325325

326326

327+
class Test3(unittest.TestCase):
328+
def test_two_arg_increment_count(self):
329+
import termdoc
330+
331+
c = termdoc.HTDM()
332+
c.increment_count("1", "foo")
333+
c.increment_count("1", "bar", 2)
334+
c.increment_count("2", "foo", 2)
335+
c.increment_count("2", "bar")
336+
self.assertEqual(c.get_counts()["foo"], 3)
337+
self.assertEqual(c.get_counts()["bar"], 3)
338+
339+
327340
if __name__ == "__main__":
328341
unittest.main()

0 commit comments

Comments
 (0)