66from spacy .tokens import Doc , Span , Token
77from spacy_conll .utils import PD_AVAILABLE , merge_dicts_strict
88
9-
109if PD_AVAILABLE :
1110 import pandas as pd
1211
1312CONLL_FIELD_NAMES = [
14- "id " ,
15- "form " ,
16- "lemma " ,
17- "upostag " ,
18- "xpostag " ,
19- "feats " ,
20- "head " ,
21- "deprel " ,
22- "deps " ,
23- "misc " ,
13+ "ID " ,
14+ "FORM " ,
15+ "LEMMA " ,
16+ "UPOS " ,
17+ "XPOS " ,
18+ "FEATS " ,
19+ "HEAD " ,
20+ "DEPREL " ,
21+ "DEPS " ,
22+ "MISC " ,
2423]
2524
2625
2726@Language .factory (
2827 "conll_formatter" ,
29- default_config = {"conversion_maps" : None , "ext_names" : None , "include_headers" : False , "disable_pandas" : False },
28+ default_config = {
29+ "conversion_maps" : None ,
30+ "ext_names" : None ,
31+ "field_names" : None ,
32+ "include_headers" : False ,
33+ "disable_pandas" : False
34+ },
3035)
3136def create_conll_formatter (
32- nlp : Language ,
33- name : str ,
34- conversion_maps : Optional [Dict [str , Dict [str , str ]]] = None ,
35- ext_names : Optional [Dict [str , str ]] = None ,
36- include_headers : bool = False ,
37- disable_pandas : bool = False ,
37+ nlp : Language ,
38+ name : str ,
39+ conversion_maps : Optional [Dict [str , Dict [str , str ]]] = None ,
40+ ext_names : Optional [Dict [str , str ]] = None ,
41+ field_names : Dict [str , str ] = None ,
42+ include_headers : bool = False ,
43+ disable_pandas : bool = False ,
3844):
3945 return ConllFormatter (
4046 conversion_maps = conversion_maps ,
4147 ext_names = ext_names if ext_names else {},
48+ field_names = field_names if field_names else {},
4249 include_headers = include_headers ,
4350 disable_pandas = disable_pandas ,
4451 )
@@ -77,8 +84,11 @@ class ConllFormatter:
7784 on the first level, and the conversion map on the second.
7885 E.g. {'lemma': {'-PRON-': 'PRON'}} will map the lemma '-PRON-' to 'PRON'
7986 :param ext_names: dictionary containing names for the custom spaCy extensions. You can rename the following
80- extensions: conll, conll_pd, conll_str.
81- E.g. {'conll': 'conll_dict', 'conll_pd': 'conll_pandas'} will rename the properties accordingly
87+ extensions (use as keys): 'conll', 'conll_pd', 'conll_str'. E.g. {'conll': 'conll_dict', 'conll_pd': 'conll_pandas'}
88+ will rename the properties accordingly
89+ :param field_names: dictionary containing names for custom field names in case you do not want to use default
90+ CoNLL-U field names. You can rename the following fields (use as keys): 'ID', 'FORM', 'LEMMA', 'UPOS', 'XPOS',
91+ 'FEATS', 'HEAD', 'DEPREL', 'DEPS', 'MISC'. E.g. {'UPOS': 'upostag'} will rename the field name UPOS accordingly.
8292 :param include_headers: whether to include the CoNLL headers in the conll_str string output. These consist
8393 of two lines containing the sentence id and the text as per the CoNLL format
8494 https://universaldependencies.org/format.html#sentence-boundaries-and-comments.
@@ -88,13 +98,16 @@ class ConllFormatter:
8898
8999 conversion_maps : Optional [Dict [str , Dict [str , str ]]] = None
90100 ext_names : Dict [str , str ] = field (default_factory = dict )
101+ field_names : Dict [str , str ] = field (default_factory = dict )
91102 include_headers : bool = False
92103 disable_pandas : bool = False
93104
94105 def __post_init__ (self ):
95106 # Set custom attribute names so that users can access them with their own preference
96107 default_ext_names = {"conll_str" : "conll_str" , "conll" : "conll" , "conll_pd" : "conll_pd" }
97108 self .ext_names = merge_dicts_strict (default_ext_names , self .ext_names )
109+ default_field_names = {fname : fname for fname in CONLL_FIELD_NAMES }
110+ self .field_names = merge_dicts_strict (default_field_names , self .field_names )
98111
99112 # Initialize extensions
100113 self ._set_extensions ()
@@ -197,7 +210,7 @@ def _set_token_conll(self, token: Token, token_idx: int = 1) -> Token:
197210 )
198211
199212 # turn field name values (keys) and token values (values) into dict
200- token_conll_d = OrderedDict (zip (CONLL_FIELD_NAMES , token_conll ))
213+ token_conll_d = OrderedDict (zip (list ( self . field_names . values ()) , token_conll ))
201214
202215 # convert properties if needed
203216 if self .conversion_maps :
0 commit comments