Skip to content

Commit c1aa002

Browse files
committed
Display the main pos by default
1 parent 7aeb0fc commit c1aa002

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

plugins/hanlp_common/hanlp_common/conll.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,20 @@ def nonempty_fields(self):
6868
[self.form, self.lemma, self.cpos, self.pos, self.feats, self.head, self.deprel, self.phead,
6969
self.pdeprel] if f)
7070

71-
def get_pos(self):
71+
def get_pos(self, main_pos=False):
7272
"""
7373
Get the precisest pos for this word.
7474
75+
Args:
76+
main_pos: Use the main pos (cpos or upos) or the minor pos (pos or xpos).
77+
7578
Returns: ``self.pos`` or ``self.cpos``.
7679
7780
"""
78-
return self.pos or self.cpos
81+
if main_pos:
82+
return self.cpos or self.pos
83+
else:
84+
return self.pos or self.cpos
7985

8086

8187
class CoNLLUWord(SerializableDict):
@@ -155,14 +161,20 @@ def nonempty_fields(self):
155161
[self.form, self.lemma, self.upos, self.xpos, self.feats, self.head, self.deprel, self.deps,
156162
self.misc] if f)
157163

158-
def get_pos(self):
164+
def get_pos(self, main_pos=False):
159165
"""
160166
Get the precisest pos for this word.
161167
168+
Args:
169+
main_pos: Use the main pos (cpos or upos) or the minor pos (pos or xpos).
170+
162171
Returns: ``self.xpos`` or ``self.upos``
163172
164173
"""
165-
return self.xpos or self.upos
174+
if main_pos:
175+
return self.upos or self.xpos
176+
else:
177+
return self.xpos or self.upos
166178

167179

168180
class CoNLLSentence(list):
@@ -283,11 +295,12 @@ def to_markdown(self, headings: Union[str, List[str]] = 'auto') -> str:
283295
text = markdown_table(headings, cells, alignment=alignment)
284296
return text
285297

286-
def to_tree(self, extras: List[str] = None) -> str:
298+
def to_tree(self, extras: List[str] = None, main_pos=True) -> str:
287299
"""Convert into a pretty tree string which can be printed to show the tree structure.
288300
289301
Args:
290302
extras: Extra table to be aligned to this tree.
303+
main_pos: Use the main pos (cpos or upos) or the minor pos (pos or xpos).
291304
292305
Returns:
293306
A pretty tree string along with extra table if passed any.
@@ -313,7 +326,7 @@ def to_tree(self, extras: List[str] = None) -> str:
313326
if has_lem:
314327
cell_per_word.append(word.lemma)
315328
if has_pos:
316-
cell_per_word.append(word.get_pos())
329+
cell_per_word.append(word.get_pos(main_pos))
317330
if extras:
318331
cell_per_word.extend(extras[i + 1])
319332
rows.append(cell_per_word)

plugins/hanlp_common/hanlp_common/document.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def condense(block_, extras_=None):
365365
r.extend(s)
366366
# warnings.warn('Unable to visualize non-projective trees.')
367367
if dep in self and conll.projective:
368-
text = conll.to_tree(extras)
368+
text = conll.to_tree(extras, main_pos=True)
369369
if not show_header:
370370
text = text.split('\n')
371371
text = '\n'.join(text[2:])

0 commit comments

Comments
 (0)