load_feature.py 1.8 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 os
import sys
import cPickle
import logging

20 21
logging.basicConfig(
    format='[%(levelname)s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
Z
zhangjinchao01 已提交
22 23
logging.getLogger().setLevel(logging.INFO)

24

Z
zhangjinchao01 已提交
25 26 27 28 29 30 31 32 33 34
def load_feature_c(file):
    """
    Load feature extracted by C++ interface.
    Return a list.
    file: feature file.
    """
    features = []
    f = open(file, 'r')
    for line in f:
        sample = []
35 36
        for slot in line.strip().split(";"):
            fea = [float(val) for val in slot.strip().split()]
Z
zhangjinchao01 已提交
37 38 39 40 41 42
            if fea:
                sample.append(fea)
        features.append(sample)
    f.close()
    return features

43

Z
zhangjinchao01 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
def load_feature_py(feature_dir):
    """
    Load feature extracted by python interface.
    Return a dictionary.
    feature_dir: directory of feature file.
    """
    file_list = os.listdir(feature_dir)
    file_list = [os.path.join(feature_dir, f) for f in file_list]
    features = {}
    for file_name in file_list:
        with open(file_name, 'rb') as f:
            feature = cPickle.load(f)
            features.update(feature)
            logging.info('Load feature file %s', file_name)
    return features

60

Z
zhangjinchao01 已提交
61
if __name__ == '__main__':
62
    print load_feature_py(sys.argv[1])
Z
zhangjinchao01 已提交
63
    #print load_feature_c(sys.argv[1])