Example #1
0
 def __contains__(self, term):
     # Check if the term is in the dictionary.
     # If the term is not in the dictionary, check the classifiers.
     term = self._normalize(term)
     if dict.__contains__(self, term):
         return True
     for classifier in self.classifiers:
         if classifier.parents(term) \
         or classifier.children(term):
             return True
     return False
Example #2
0
 def test_lazydict(self):
     # Assert lazy dictionary only has data after one of its methods is called.
     class V(text.lazydict):
         def load(self):
             dict.__setitem__(self, "a", 1)
     v = V()
     self.assertTrue(dict.__len__(v) == 0)
     self.assertTrue(dict.__contains__(v, "a") is False)
     self.assertTrue(len(v), 1)
     self.assertTrue(v["a"] == 1)
     print("pattern.text.lazydict")
Example #3
0
 def __contains__(self, term):
     # Check if the term is in the dictionary.
     # If the term is not in the dictionary, check the classifiers.
     term = self._normalize(term)
     if dict.__contains__(self, term):
         return True
     for classifier in self.classifiers:
         if classifier.parents(term) \
         or classifier.children(term):
             return True
     return False
Example #4
0
    def test_lazydict(self):
        # Assert lazy dictionary only has data after one of its methods is called.
        class V(text.lazydict):
            def load(self):
                dict.__setitem__(self, "a", 1)

        v = V()
        self.assertTrue(dict.__len__(v) == 0)
        self.assertTrue(dict.__contains__(v, "a") is False)
        self.assertTrue(len(v), 1)
        self.assertTrue(v["a"] == 1)
        print("pattern.text.lazydict")
Example #5
0
 def dfs(term, recursive=False, visited={}, **kwargs):
     if term in visited: # Break on cyclic relations.
         return []
     visited[term], a = True, []
     if dict.__contains__(self, term):
         a = list(self[term][1].keys())
     for classifier in self.classifiers:
         a.extend(classifier.children(term, **kwargs) or [])
     if recursive:
         for w in a:
             a += dfs(w, recursive, visited, **kwargs)
     return a
Example #6
0
 def dfs(term, recursive=False, visited={}, **kwargs):
     if term in visited:  # Break on cyclic relations.
         return []
     visited[term], a = True, []
     if dict.__contains__(self, term):
         a = list(self[term][1].keys())
     for classifier in self.classifiers:
         a.extend(classifier.children(term, **kwargs) or [])
     if recursive:
         for w in a:
             a += dfs(w, recursive, visited, **kwargs)
     return a
Example #7
0
 def classify(self, term, **kwargs):
     """ Returns the (most recently added) semantic type for the given term ("many" => "quantity").
         If the term is not in the dictionary, try Taxonomy.classifiers.
     """
     term = self._normalize(term)
     if dict.__contains__(self, term):
         return list(self[term][0].keys())[-1]
     # If the term is not in the dictionary, check the classifiers.
     # Returns the first term in the list returned by a classifier.
     for classifier in self.classifiers:
         # **kwargs are useful if the classifier requests extra information,
         # for example the part-of-speech tag.
         v = classifier.parents(term, **kwargs)
         if v:
             return v[0]
Example #8
0
 def classify(self, term, **kwargs):
     """ Returns the (most recently added) semantic type for the given term ("many" => "quantity").
         If the term is not in the dictionary, try Taxonomy.classifiers.
     """
     term = self._normalize(term)
     if dict.__contains__(self, term):
         return list(self[term][0].keys())[-1]
     # If the term is not in the dictionary, check the classifiers.
     # Returns the first term in the list returned by a classifier.
     for classifier in self.classifiers:
         # **kwargs are useful if the classifier requests extra information,
         # for example the part-of-speech tag.
         v = classifier.parents(term, **kwargs)
         if v:
             return v[0]
Example #9
0
 def __contains__(self, k):
     try:
         return hasattr(self, k) or dict.__contains__(self, k)
     except:
         return False
Example #10
0
 def remove(self, term):
     if dict.__contains__(self, term):
         for w in self.parents(term):
             self[w][1].pop(term)
         dict.pop(self, term)
Example #11
0
 def remove(self, term):
     if dict.__contains__(self, term):
         for w in self.parents(term):
             self[w][1].pop(term)
         dict.pop(self, term)