コード例 #1
0
 def __init__(self, root_synset):
     super(WordnetPuzzleGenerator, self).__init__()
     self.root_synset = wn.synset(root_synset)
     self.synset_gen = GetRandomSynset(root_synset)
     self.vocab = self._build_vocab()
     self.specificity_lb = 10
     self.specificity_ub = 5000
コード例 #2
0
class WordnetPuzzleGenerator(PuzzleGenerator):
    
    def __init__(self, root_synset):
        super(WordnetPuzzleGenerator, self).__init__()
        self.root_synset = wn.synset(root_synset)
        self.synset_gen = GetRandomSynset(root_synset)
        self.vocab = self._build_vocab()
        self.specificity_lb = 10
        self.specificity_ub = 5000
        
    def _build_vocab(self):
        words = sorted(list(get_all_lemmas_from_sense(self.root_synset)))
        word_to_ix = dict([(v, k) for (k,v) in enumerate(words)])
        print("vocab size: {}".format(len(word_to_ix)))
        return word_to_ix
    
    def get_vocab(self):
        return self.vocab
    
    def reset_root(self, root_synset):
        self.root_synset = wn.synset(root_synset)
        self.synset_gen = GetRandomSynset(root_synset)        
    
    def generate(self):
        root = self.synset_gen.random_synset_with_specificity(self.specificity_lb, 
                                                              self.specificity_ub)
        while root == self.root_synset:
            root = self.synset_gen.random_synset_with_specificity(self.specificity_lb,
                                                                  self.specificity_ub)
        hyps = get_all_lemmas_from_sense(root) # children?
        puzzle = random.sample(hyps, 4)
        random_hyp = self.synset_gen.random_non_hyponym(root)
        random_word = random.choice(list(get_all_lemmas_from_sense(random_hyp)))
        puzzle.append(random_word)
        (w1, w2, w3, w4, w5) = puzzle#[normalize_lemma(random.choice(s.lemmas()).name()) for s in puzzle]
        result = [(str(w1), 0), (str(w2), 0), (str(w3), 0), 
                  (str(w4), 0), (str(w5), 1)]
        random.shuffle(result)
        xyz = tuple([i for (i,_) in result])
        onehot = [j for (_,j) in result]
        return (xyz, onehot.index(1))
コード例 #3
0
class TestWordnet(unittest.TestCase):
    def setUp(self):
        self.grs = GetRandomSynset('bird.n.1')

    def test_lowest_common_ancestor(self):
        ancestor = find_lowest_common_ancestor(['car', 'tank', 'bike'])
        assert ancestor == (120, wn.synset('self-propelled_vehicle.n.01'))

    def test_random_synset_with_specificity(self):
        assert self.grs.random_synset_with_specificity(
            120, 140) == wn.synset('wading_bird.n.01')

    """
コード例 #4
0
 def reset_root(self, root_synset):
     self.root_synset = wn.synset(root_synset)
     self.synset_gen = GetRandomSynset(root_synset)
コード例 #5
0
 def setUp(self):
     self.grs = GetRandomSynset('bird.n.1')