|
| 1 | +import unittest |
| 2 | + |
| 3 | +from rdflib import Graph |
| 4 | + |
| 5 | +from verbalizer.process import Processor |
| 6 | +from verbalizer.sampler import Sampler |
| 7 | +from verbalizer.vocabulary import Vocabulary |
| 8 | +from verbalizer import Verbalizer |
| 9 | + |
| 10 | +rename_iri = { |
| 11 | + 'http://www.w3.org/2002/07/owl#equivalentClass': 'is same as', |
| 12 | + 'http://www.w3.org/2000/01/rdf-schema#subClassOf': 'is a type of', |
| 13 | + 'http://www.w3.org/2002/07/owl#intersectionOf': 'all of', |
| 14 | + 'http://www.w3.org/2002/07/owl#unionOf': 'any of', |
| 15 | + 'http://www.w3.org/2002/07/owl#disjointWith': 'is different from', |
| 16 | + 'http://www.w3.org/2002/07/owl#withRestrictions': 'must be' |
| 17 | +} |
| 18 | +ignore_iri = { |
| 19 | + 'http://www.w3.org/2002/07/owl#onDatatype', |
| 20 | + 'http://www.w3.org/2000/01/rdf-schema#seeAlso', |
| 21 | + 'http://www.w3.org/2000/01/rdf-schema#label', |
| 22 | + 'http://www.w3.org/2000/01/rdf-schema#comment', |
| 23 | + 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', |
| 24 | + 'http://www.w3.org/2000/01/rdf-schema#isDefinedBy', |
| 25 | + 'http://www.w3.org/2003/06/sw-vocab-status/ns#term_status', |
| 26 | + 'http://www.w3.org/2000/01/rdf-schema#Class' |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +class TestVerbalization(unittest.TestCase): |
| 31 | + |
| 32 | + def test_verbalization(self): |
| 33 | + # graph |
| 34 | + ontology = Processor.from_file('./data/foaf.owl') |
| 35 | + |
| 36 | + # create vocabulary |
| 37 | + vocab = Vocabulary(ontology, ignore=ignore_iri, rephrased=rename_iri) |
| 38 | + |
| 39 | + # create verbalizer |
| 40 | + verbalizer = Verbalizer(vocab) |
| 41 | + |
| 42 | + results = Processor.verbalize_with(verbalizer, namespace='foaf') |
| 43 | + self.assertEqual(12, len(results)) |
| 44 | + |
| 45 | + # Add default prefix (won't work without this) |
| 46 | + fragment_sample = '@prefix : <https://zaitoun.dev#> .\n' + results[0]['fragment'] |
| 47 | + g = Graph() |
| 48 | + g.parse(data=fragment_sample, format="turtle") |
| 49 | + |
| 50 | + self.assertEqual(7, len(list(g.triples((None, None, None))))) |
| 51 | + |
| 52 | + def test_verbalization_with_sampler(self): |
| 53 | + # graph |
| 54 | + ontology = Processor.from_file('./data/foaf.owl') |
| 55 | + |
| 56 | + # create vocabulary |
| 57 | + vocab = Vocabulary(ontology, ignore=ignore_iri, rephrased=rename_iri) |
| 58 | + |
| 59 | + # create verbalizer |
| 60 | + verbalizer = Verbalizer(vocab) |
| 61 | + |
| 62 | + sampler = Sampler(sample_n=10, seed=42) |
| 63 | + results = Processor.verbalize_with(verbalizer, namespace='foaf', sampler=sampler) |
| 64 | + |
| 65 | + # although we sampled 10, only 7 were applicable. |
| 66 | + self.assertEqual(7, len(results)) |
0 commit comments