Пример #1
0
    def __call__(self, args):
        self.args = args
        if not hasattr(self.args, 'interpolation'):
            self.args.interpolation = 0.5
        if not os.path.exists(args.file):
            os.mkdir(args.file)
        if not os.path.exists(args.fields) or args.preprocess:
            print("Preprocess the data")
            self.WORD = Field('words', pad=pad, unk=unk, bos=bos, lower=True)
            # if args.feat == 'char':
            #     self.FEAT = CharField('chars', pad=pad, unk=unk, bos=bos,
            #                           fix_len=args.fix_len, tokenize=list)
            # elif args.feat == 'bert':
            #     tokenizer = BertTokenizer.from_pretrained(args.bert_model)
            #     self.FEAT = BertField('bert', pad='[PAD]', bos='[CLS]',
            #                           tokenize=tokenizer.encode)
            # else:
            #     self.FEAT = Field('tags', bos=bos)

            self.CHAR_FEAT = None
            self.POS_FEAT = None
            self.BERT_FEAT = None
            self.FEAT = [self.WORD]
            if args.use_char:
                self.CHAR_FEAT = CharField('chars',
                                           pad=pad,
                                           unk=unk,
                                           bos=bos,
                                           fix_len=args.fix_len,
                                           tokenize=list)
                self.FEAT.append(self.CHAR_FEAT)
            if args.use_pos:
                self.POS_FEAT = Field('tags', bos=bos)
            if args.use_bert:
                tokenizer = BertTokenizer.from_pretrained(args.bert_model)
                self.BERT_FEAT = BertField('bert',
                                           pad='[PAD]',
                                           bos='[CLS]',
                                           tokenize=tokenizer.encode)
                self.FEAT.append(self.BERT_FEAT)

            self.HEAD = Field('heads', bos=bos, use_vocab=False, fn=int)
            self.REL = Field('rels', bos=bos)

            self.fields = CoNLL(FORM=self.FEAT,
                                CPOS=self.POS_FEAT,
                                HEAD=self.HEAD,
                                DEPREL=self.REL)
            # if args.feat in ('char', 'bert'):
            #     self.fields = CoNLL(FORM=(self.WORD, self.FEAT),
            #                         HEAD=self.HEAD, DEPREL=self.REL)
            # else:
            #     self.fields = CoNLL(FORM=self.WORD, CPOS=self.FEAT,
            #                         HEAD=self.HEAD, DEPREL=self.REL)

            train = Corpus.load(args.ftrain, self.fields)
            if args.fembed:
                embed = Embedding.load(args.fembed, args.unk)
            else:
                embed = None
            self.WORD.build(train, args.min_freq, embed)
            if args.use_char:
                self.CHAR_FEAT.build(train)
            if args.use_pos:
                self.POS_FEAT.build(train)
            if args.use_bert:
                self.BERT_FEAT.build(train)
            # self.FEAT.build(train)
            self.REL.build(train)
            torch.save(self.fields, args.fields)
        else:
            self.fields = torch.load(args.fields)
            if args.feat in ('char', 'bert'):
                self.WORD, self.FEAT = self.fields.FORM
            else:
                self.WORD, self.FEAT = self.fields.FORM, self.fields.CPOS
            self.HEAD, self.REL = self.fields.HEAD, self.fields.DEPREL
        self.puncts = torch.tensor([
            i for s, i in self.WORD.vocab.stoi.items() if ispunct(s)
        ]).to(args.device)
        self.rel_criterion = nn.CrossEntropyLoss()
        self.arc_criterion = nn.CrossEntropyLoss()
        if args.binary:
            self.arc_criterion = nn.BCEWithLogitsLoss(reduction='none')

        # print(f"{self.WORD}\n{self.FEAT}\n{self.HEAD}\n{self.REL}")
        print(f"{self.WORD}\n{self.HEAD}\n{self.REL}")
        update_info = {}
        # pdb.set_trace()
        if args.use_char:
            update_info['n_char_feats'] = len(self.CHAR_FEAT.vocab)
        if args.use_pos:
            update_info['n_pos_feats'] = len(self.POS_FEAT.vocab)
        args.update({
            'n_words': self.WORD.vocab.n_init,
            # 'n_feats': len(self.FEAT.vocab),
            'n_rels': len(self.REL.vocab),
            'pad_index': self.WORD.pad_index,
            'unk_index': self.WORD.unk_index,
            'bos_index': self.WORD.bos_index
        })
        args.update(update_info)
Пример #2
0
    def __call__(self, args):
        self.args = args
        if not os.path.exists(args.file):
            os.mkdir(args.file)
        if not os.path.exists(args.fields) or args.preprocess:
            print("Preprocess the data")
            self.WORD = Field('words', pad=pad, unk=unk, bos=bos, lower=True)
            if args.feat == 'char':
                self.FEAT = CharField('chars',
                                      pad=pad,
                                      unk=unk,
                                      bos=bos,
                                      fix_len=args.fix_len,
                                      tokenize=list)
            elif args.feat == 'bert':
                tokenizer = BertTokenizer.from_pretrained(args.bert_model)
                self.FEAT = BertField('bert',
                                      pad='[PAD]',
                                      bos='[CLS]',
                                      tokenize=tokenizer.encode)
            else:
                self.FEAT = Field('tags', bos=bos)
            self.HEAD = Field('heads', bos=bos, use_vocab=False, fn=int)
            self.REL = Field('rels', bos=bos)
            if args.feat in ('char', 'bert'):
                self.fields = CoNLL(FORM=(self.WORD, self.FEAT),
                                    HEAD=self.HEAD,
                                    DEPREL=self.REL)
            else:
                self.fields = CoNLL(FORM=self.WORD,
                                    CPOS=self.FEAT,
                                    HEAD=self.HEAD,
                                    DEPREL=self.REL)

            train = Corpus.load(args.ftrain, self.fields)
            if args.fembed:
                embed = Embedding.load(args.fembed, args.unk)
            else:
                embed = None
            self.WORD.build(train, args.min_freq, embed)
            self.FEAT.build(train)
            self.REL.build(train)
            torch.save(self.fields, args.fields)
        else:
            self.fields = torch.load(args.fields)
            if args.feat in ('char', 'bert'):
                self.WORD, self.FEAT = self.fields.FORM
            else:
                self.WORD, self.FEAT = self.fields.FORM, self.fields.CPOS
            self.HEAD, self.REL = self.fields.HEAD, self.fields.DEPREL
        self.puncts = torch.tensor([
            i for s, i in self.WORD.vocab.stoi.items() if ispunct(s)
        ]).to(args.device)
        self.criterion = nn.CrossEntropyLoss()

        print(f"{self.WORD}\n{self.FEAT}\n{self.HEAD}\n{self.REL}")
        args.update({
            'n_words': self.WORD.vocab.n_init,
            'n_feats': len(self.FEAT.vocab),
            'n_rels': len(self.REL.vocab),
            'pad_index': self.WORD.pad_index,
            'unk_index': self.WORD.unk_index,
            'bos_index': self.WORD.bos_index
        })
Пример #3
0
    def __call__(self, args):
        self.args = args
        if not os.path.exists(args.file):
            os.mkdir(args.file)
        if not os.path.exists(args.fields) or args.preprocess:
            logger.info("Preprocess the data")
            self.WORD = Field('words',
                              pad=pad,
                              unk=unk,
                              bos=bos,
                              lower=args.lower)
            if args.feat == 'char':
                self.FEAT = SubwordField('chars',
                                         pad=pad,
                                         unk=unk,
                                         bos=bos,
                                         fix_len=args.fix_len,
                                         tokenize=list)
            elif args.feat == 'bert':
                tokenizer = SubwordField.tokenizer(args.bert_model)
                self.FEAT = SubwordField('bert',
                                         tokenizer=tokenizer,
                                         fix_len=args.fix_len)
                self.bos = self.FEAT.bos or bos
                if hasattr(tokenizer, 'vocab'):
                    self.FEAT.vocab = tokenizer.vocab
                else:
                    self.FEAT.vocab = FieldVocab(
                        tokenizer.unk_token_id, {
                            tokenizer._convert_id_to_token(i): i
                            for i in range(len(tokenizer))
                        })
            else:
                self.FEAT = Field('tags', bos=self.bos)
            self.ARC = Field('arcs',
                             bos=self.bos,
                             use_vocab=False,
                             fn=numericalize)
            self.REL = Field('rels', bos=self.bos)
            if args.feat == 'bert':
                if args.n_embed:
                    self.fields = CoNLL(FORM=(self.WORD, self.FEAT),
                                        HEAD=self.ARC,
                                        DEPREL=self.REL)
                    self.WORD.bos = self.bos  # ensure representations of the same length
                else:
                    self.fields = CoNLL(FORM=self.FEAT,
                                        HEAD=self.ARC,
                                        DEPREL=self.REL)
                    self.WORD = None
            elif args.feat == 'char':
                self.fields = CoNLL(FORM=(self.WORD, self.FEAT),
                                    HEAD=self.ARC,
                                    DEPREL=self.REL)
            else:
                self.fields = CoNLL(FORM=self.WORD,
                                    CPOS=self.FEAT,
                                    HEAD=self.ARC,
                                    DEPREL=self.REL)

            train = Corpus.load(args.ftrain, self.fields, args.max_sent_length)
            if args.fembed:
                embed = Embedding.load(args.fembed, args.unk)
            else:
                embed = None
            if self.WORD:
                self.WORD.build(train, args.min_freq, embed)
            self.FEAT.build(train)
            self.REL.build(train)
            if args.feat == 'bert':
                # do not save the tokenize funztion, or else it might be incompatible with new releases
                tokenize = self.FEAT.tokenize  # save it
                self.FEAT.tokenize = None
            torch.save(self.fields, args.fields)
            if args.feat == 'bert':
                self.FEAT.tokenize = tokenize  # restore
            self.trainset = train  # pass it on to subclasses
        else:
            self.trainset = None
            self.fields = torch.load(args.fields)
            if args.feat == 'bert':
                tokenizer = SubwordField.tokenizer(args.bert_model)
                if args.n_embed:
                    self.fields.FORM[1].tokenize = tokenizer.tokenize
                else:
                    self.fields.FORM.tokenize = tokenizer.tokenize
            if args.feat in ('char', 'bert'):
                if isinstance(self.fields.FORM, tuple):
                    self.WORD, self.FEAT = self.fields.FORM
                else:
                    self.WORD, self.FEAT = None, self.fields.FORM
            else:
                self.WORD, self.FEAT = self.fields.FORM, self.fields.CPOS
            self.ARC, self.REL = self.fields.HEAD, self.fields.DEPREL
        self.puncts = torch.tensor(
            [i for s, i in self.WORD.vocab.stoi.items()
             if ispunct(s)]).to(args.device) if self.WORD else []

        # override parameters from embeddings:
        if self.WORD:
            args.update({
                'n_words': self.WORD.vocab.n_init,
                'pad_index': self.WORD.pad_index,
                'unk_index': self.WORD.unk_index,
                'bos_index': self.WORD.bos_index,
            })
        args.update({
            'n_feats': len(self.FEAT.vocab),
            'n_rels': len(self.REL.vocab),
            'feat_pad_index': self.FEAT.pad_index,
        })

        logger.info("Features:")
        if self.WORD:
            logger.info(f"   {self.WORD}")
        logger.info(f"   {self.FEAT}\n   {self.ARC}\n   {self.REL}")
Пример #4
0
    def __call__(self, args):
        self.args = args
        logging.basicConfig(filename=args.output, filemode='w', format='%(asctime)s %(levelname)-8s %(message)s', level=logging.INFO, datefmt='%Y-%m-%d %H:%M:%S')
        
        args.ud_dataset = {
                'en': (
                    'data/ud/UD_English-EWT/en_ewt-ud-train.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-dev.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-test.conllx',
                    "data/fastText_data/wiki.en.ewt.vec.new",
                ),
                'en20': (
                    'data/ud/UD_English-EWT/en_ewt-ud-train20.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-dev.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-test.conllx',
                    "data/fastText_data/wiki.en.ewt.vec.new",
                ),
                'en40': (
                    'data/ud/UD_English-EWT/en_ewt-ud-train40.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-dev.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-test.conllx',
                    "data/fastText_data/wiki.en.ewt.vec.new",
                ),
                'en60': (
                    'data/ud/UD_English-EWT/en_ewt-ud-train60.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-dev.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-test.conllx',
                    "data/fastText_data/wiki.en.ewt.vec.new",
                ),
                'en80': (
                    'data/ud/UD_English-EWT/en_ewt-ud-train80.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-dev.conllx',
                    'data/ud/UD_English-EWT/en_ewt-ud-test.conllx',
                    "data/fastText_data/wiki.en.ewt.vec.new",
                ),
                'ar': (
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-train.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-dev.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-test.conllx",
                    "data/fastText_data/wiki.ar.padt.vec.new",
                ),
                'ar20': (
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-train20.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-dev.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-test.conllx",
                    "data/fastText_data/wiki.ar.padt.vec.new",
                ),
                'ar40': (
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-train40.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-dev.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-test.conllx",
                    "data/fastText_data/wiki.ar.padt.vec.new",
                ),
                'ar60': (
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-train60.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-dev.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-test.conllx",
                    "data/fastText_data/wiki.ar.padt.vec.new",
                ),
                'ar80': (
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-train80.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-dev.conllx",
                    "data/ud/UD_Arabic-PADT/ar_padt-ud-test.conllx",
                    "data/fastText_data/wiki.ar.padt.vec.new",
                ),
                'bg': (
                    "data/ud/UD_Bulgarian-BTB/bg_btb-ud-train.conllx",
                    "data/ud/UD_Bulgarian-BTB/bg_btb-ud-dev.conllx",
                    "data/ud/UD_Bulgarian-BTB/bg_btb-ud-test.conllx",
                    "data/fastText_data/wiki.bg.btb.vec.new",
                ),
                'da': (
                    "data/ud/UD_Danish-DDT/da_ddt-ud-train.conllx",
                    "data/ud/UD_Danish-DDT/da_ddt-ud-dev.conllx",
                    "data/ud/UD_Danish-DDT/da_ddt-ud-test.conllx",
                    "data/fastText_data/wiki.da.ddt.vec.new",
                ),
                'de': (
                    "data/ud/UD_German-GSD/de_gsd-ud-train.conllx",
                    "data/ud/UD_German-GSD/de_gsd-ud-dev.conllx",
                    "data/ud/UD_German-GSD/de_gsd-ud-test.conllx",
                    "data/fastText_data/wiki.de.gsd.vec.new",
                ),
                'es': (
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-train.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-dev.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-test.conllx",
                    "data/fastText_data/wiki.es.gsdancora.vec.new",
                ),
                'es20': (
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-train20.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-dev.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-test.conllx",
                    "data/fastText_data/wiki.es.gsdancora.vec.new",
                ),
                'es40': (
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-train40.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-dev.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-test.conllx",
                    "data/fastText_data/wiki.es.gsdancora.vec.new",
                ),
                'es60': (
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-train60.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-dev.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-test.conllx",
                    "data/fastText_data/wiki.es.gsdancora.vec.new",
                ),
                'es80': (
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-train80.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-dev.conllx",
                    "data/ud/UD_Spanish-GSDAnCora/es_gsdancora-ud-test.conllx",
                    "data/fastText_data/wiki.es.gsdancora.vec.new",
                ),
                'fa': (
                    "data/ud/UD_Persian-Seraji/fa_seraji-ud-train.conllx",
                    "data/ud/UD_Persian-Seraji/fa_seraji-ud-dev.conllx",
                    "data/ud/UD_Persian-Seraji/fa_seraji-ud-test.conllx",
                    "data/fastText_data/wiki.fa.seraji.vec.new",
                ),
                'fr': (
                    "data/ud/UD_French-GSD/fr_gsd-ud-train.conllx",
                    "data/ud/UD_French-GSD/fr_gsd-ud-dev.conllx",
                    "data/ud/UD_French-GSD/fr_gsd-ud-test.conllx",
                    "data/fastText_data/wiki.fr.gsd.vec.new",
                ),
                'he': (
                    "data/ud/UD_Hebrew-HTB/he_htb-ud-train.conllx",
                    "data/ud/UD_Hebrew-HTB/he_htb-ud-dev.conllx",
                    "data/ud/UD_Hebrew-HTB/he_htb-ud-test.conllx",
                    "data/fastText_data/wiki.he.htb.vec.new",
                ),
                'hi': (
                    "data/ud/UD_Hindi-HDTB/hi_hdtb-ud-train.conllx",
                    "data/ud/UD_Hindi-HDTB/hi_hdtb-ud-dev.conllx",
                    "data/ud/UD_Hindi-HDTB/hi_hdtb-ud-test.conllx",
                    "data/fastText_data/wiki.hi.hdtb.vec.new",
                ),
                'hr': (
                    "data/ud/UD_Croatian-SET/hr_set-ud-train.conllx",
                    "data/ud/UD_Croatian-SET/hr_set-ud-dev.conllx",
                    "data/ud/UD_Croatian-SET/hr_set-ud-test.conllx",
                    "data/fastText_data/wiki.hr.set.vec.new",
                ),
                'id': (
                    "data/ud/UD_Indonesian-GSD/id_gsd-ud-train.conllx",
                    "data/ud/UD_Indonesian-GSD/id_gsd-ud-dev.conllx",
                    "data/ud/UD_Indonesian-GSD/id_gsd-ud-test.conllx",
                    "data/fastText_data/wiki.id.gsd.vec.new",
                ),
                'it': (
                    "data/ud/UD_Italian-ISDT/it_isdt-ud-train.conllx",
                    "data/ud/UD_Italian-ISDT/it_isdt-ud-dev.conllx",
                    "data/ud/UD_Italian-ISDT/it_isdt-ud-test.conllx",
                    "data/fastText_data/wiki.it.isdt.vec.new",
                ),
                'ja': (
                    "data/ud/UD_Japanese-GSD/ja_gsd-ud-train.conllx",
                    "data/ud/UD_Japanese-GSD/ja_gsd-ud-dev.conllx",
                    "data/ud/UD_Japanese-GSD/ja_gsd-ud-test.conllx",
                    "data/fastText_data/wiki.ja.gsd.vec.new",
                ),
                'ko': (
                    "data/ud/UD_Korean-GSDKaist/ko_gsdkaist-ud-train.conllx",
                    "data/ud/UD_Korean-GSDKaist/ko_gsdkaist-ud-dev.conllx",
                    "data/ud/UD_Korean-GSDKaist/ko_gsdkaist-ud-test.conllx",
                    "data/fastText_data/wiki.ko.gsdkaist.vec.new",
                ),
                'nl': (
                    "data/ud/UD_Dutch-AlpinoLassySmall/nl_alpinolassysmall-ud-train.conllx",
                    "data/ud/UD_Dutch-AlpinoLassySmall/nl_alpinolassysmall-ud-dev.conllx",
                    "data/ud/UD_Dutch-AlpinoLassySmall/nl_alpinolassysmall-ud-test.conllx",
                    "data/fastText_data/wiki.nl.alpinolassysmall.vec.new",
                ),
                'no': (
                    "data/ud/UD_Norwegian-BokmaalNynorsk/no_bokmaalnynorsk-ud-train.conllx",
                    "data/ud/UD_Norwegian-BokmaalNynorsk/no_bokmaalnynorsk-ud-dev.conllx",
                    "data/ud/UD_Norwegian-BokmaalNynorsk/no_bokmaalnynorsk-ud-test.conllx",
                    "data/fastText_data/wiki.no.bokmaalnynorsk.vec.new",
                ),
                'pt': (
                    "data/ud/UD_Portuguese-BosqueGSD/pt_bosquegsd-ud-train.conllx",
                    "data/ud/UD_Portuguese-BosqueGSD/pt_bosquegsd-ud-dev.conllx",
                    "data/ud/UD_Portuguese-BosqueGSD/pt_bosquegsd-ud-test.conllx",
                    "data/fastText_data/wiki.pt.bosquegsd.vec.new",
                ),
                'sv': (
                    "data/ud/UD_Swedish-Talbanken/sv_talbanken-ud-train.conllx",
                    "data/ud/UD_Swedish-Talbanken/sv_talbanken-ud-dev.conllx",
                    "data/ud/UD_Swedish-Talbanken/sv_talbanken-ud-test.conllx",
                    "data/fastText_data/wiki.sv.talbanken.vec.new",
                ),
                'tr': (
                    "data/ud/UD_Turkish-IMST/tr_imst-ud-train.conllx",
                    "data/ud/UD_Turkish-IMST/tr_imst-ud-dev.conllx",
                    "data/ud/UD_Turkish-IMST/tr_imst-ud-test.conllx",
                    "data/fastText_data/wiki.tr.imst.vec.new",
                ),
                'zh': (
                    "data/ud/UD_Chinese-GSD/zh_gsd-ud-train.conllx",
                    "data/ud/UD_Chinese-GSD/zh_gsd-ud-dev.conllx",
                    "data/ud/UD_Chinese-GSD/zh_gsd-ud-test.conllx",
                    "data/fastText_data/wiki.zh.gsd.vec.new",
                )}

        self.args.ftrain = args.ud_dataset[args.lang][0]
        self.args.fdev = args.ud_dataset[args.lang][1]
        self.args.ftest = args.ud_dataset[args.lang][2]
        self.args.fembed = args.ud_dataset[args.lang][3]

        if not os.path.exists(args.file):
            os.mkdir(args.file)
        if not os.path.exists(args.fields) or args.preprocess:
            logging.info("Preprocess the data")
            
            self.WORD = Field('words', pad=pad, unk=unk, bos=bos, lower=True)

            tokenizer = BertTokenizer.from_pretrained(args.bert_model)
            self.BERT = BertField('bert', pad='[PAD]', bos='[CLS]',
                                    tokenize=tokenizer.encode)

            if args.feat == 'char':
                self.FEAT = CharField('chars', pad=pad, unk=unk, bos=bos,
                                      fix_len=args.fix_len, tokenize=list)
            elif args.feat == 'bert':
                tokenizer = BertTokenizer.from_pretrained(args.bert_model)
                self.FEAT = BertField('bert', pad='[PAD]', bos='[CLS]',
                                      tokenize=tokenizer.encode)
            else:
                self.FEAT = Field('tags', bos=bos)
            self.HEAD = Field('heads', bos=bos, use_vocab=False, fn=int)
            self.REL = Field('rels', bos=bos)
            if args.feat in ('char', 'bert'):
                self.fields = CoNLL(FORM=(self.WORD, self.BERT, self.FEAT),
                                    HEAD=self.HEAD, DEPREL=self.REL)
            else:
                self.fields = CoNLL(FORM=(self.WORD, self.BERT), CPOS=self.FEAT,
                                    HEAD=self.HEAD, DEPREL=self.REL)

            train = Corpus.load(args.ftrain, self.fields, args.max_len)
            if args.fembed:
                if args.bert is False:
                    # fasttext
                    embed = Embedding.load(args.fembed, args.lang, unk=args.unk)
                else:
                    embed = None
            else:
                embed = None
            
            self.WORD.build(train, args.min_freq, embed)
            self.FEAT.build(train)
            self.BERT.build(train)
            self.REL.build(train)
            torch.save(self.fields, args.fields)
        else:
            self.fields = torch.load(args.fields)
            if args.feat in ('char', 'bert'):
                self.WORD, self.BERT, self.FEAT = self.fields.FORM
            else:
                self.WORD, self.BERT, self.FEAT = self.fields.FORM, self.fields.CPOS
            self.HEAD, self.REL = self.fields.HEAD, self.fields.DEPREL


        self.puncts = torch.tensor([i for s, i in self.WORD.vocab.stoi.items()
                                    if ispunct(s)]).to(args.device)
        self.criterion = nn.CrossEntropyLoss()

        logging.info(f"{self.WORD}\n{self.FEAT}\n{self.BERT}\n{self.HEAD}\n{self.REL}")
        args.update({
            'n_words': self.WORD.vocab.n_init,
            'n_feats': len(self.FEAT.vocab),
            'n_bert': len(self.BERT.vocab),
            'n_rels': len(self.REL.vocab),
            'pad_index': self.WORD.pad_index,
            'unk_index': self.WORD.unk_index,
            'bos_index': self.WORD.bos_index
        })
        logging.info(f"n_words {args.n_words} n_feats {args.n_feats} n_bert {args.n_bert} pad_index {args.pad_index} bos_index {args.bos_index}")