MCPcopy Create free account
hub / github.com/baidu/DDParser / Sentence

Class Sentence

ddparser/parser/data_struct/corpus.py:36–72  ·  view source on GitHub ↗

Sentence

Source from the content-addressed store, hash-verified

34
35
36class Sentence(object):
37 """Sentence"""
38 def __init__(self, fields, values):
39 for field, value in zip(fields, values):
40 if isinstance(field, Iterable):
41 for j in range(len(field)):
42 setattr(self, field[j].name, value)
43 else:
44 setattr(self, field.name, value)
45 self.fields = fields
46
47 @property
48 def values(self):
49 """Returns an iterator containing all the features of one sentence"""
50 for field in self.fields:
51 if isinstance(field, Iterable):
52 yield getattr(self, field[0].name)
53 else:
54 yield getattr(self, field.name)
55
56 def __len__(self):
57 """Get sentence length"""
58 return len(next(iter(self.values)))
59
60 def __repr__(self):
61 """repr"""
62 return '\n'.join('\t'.join(map(str, line)) for line in zip(*self.values)) + '\n'
63
64 def get_result(self):
65 """Returns json style result"""
66 output = {}
67 for field in self.fields:
68 if isinstance(field, Iterable) and not field[0].name.isdigit():
69 output[field[0].name] = getattr(self, field[0].name)
70 elif not field.name.isdigit():
71 output[field.name] = getattr(self, field.name)
72 return output
73
74
75class Corpus(object):

Callers 3

loadMethod · 0.85
load_lac_resultsMethod · 0.85
load_word_segmentsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected