예제 #1
0
    def __init__(self, config, dataset, feature_extractor):
        self.config = config
        self.X = dataset
        self.n_feature = dataset.n_feature  # 特征总数量
        self.n_tag = dataset.n_tag  # 标签总数量==5

        if config.init_model is None:
            self.model = Model(self.n_feature, self.n_tag)  # do this
        else:
            self.model = Model.load(config.init_model)
            self.model.expand(self.n_feature, self.n_tag)

        self.optim = self._get_optimizer(dataset, self.model)

        self.feature_extractor = feature_extractor
        self.idx_to_chunk_tag = {
        }  # {0: 'B', 1: 'B_single', 2: 'I', 3: 'I', 4: 'I'}
        """
        `tag_to_idx` : 
            {'B': 0, 'B_single': 1, 'I': 2, 'I_end': 3, 'I_first': 4}
        
        `startswith()` 函数:
            >>> aaa
            'Begin'
            >>> aaa.startswith("A")
            False
            >>> aaa.startswith("B")
            True
        """
        for tag, idx in feature_extractor.tag_to_idx.items():
            if tag.startswith("I"):  # ['I', 'I_end', 'I_first']
                tag = "I"
            if tag.startswith("O"):
                tag = "O"
            self.idx_to_chunk_tag[idx] = tag
예제 #2
0
    def __init__(self, config, dataset, feature_extractor):
        self.config = config
        self.X = dataset
        self.n_feature = dataset.n_feature
        self.n_tag = dataset.n_tag

        self.model = Model(self.n_feature, self.n_tag)
        self.optim = self._get_optimizer(dataset, self.model)

        self.feature_extractor = feature_extractor
        self.idx_to_chunk_tag = {}
        for tag, idx in feature_extractor.tag_to_idx.items():
            if tag.startswith("I"):
                tag = "I"
            if tag.startswith("O"):
                tag = "O"
            self.idx_to_chunk_tag[idx] = tag