MCPcopy Create free account
hub / github.com/hankcs/HanLP / to_conll

Method to_conll

plugins/hanlp_common/hanlp_common/document.py:132–186  ·  view source on GitHub ↗

Convert to :class:`~hanlp_common.conll.CoNLLSentence`. Args: tok (str): Field name for tok. lem (str): Field name for lem. pos (str): Field name for upos. xpos (str): Field name for xpos. fea (str): Field name for feats.

(self, tok='tok', lem='lem', pos='pos', xpos='pos/xpos', fea='fea', dep='dep', sdp='sdp')

Source from the content-addressed store, hash-verified

130 return self.to_json()
131
132 def to_conll(self, tok='tok', lem='lem', pos='pos', xpos='pos/xpos', fea='fea', dep='dep', sdp='sdp') -> Union[
133 CoNLLSentence, List[CoNLLSentence]]:
134 """
135 Convert to :class:`~hanlp_common.conll.CoNLLSentence`.
136
137 Args:
138 tok (str): Field name for tok.
139 lem (str): Field name for lem.
140 pos (str): Field name for upos.
141 xpos (str): Field name for xpos.
142 fea (str): Field name for feats.
143 dep (str): Field name for dependency parsing.
144 sdp (str): Field name for semantic dependency parsing.
145
146 Returns:
147 A :class:`~hanlp_common.conll.CoNLLSentence` representation.
148
149 """
150 tok = prefix_match(tok, self)
151 lem = prefix_match(lem, self)
152 pos = prefix_match(pos, self)
153 xpos = prefix_match(xpos, self)
154 fea = prefix_match(fea, self)
155 dep = prefix_match(dep, self)
156 sdp = prefix_match(sdp, self)
157 results = CoNLLSentenceList()
158 if not tok or not self[tok]:
159 return results
160 self = self._to_doc_without_spans(tok)
161 flat = isinstance(self[tok][0], str)
162 if flat:
163 d = Document((k, [v]) for k, v in self.items())
164 else:
165 d = self
166 for sample in [dict(zip(d, t)) for t in zip(*d.values())]:
167 def get(_k, _i):
168 _v = sample.get(_k, None)
169 if not _v:
170 return None
171 return _v[_i]
172
173 sent = CoNLLSentence()
174
175 for i, _tok in enumerate(sample[tok]):
176 _dep = get(dep, i)
177 if not _dep:
178 _dep = (None, None)
179 sent.append(
180 CoNLLUWord(i + 1, form=_tok, lemma=get(lem, i), upos=get(pos, i), xpos=get(xpos, i),
181 feats=get(fea, i), head=_dep[0], deprel=_dep[1],
182 deps=None if not get(sdp, i) else '|'.join(f'{x[0]}:{x[1]}' for x in get(sdp, i))))
183 results.append(sent)
184 if flat:
185 return results[0]
186 return results
187
188 def to_pretty(self, tok='tok', lem='lem', pos='pos', dep='dep', sdp='sdp', ner='ner', srl='srl', con='con',
189 show_header=True, html=False) -> Union[str, List[str]]:

Callers 1

to_prettyMethod · 0.95

Calls 8

_to_doc_without_spansMethod · 0.95
prefix_matchFunction · 0.90
CoNLLSentenceListClass · 0.90
CoNLLSentenceClass · 0.90
CoNLLUWordClass · 0.90
DocumentClass · 0.85
itemsMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected