[docs]defpredict_with_model(config_path:[Path,str])->List[Optional[List[str]]]:"""Returns predictions of morphotagging model given in config :config_path:. Args: config_path: a path to config Returns: a list of morphological analyses for each sentence. Each analysis is either a list of tags or a list of full CONLL-U descriptions. """config=parse_config(config_path)reader_config=config['dataset_reader']reader=get_model(reader_config['class_name'])()data_path=expand_path(reader_config.get('data_path',''))read_params={k:vfork,vinreader_config.items()ifknotin['class_name','data_path']}data:Dict=reader.read(data_path,**read_params)iterator_config=config['dataset_iterator']iterator:MorphoTaggerDatasetIterator=from_params(iterator_config,data=data)model=build_model(config,load_trained=True)answers=[None]*len(iterator.test)batch_size=config['predict'].get("batch_size",-1)forindexes,(x,_)initerator.gen_batches(batch_size=batch_size,data_type="test",shuffle=False,return_indexes=True):y=model(x)fori,eleminzip(indexes,y):answers[i]=elemoutfile=config['predict'].get("outfile")ifoutfileisnotNone:outfile=Path(outfile)ifnotoutfile.exists():outfile.parent.mkdir(parents=True,exist_ok=True)withopen(outfile,"w",encoding="utf8")asfout:foreleminanswers:fout.write(elem+"\n")returnanswers

[docs]@register('tag_output_prettifier')classTagOutputPrettifier(Component):"""Class which prettifies morphological tagger output to 4-column or 10-column (Universal Dependencies) format. Args: format_mode: output format, in `basic` mode output data contains 4 columns (id, word, pos, features), in `conllu` or `ud` mode it contains 10 columns: id, word, lemma, pos, xpos, feats, head, deprel, deps, misc (see http://universaldependencies.org/format.html for details) Only id, word, tag and pos values are present in current version, other columns are filled by `_` value. return_string: whether to return a list of strings or a single string begin: a string to append in the beginning end: a string to append in the end sep: separator between word analyses """def__init__(self,format_mode:str="basic",return_string:bool=True,begin:str="",end:str="",sep:str="\n",**kwargs)->None:self.set_format_mode(format_mode)self.return_string=return_stringself.begin=beginself.end=endself.sep=sep

def_make_format_string(self)->None:ifself.format_mode=="basic":self.format_string="{}\t{}\t{}\t{}"elifself.format_modein["conllu","ud"]:self.format_string="{}\t{}\t_\t{}\t_\t{}\t_\t_\t_\t_"else:raiseValueError("Wrong mode for TagOutputPrettifier: {}, ""it must be 'basic', 'conllu' or 'ud'.".format(self.mode))

[docs]def__call__(self,X:List[List[str]],Y:List[List[str]])->List[Union[List[str],str]]:"""Calls the ``prettify`` function for each input sentence. Args: X: a list of input sentences Y: a list of list of tags for sentence words Returns: a list of prettified morphological analyses """return[self.prettify(x,y)forx,yinzip(X,Y)]