1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# standard imports
import os
import unittest
import warnings
# bsie imports
from bsie.extractor import base
from bsie.matcher import nodes
from bsie.reader.document import Document
from bsie.utils import bsfs, ns
# objects to test
from bsie.extractor.text.summary import Summary
## code ##
class TestTextMetrics(unittest.TestCase):
def test_schema(self):
self.assertEqual(Summary().schema,
bsfs.schema.from_string(base.SCHEMA_PREAMBLE + '''
bse:summary rdfs:subClassOf bsfs:Predicate ;
rdfs:domain bsn:Entity ;
rdfs:range xsd:string ;
bsfs:unique "true"^^xsd:boolean .
'''))
def test_extract(self):
# setup
rdr = Document()
ext = Summary(max_length=20, num_beams=1)
subject = nodes.Entity(ucid='abc123')
principals = set(ext.principals)
path = os.path.join(os.path.dirname(__file__), 'example-en.txt')
# fetch document
text = rdr(path)
# empty input yields no triples
self.assertEqual(list(ext.extract(subject, [], principals)), [])
self.assertEqual(list(ext.extract(subject, [' '], principals)), [])
self.assertEqual(list(ext.extract(subject, [' ', ' ', ' '], principals)), [])
# creates a summary
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=FutureWarning)
triples = list(ext.extract(subject, text, principals))
self.assertEqual(triples, [
(subject, ext.schema.predicate(ns.bse.summary),
'Alice is tired of sitting by her sister on the bank')])
# skip unknown predicates
self.assertSetEqual(set(), set(ext.extract(subject, text,
{ext.schema.predicate(ns.bsfs.Predicate).child(ns.bse.unknown)})))
## main ##
if __name__ == '__main__':
unittest.main()
## EOF ##
|