extract_dict_feature.py 2.7 KB
Newer Older
1
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys
import os
from optparse import OptionParser


Z
zhangjinchao01 已提交
20 21 22
def extract_dict_features(pair_file, feature_file):

    with open(pair_file) as fin, open(feature_file, 'w') as feature_out:
Z
zhangjinchao01 已提交
23
        for line in fin:
Z
zhangjinchao01 已提交
24
            sentence, predicate, labels = line.strip().split('\t')
Z
zhangjinchao01 已提交
25 26 27 28 29 30 31 32 33 34 35
            sentence_list = sentence.split()
            labels_list = labels.split()

            verb_index = labels_list.index('B-V')

            mark = [0] * len(labels_list)
            if verb_index > 0:
                mark[verb_index - 1] = 1
                ctx_n1 = sentence_list[verb_index - 1]
            else:
                ctx_n1 = 'bos'
Y
Yu Yang 已提交
36

Z
zhangjinchao01 已提交
37 38 39 40 41
            if verb_index > 1:
                mark[verb_index - 2] = 1
                ctx_n2 = sentence_list[verb_index - 2]
            else:
                ctx_n2 = 'bos'
Z
zhangjinchao01 已提交
42 43

            mark[verb_index] = 1
Z
zhangjinchao01 已提交
44
            ctx_0 = sentence_list[verb_index]
Z
zhangjinchao01 已提交
45

Z
zhangjinchao01 已提交
46
            if verb_index < len(labels_list) - 1:
Z
zhangjinchao01 已提交
47 48 49 50
                mark[verb_index + 1] = 1
                ctx_p1 = sentence_list[verb_index + 1]
            else:
                ctx_p1 = 'eos'
Y
Yu Yang 已提交
51

Z
zhangjinchao01 已提交
52
            if verb_index < len(labels_list) - 2:
Z
zhangjinchao01 已提交
53 54 55 56 57
                mark[verb_index + 2] = 1
                ctx_p2 = sentence_list[verb_index + 2]
            else:
                ctx_p2 = 'eos'

Z
zhangjinchao01 已提交
58 59

            feature_str  = sentence + '\t' \
Z
zhangjinchao01 已提交
60 61 62 63 64 65
                           + predicate + '\t' \
                           + ctx_n2 + '\t' \
                           + ctx_n1 + '\t' \
                           + ctx_0 + '\t' \
                           + ctx_p1 + '\t' \
                           + ctx_p2 + '\t' \
Z
zhangjinchao01 已提交
66 67 68 69 70 71 72 73
                           + ' '.join([str(i) for i in mark]) + '\t' \
                           + labels

            feature_out.write(feature_str + '\n')


if __name__ == '__main__':

Z
zhangjinchao01 已提交
74
    usage = '-p pair_file -f feature_file'
Z
zhangjinchao01 已提交
75 76
    parser = OptionParser(usage)
    parser.add_option('-p', dest='pair_file', help='the pair file')
Z
zhangjinchao01 已提交
77
    parser.add_option('-f', dest='feature_file', help='the feature file')
Z
zhangjinchao01 已提交
78 79 80

    (options, args) = parser.parse_args()

Z
zhangjinchao01 已提交
81
    extract_dict_features(options.pair_file, options.feature_file)