Пример #1
0
 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))
Пример #2
0
 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)
Пример #3
0
    def setUpTestData(cls):
        """Note, this requires the full pronoun complement."""
        cls.editor = User.objects.create_user('admin', '*****@*****.**',
                                              "test")
        cls.lang = Language.objects.create(language='A',
                                           slug='langa',
                                           information='i.1',
                                           classification='a, b',
                                           isocode='aaa',
                                           editor=cls.editor)
        cls.source = Source.objects.create(year="1991",
                                           author='Smith',
                                           slug='Smith1991',
                                           reference='S2',
                                           comment='c1',
                                           editor=cls.editor)

        # Load all pronoun combinations
        for i, p in enumerate(PronounCombinations, 1):
            # create word
            w = short_repr_row(p)
            this_word = Word.objects.create(word=w,
                                            slug=w.lower().replace(" ", "_"),
                                            full=full_repr_row(p),
                                            editor=cls.editor)
            this_word.save()

            gender = p['gender'] if p['gender'] is None else p['gender'][0]

            ptype = PronounType.objects.create(alignment=p['alignment'][0],
                                               person=p['person'][0],
                                               number=p['number'][0],
                                               gender=gender,
                                               sequence=i,
                                               word=this_word,
                                               editor=cls.editor)
            ptype.save()

        # change the paradigm, man
        cls.fullpdm = Paradigm.objects.create(language=cls.lang,
                                              source=cls.source,
                                              editor=cls.editor,
                                              comment="full paradigm")
        cls.fullpdm.save()

        # add some lexicon...
        for p in cls.fullpdm.pronoun_set.all():
            lex = Lexicon.objects.create(editor=cls.editor,
                                         language=cls.lang,
                                         source=cls.source,
                                         word=p.pronountype.word,
                                         entry='%d' % p.id)
            lex.save()
            p.entries.add(lex)

        cls.url = reverse('pronouns:edit',
                          kwargs={'paradigm_id': cls.fullpdm.id})
        cls.detail_url = reverse('pronouns:detail',
                                 kwargs={'paradigm_id': cls.fullpdm.id})
 def setUp(self):
     """Note, this requires the full pronoun complement."""
     self.editor = User.objects.create_user('admin', '*****@*****.**', "test")
     self.lang = Language.objects.create(language='A', slug='langa', 
                                          information='i.1', 
                                          classification='a, b',
                                          isocode='aaa', editor=self.editor)
     self.source = Source.objects.create(year="1991", author='Smith', 
                              slug='Smith1991', reference='S2',
                              comment='c1', editor=self.editor)
     
     # Load all pronoun combinations
     for i, p in enumerate(PronounCombinations, 1):
         # create word
         w = short_repr_row(p)
         this_word = Word.objects.create(
             word = w,
             slug = w.lower().replace(" ", "_"),
             full = full_repr_row(p),
             editor=self.editor
         )
         this_word.save()
         
         if p['gender'] is None:
             gender = None
         else:
             gender = p['gender'][0]
         
         ptype = PronounType.objects.create(
             alignment = p['alignment'][0],
             person = p['person'][0],
             number = p['number'][0],
             gender = gender,
             sequence = i, 
             word=this_word,
             editor=self.editor
         )
         ptype.save()
         
     # change the paradigm, man
     self.fullpdm = Paradigm.objects.create(language=self.lang, 
                              source=self.source, 
                              editor=self.editor,
                              comment="full paradigm")
     self.fullpdm.save()
     assert self.fullpdm.pronoun_set.count() == len(PronounCombinations), \
         "Expected %d pronouns not %d" % (len(PronounCombinations), self.fullpdm.pronoun_set.count())
     
     # add some lexicon... 
     for p in self.fullpdm.pronoun_set.all():
         lex = Lexicon.objects.create(
             editor=self.editor,
             language=self.lang,
             source=self.source,
             word=p.pronountype.word,
             entry='%d' % p.id
         )
         lex.save()
         p.entries.add(lex)
     
     self.url = reverse('pronouns:edit', kwargs={'paradigm_id': self.fullpdm.id})
     self.client = Client()
     self.client.login(username='******', password='******')
     self.response = self.client.get(self.url)