def test1ParseGoverment(self): f = "src/pysumo/data/Government.kif" o = Ontology(f, lpath=self.tmpdir) atexit.unregister(o.action_log.log_io.flush_write_queues) with open(o.path) as f: parser.kifparse(f, o) o.action_log.log_io.flush_write_queues()
def test2InvalideSyntax(self): text1 = "( bla" text2 = "bla (" with self.assertRaises(parser.ParseError): parser.kifparse(StringIO(text1), None) with self.assertRaises(parser.ParseError): parser.kifparse(StringIO(text2), None)
def test0FirstOntology(self): self.assertEqual(self.syntaxcontroller.index.root, None) self.syntaxcontroller.add_ontology(self.sumo) self.assertNotEqual(self.syntaxcontroller.index.root, None) with open(self.sumo.path) as f: kif = kifparse(f, self.sumo) kif.ontology = None self.assertEqual(self.syntaxcontroller.index.root, kif)
def setUp(self): self.tmpdir = mkdtemp() self.sumo = Ontology('src/pysumo/data/Merge.kif', name='SUMO', lpath=self.tmpdir) atexit.unregister(self.sumo.action_log.log_io.flush_write_queues) with open(self.sumo.path) as f: self.kif = parser.kifparse(f, self.sumo) self.indexabstractor = IndexAbstractor() self.indexabstractor.update_index(self.kif)
def test0ParseSerilize(self): tempd = mkdtemp() out1 = "/".join([tempd, "out1"]) out2 = "/".join([tempd, "out2"]) f = "src/pysumo/data/Merge.kif" o = Ontology(f, lpath=self.tmpdir) atexit.unregister(o.action_log.log_io.flush_write_queues) with open(o.path) as f: a = parser.kifparse(f, o) self.assertNotEqual(a.children, []) with open(out1, 'w') as f: parser.kifserialize(a, o, f) with open(o.path) as f: a = parser.kifparse(f, o) with open(out2, 'w') as f: parser.kifserialize(a, o, f) ret = subprocess.call(["diff", out1, out2]) rmtree(tempd) assert ret == 0 o.action_log.log_io.flush_write_queues()
def test3lineNumber(self): f = "src/pysumo/data/Merge.kif" a = None node = None with open(f, errors='replace') as of: a = parser.kifparse(of, None) for n in a.children: if str(n) != "( instance domain TernaryPredicate )": continue node = n break self.assertEqual(n.line, 199)
def test6MultipleOntologies(self): milo = Ontology('src/pysumo/data/MILO.kif', lpath=self.tmpdir) atexit.unregister(milo.action_log.log_io.flush_write_queues) with open(milo.path) as f: milo_kif = parser.kifparse(f, milo) merged_asts = parser.astmerge((self.kif, milo_kif)) search_results = self.indexabstractor.search('rangesubclass')[ self.sumo] self.indexabstractor.update_index(merged_asts) new_results = self.indexabstractor.search('rangesubclass')[self.sumo] self.assertListEqual(search_results, new_results) milo.action_log.log_io.flush_write_queues()
def test7GetCompletions(self): completions = self.indexabstractor.get_completions() self.assertIn('TwoDimensionalFigure', completions) self.assertEqual(len(completions), 1168) milo = Ontology('src/pysumo/data/MILO.kif', lpath=self.tmpdir) atexit.unregister(milo.action_log.log_io.flush_write_queues) with open(milo.path) as f: milo_kif = parser.kifparse(f, milo) merged_asts = parser.astmerge((self.kif, milo_kif)) self.indexabstractor.update_index(merged_asts) completions = self.indexabstractor.get_completions() self.assertIn('TwoDimensionalFigure', completions) self.assertIn('SubstringFn', completions) self.assertEqual(len(completions), 3228) milo.action_log.log_io.flush_write_queues()
def test6ParseDiff(self): self.syntaxcontroller.add_ontology(self.sumo) old_ast = deepcopy(self.syntaxcontroller.index.root) code_block = StringIO(_DIFF_ADD) self.syntaxcontroller.parse_patch(self.sumo, _DIFF_ADD) sterm = self.syntaxcontroller.index.search('foo') self.assertListEqual([x[0] for x in sterm[self.sumo]], [ '( instance foo Entity )', '( documentation foo EnglishLanguage "&%foo is an object of type foo" )' ]) sio = StringIO( '(instance foo Entity)\n(documentation foo EnglishLanguage "&%foo is an object of type foo")\n' ) ast = kifparse(sio, self.sumo) old_ast.children.extend(ast.children) self.assertEqual(len(self.syntaxcontroller.index.root.children), len(old_ast.children)) self.syntaxcontroller.parse_patch(self.sumo, _DIFF_SUB) sterm = self.syntaxcontroller.index.search('foo') self.assertListEqual([x[0] for x in sterm[self.sumo]], list()) self.assertRaises(KeyError, self.syntaxcontroller.index._find_term, 'foo')
def test5ParseAdd(self): self.syntaxcontroller.add_ontology(self.sumo) old_ast = deepcopy(self.syntaxcontroller.index.root) code_block = StringIO() with open(self.sumo.path) as code: code_block.write(code.read()) code_block.write( '(instance foo Entity)\n(documentation foo EnglishLanguage "&%foo is an object of type foo")\n' ) self.syntaxcontroller.add_ontology(self.sumo, code_block.getvalue()) sterm = self.syntaxcontroller.index.search('foo') self.assertListEqual([x[0] for x in sterm[self.sumo]], [ '( instance foo Entity )', '( documentation foo EnglishLanguage "&%foo is an object of type foo" )' ]) sio = StringIO( '(instance foo Entity)\n(documentation foo EnglishLanguage "&%foo is an object of type foo")\n' ) ast = kifparse(sio, self.sumo) old_ast.children.extend(ast.children) self.assertEqual(len(self.syntaxcontroller.index.root.children), len(old_ast.children))
#!/usr/bin/env python3 import sys sys.path.append('../src') from tempfile import mkdtemp import pysumo from pysumo import parser from pysumo.indexabstractor import * from pysumo.syntaxcontroller import Ontology sumo = Ontology('../src/pysumo/data/Merge.kif', name='SUMO') with open(sumo.path) as f: mergeKif = parser.kifparse(f, sumo) index = IndexAbstractor() index.update_index(mergeKif) EntitySubclassGraph = index.get_graph([(0, 'subclass')], root='Entity') print('In "subclass" relation with "Entity":') print(EntitySubclassGraph.nodes) BinaryPredicateInstanceGraph = index.get_graph([(0, 'instance')], root='BinaryPredicate') print('\nIn "instance" relation with "BinaryPredicate":') print(BinaryPredicateInstanceGraph.nodes)
def test5GetOntology(self): of = self.indexabstractor.get_ontology_file(self.sumo) o_ast = parser.kifparse(of, self.sumo) with open(self.sumo.path) as sumo: s_ast = parser.kifparse(sumo, self.sumo) self.assertEqual(o_ast, s_ast)