From c2411d9df0249d67fe0045f1000a486b8dca99c5 Mon Sep 17 00:00:00 2001 From: Rob Speer Date: Fri, 30 Sep 2011 15:47:59 -0400 Subject: [PATCH] fixed errors in graph.get_or_create_assertion, added test case --- conceptnet5/graph.py | 19 +++++++++++++++---- pylintrc | 2 +- test/test_assertions.py | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/conceptnet5/graph.py b/conceptnet5/graph.py index 9b24f57b..5701aa11 100644 --- a/conceptnet5/graph.py +++ b/conceptnet5/graph.py @@ -46,10 +46,10 @@ def _list_nodes_and_uris(self, input_list): uris = [] nodes = [] for index, node_uri in enumerate(input_list): - if isinstance(node_uri,Node): + if isinstance(node_uri, Node): uris.append(node_uri['uri']) nodes.append(node_uri) - elif uri_is_safe(node_uri): + elif isinstance(node_uri, basestring): uris.append(node_uri) nodes.append(self.get_or_create_node(node_uri)) else: @@ -219,6 +219,16 @@ def get_node(self, uri): else: assert False, "Got multiple results for URI %r" % uri + def find_nodes(self, pattern): + """ + Search for all nodes whose URIs match a given wildcard pattern, + using Lucene's wildcard syntax. Returns an iterator of the results. + + See this document for Lucene's syntax: + http://lucene.apache.org/java/2_0_0/queryparsersyntax.html + """ + return self._node_index.query('uri', pattern) + def get_edges(self, source, target): """ Get edges between `source` and `target`, specified as IDs or nodes. @@ -325,8 +335,9 @@ def get_or_create_assertion(self, relation, args, properties = {}): if index == 0: invalid = 'relation' else: invalid = 'argument ' + str(index) raise TypeError("%s is an invalid type. " % invalid) - uri = self.make_assertion_uri(self, uris[0],uris[1:]) - return self.get_node(uri) or self._create_assertion_from_components(self, uri, nodes[0],nodes[1:], properties) + + uri = self.make_assertion_uri(uris[0], uris[1:]) + return self.get_node(uri) or self._create_assertion_from_components(uri, nodes[0],nodes[1:], properties) def get_or_create_conjunction(self, conjuncts): conjuncts = [self._any_to_node(c) for c in conjuncts] diff --git a/pylintrc b/pylintrc index f6853db1..4c93f663 100644 --- a/pylintrc +++ b/pylintrc @@ -33,7 +33,7 @@ load-plugins= # can either give multiple identifier separated by comma (,) or put this option # multiple time (only on the command line, not in the configuration file where # it should appear only once). -#disable= +disable=W0142 [REPORTS] diff --git a/test/test_assertions.py b/test/test_assertions.py index c22188a3..7c7a0b2a 100644 --- a/test/test_assertions.py +++ b/test/test_assertions.py @@ -5,6 +5,9 @@ def test_create_assertions_twice(): g = ConceptNetGraph('http://tortoise.csc.media.mit.edu/db/data') a1 = g.get_or_create_node(u"/assertion/_/relation/IsA/_/concept/en/dog/_/concept/en/animal") assert a1 == g.get_or_create_node(u"/assertion/_/relation/IsA/_/concept/en/dog/_/concept/en/animal") + assert a1 == g.get_or_create_assertion(u'/relation/IsA', + [u'/concept/en/dog', u'/concept/en/animal'] + ) a2 = g.get_or_create_node(u"/assertion/_/relation/UsedFor/_/concept/zh_TW/枕頭/_/concept/zh_TW/睡覺") assert a2 == g.get_or_create_node(u"/assertion/_/relation/UsedFor/_/concept/zh_TW/枕頭/_/concept/zh_TW/睡覺")