def test_prefill(self): # make sure the correct number of pronouns is there.. assert self.pdm.pronoun_set.count() == len(PronounType._generate_all_combinations()) # check the pronouns themselves... for comb in PronounType._generate_all_combinations(): queryset = Pronoun.objects.filter(pronountype=comb) assert len(queryset) == 1, 'Got {0} not one'.format(len(queryset))
def test_prefill(self): # make sure the correct number of pronouns is there.. combs = PronounType._generate_all_combinations() assert self.pdm.pronoun_set.count() == len(combs) # check the pronouns themselves... for comb in PronounType._generate_all_combinations(): count = Pronoun.objects.filter(pronountype=comb).count() assert count == 1, 'Got %d not one' % count
def test_partial_prefill(self): # create new pdm pdm = Paradigm.objects.create( language=self.lang, source=self.source, editor=self.editor, comment="test_partial_prefill" ) pdm._prefill_pronouns() # we should have a full complement. combs = PronounType._generate_all_combinations() assert pdm.pronoun_set.count() == len(combs) # Let's delete some... for pron in pdm.pronoun_set.all(): if pron.pronountype.person == '2': pron.delete() else: # modify the stored entries so we can identify them later. pron.entries.add(Lexicon.objects.create( editor=self.editor, source=self.source, language=self.lang, word=self.word, entry="old" )) pron.save() # how many should we have deleted combs = PronounType._generate_all_combinations() missing = [_ for _ in combs if _.person == '2'] assert len(missing) == 1 assert pdm.pronoun_set.count() == (len(combs) - len(missing)) # re-run prefill pdm._prefill_pronouns() # we should now have a full complement again. combs = PronounType._generate_all_combinations() assert pdm.pronoun_set.count() == len(combs) for pron in pdm.pronoun_set.all(): if pron.pronountype.person == '2': assert pron.entries.count() == 0 else: assert pron.entries.count() == 1 assert pron.entries.all()[0].entry == 'old'
def test_paradigm_creates_pronouns(self): count = Pronoun.objects.count() response = self.client.post(self.url, { 'language': self.lang.id, 'source': self.source.id, 'comment': 'foo' }, follow=True) self.assertEqual(Pronoun.objects.count(), count+len(PronounType._generate_all_combinations()))
def handle(self, *args, **options): pdm = Paradigm.objects.get(pk=args[0]) pf = PronounFinder() labels = [short_repr_row(p) for p in PronounType._generate_all_combinations()] data = {} for p1 in pdm.pronoun_set.all(): p1_id = short_repr_row(p1) for p2 in pdm.pronoun_set.all(): p2_id = short_repr_row(p2) data[(p1_id, p2_id)] = pf.compare(p1.form, p2.form) rows = [] for p1 in labels: row = [] for p2 in labels: row.append(data.get((p1,p2), 0.0)) rows.append(row) data = np.array(rows) # start plotting fig, axes = plt.subplots() heatmap = axes.pcolor(data, cmap=plt.cm.Blues) # put the major ticks at the middle of each cell axes.set_xticks(np.arange(data.shape[0])+0.5, minor=False) axes.set_yticks(np.arange(data.shape[1])+0.5, minor=False) # want a more natural, table-like display axes.invert_yaxis() # set labels axes.set_xticklabels(labels, minor=False, rotation=90, fontsize=5) axes.set_yticklabels(labels, minor=False, fontsize=5) plt.tick_params(direction="out") plt.tick_params(right="off") plt.tick_params(top="off") plt.suptitle(pdm.language) plt.savefig("%s-%d.png" % (pdm.language.slug, pdm.id)) print("Written to %s-%d.png" % (pdm.language.slug, pdm.id)) cols = ['-'] cols.extend(labels) x = PrettyTable(cols) for i, row in enumerate(data): row = row.round(3) newrow = [labels[i]] for r in row: newrow.append(round(r, 3)) x.add_row(newrow) with open("%s-%d.txt" % (pdm.language.slug, pdm.id), 'w+') as handle: handle.write(x.get_string()) print("Written to %s-%d.txt" % (pdm.language.slug, pdm.id))
def test_obj(self): pt = PronounType(number='sg', alignment="A", person="1", gender=None, sequence=10, editor=self.editor, word=self.word) p = Pronoun(paradigm=self.pdm, editor=self.editor, pronountype=pt) assert full_repr_row(p) == "1st (excl) Person Singular"
def test_paradigm_create(self): pdm = Paradigm.objects.create(language=self.lang, source=self.source, editor=self.editor, comment="test") pdm._prefill_pronouns() # make sure the correct number of pronouns is there.. assert pdm.pronoun_set.count() == 2 # check the pronouns themselves... for comb in PronounType._generate_all_combinations(): queryset = Pronoun.objects.filter(pronountype=comb) assert len(queryset) == 1, 'Got {0} not one'.format(len(queryset))
def handle(self, *args, **options): pdm = Paradigm.objects.get(pk=args[0]) pf = PronounFinder() labels = [short_repr_row(p) for p in PronounType._generate_all_combinations()] data = {} for p1 in pdm.pronoun_set.all(): p1_id = short_repr_row(p1) for p2 in pdm.pronoun_set.all(): p2_id = short_repr_row(p2) if len(p1.form) == 0 or len(p2.form) == 0: continue part1 = u"%s (%s)" % (p1_id, p1.form) part2 = u"%s (%s)" % (p2_id, p2.form) print part1.ljust(20), '::', part2.ljust(20), "%0.2f" % pf.compare(p1.form, p2.form)
def add_pronoun_table(pronoun_set, filter_empty_rows=True): """Construct a table for the given pronoun set `filter_empty_rows` - leave out the rows that are empty. """ # loop over the pronouns we've been given and fill a table of the cells. cells = {} for p in pronoun_set: label = full_repr_row(p) # get row or set it to (A, None), (S, None), (O, None), (P, None) # i.e. empty placeholders for each different ALIGNMENT cells[label] = cells.get(label, dict(zip( [_[0] for _ in ALIGNMENT_CHOICES], [None for _ in ALIGNMENT_CHOICES] )) ) # Save the pronoun into the right row/alignment cell. cells[label][p.pronountype.alignment] = p # Now do the sorting of the table *rows* pronoun_rows = [] # Sort ptype_rows = PronounType._generate_all_rows() for row in ptype_rows: wanted_label = full_repr_row(row) found_row = False # go through each label in the cells e.g. (1st person singular...etc) for label in cells: if wanted_label == label: found_row = True # Ignore empty rows? # only add this row if at LEAST one cell has something in it. if filter_empty_rows: non_zero = 0 for cell, value in cells[label].items(): if value is not None and len(value.entries.all()) > 0: non_zero += 1 if non_zero: # at least one cell is not empty pronoun_rows.append((label, cells[label])) else: pronoun_rows.append((label, cells[label])) if not found_row: raise ValueError( "Unable to find expected row for Paradigm: %s" "- probably need to run _prefill_pronouns()" % wanted_label ) if not filter_empty_rows: assert len(pronoun_rows) == len(ptype_rows) return pronoun_rows
def test_partial_prefill(self): # we should have a full complement. assert self.pdm.pronoun_set.count() == len(PronounType._generate_all_combinations()) # Let's delete some... for pron in self.pdm.pronoun_set.all(): if pron.pronountype.person == '2': pron.delete() else: # modify the stored entries so we can identify them later. pron.entries.add(Lexicon.objects.create( editor=self.editor, source=self.source, language=self.lang, word=self.word, entry="old" )) pron.save() # how many should we have deleted missing = [_ for _ in PronounType ._generate_all_combinations() if _.person == '2'] assert len(missing) == 1 assert self.pdm.pronoun_set.count() == (len(PronounType._generate_all_combinations()) - len(missing)) # re-run prefill self.pdm._prefill_pronouns() # we should now have a full complement again. assert self.pdm.pronoun_set.count() == len(PronounType._generate_all_combinations()) for pron in self.pdm.pronoun_set.all(): if pron.pronountype.person == '2': assert pron.entries.count() == 0 else: assert pron.entries.count() == 1 assert pron.entries.all()[0].entry == 'old'
def add_pronoun_table(pronoun_set, filter_empty_rows=True): """Construct a table for the given pronoun set `filter_empty_rows` - leave out the rows that are empty. """ # loop over the pronouns we've been given and fill a table of the cells. cells = {} for p in pronoun_set: label = full_repr_row(p) # get row or set it to (A, None), (S, None), (O, None), (P, None) # i.e. empty placeholders for each different ALIGNMENT cells[label] = cells.get(label, dict(zip([_[0] for _ in ALIGNMENT_CHOICES], [None for _ in ALIGNMENT_CHOICES])) ) # Save the pronoun into the right row/alignment cell. cells[label][p.pronountype.alignment] = p # Now do the sorting of the table *rows* pronoun_rows = [] # Sort ptype_rows = PronounType._generate_all_rows() for row in ptype_rows: wanted_label = full_repr_row(row) found_row = False # go through each label in the cells e.g. (1st person singular...etc) for label in cells: if wanted_label == label: found_row = True # Ignore empty rows? # only add this row if at LEAST one cell has something in it. if filter_empty_rows: non_zero = 0 for cell, value in cells[label].items(): if value is not None and len(value.entries.all()) > 0: non_zero += 1 if non_zero: # at least one cell is not empty pronoun_rows.append((label, cells[label])) else: pronoun_rows.append((label, cells[label])) assert found_row, \ "Unable to find expected row for Paradigm: %s - probably need to run _prefill_pronouns()" % wanted_label if not filter_empty_rows: assert len(pronoun_rows) == len(ptype_rows) return pronoun_rows
def test_no_errors(self): for row in PronounType._generate_all_combinations(): full_repr_row(row)
def test_generate_all_combinations(self): combinations = PronounType._generate_all_combinations() assert combinations[0] == PronounType.objects.filter(person=1).get() assert combinations[1] == PronounType.objects.filter(person=3).get()
def test_generate_all_combinations(self): combinations = PronounType._generate_all_combinations() assert combinations[0] == PronounType.objects.filter(person=1).get() assert combinations[1] == PronounType.objects.filter(person=2).get() assert combinations[2] == PronounType.objects.filter(person=3).get()