示例#1
0
文件: search.py 项目: clips/pattern
 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
示例#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")
示例#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
示例#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")
示例#5
0
文件: search.py 项目: clips/pattern
 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
示例#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
示例#7
0
文件: search.py 项目: clips/pattern
 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]
示例#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]
示例#9
0
 def __contains__(self, k):
     try:
         return hasattr(self, k) or dict.__contains__(self, k)
     except:
         return False
示例#10
0
文件: search.py 项目: clips/pattern
 def remove(self, term):
     if dict.__contains__(self, term):
         for w in self.parents(term):
             self[w][1].pop(term)
         dict.pop(self, term)
示例#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)