infer_by_code.py 1.4 KB
Newer Older
W
wuzewu 已提交
1
import os
W
wuzewu 已提交
2
import paddlehub as hub
W
wuzewu 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46


def infer_with_input_text():
    # get lac module
    lac = hub.Module(module_dir="hub_module_lac")

    test_text = ["今天是个好日子", "天气预报说今天要下雨", "下一班地铁马上就要到了"]

    # get the input keys for signature 'lexical_analysis'
    data_format = lac.processor.data_format(sign_name='lexical_analysis')
    key = list(data_format.keys())[0]

    # set input dict
    input_dict = {key: test_text}

    # execute predict and print the result
    results = lac.lexical_analysis(data=input_dict)
    for index, result in enumerate(results):
        hub.logger.info(
            "sentence %d segmented result: %s" % (index + 1, result['word']))


def infer_with_input_file():
    # get lac module
    lac = hub.Module(module_dir="hub_module_lac")

    # get the input keys for signature 'lexical_analysis'
    data_format = lac.processor.data_format(sign_name='lexical_analysis')
    key = list(data_format.keys())[0]

    # parse input file
    test_csv = os.path.join("resources", "test", "test.csv")
    test_text = hub.io.reader.csv_reader.read(test_csv)["TEXT_INPUT"]

    # set input dict
    input_dict = {key: test_text}
    results = lac.lexical_analysis(data=input_dict)
    for index, result in enumerate(results):
        hub.logger.info(
            "sentence %d segmented result: %s" % (index + 1, result['word']))


if __name__ == "__main__":
    infer_with_input_text()