Skip to content
This repository has been archived by the owner on Sep 11, 2019. It is now read-only.

Commit

Permalink
Add min_length, max_permutations to get_best_permutations().
Browse files Browse the repository at this point in the history
  • Loading branch information
toolness committed Nov 3, 2016
1 parent c1fb44c commit 09b746b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
13 changes: 9 additions & 4 deletions data_capture/templatetags/analyze_contract.py
Expand Up @@ -32,7 +32,7 @@ def powerset(iterable):
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))


def get_best_permutations(vocab, lexemes):
def get_best_permutations(vocab, lexemes, min_length=4, max_permutations=8):
def compare(a, b):
a_len = len(a)
b_len = len(b)
Expand All @@ -45,11 +45,16 @@ def compare(a, b):
def vocab_val(iterable):
return sum([vocab[i] for i in iterable])

permutations = list(powerset(lexemes))
# Remove the first element, as it's the empty set.
permutations = list(powerset(lexemes))[1:]

permutations = list(filter(
lambda x: len(' '.join(x)) >= min_length,
permutations
))
permutations.sort(key=cmp_to_key(compare), reverse=True)

# Remove the last element, as it's the empty set.
return permutations[:-1]
return permutations[:max_permutations]


def get_vocab(cursor, model=Contract, field='search_index', min_ndoc=100):
Expand Down
33 changes: 25 additions & 8 deletions data_capture/tests/test_analyze_contract.py
Expand Up @@ -3,16 +3,33 @@
from ..templatetags.analyze_contract import get_best_permutations


class AnalyzeContractTests(TestCase):
def test_get_best_permutations_works(self):
vocab = {
'junior': 1,
'administrative': 2,
'engineer': 3,
}
VOCAB = {
'junior': 1,
'administrative': 2,
'engineer': 3,
'ii': 4,
}


class GetBestPermutationsTests(TestCase):
def test_min_length_works(self):
self.assertEqual(
get_best_permutations(VOCAB, ['engineer', 'ii'], min_length=3),
[('engineer', 'ii'),
('engineer',)],
)

def test_max_permutations_works(self):
self.assertEqual(
get_best_permutations(VOCAB, ['junior', 'administrative',
'engineer'], max_permutations=2),
[('junior', 'administrative', 'engineer'),
('administrative', 'engineer')]
)

def test_it_works(self):
self.assertEqual(
get_best_permutations(vocab, ['junior', 'administrative',
get_best_permutations(VOCAB, ['junior', 'administrative',
'engineer']),
[('junior', 'administrative', 'engineer'),
('administrative', 'engineer'),
Expand Down

0 comments on commit 09b746b

Please sign in to comment.