當前位置: 首頁>>代碼示例>>Python>>正文


Python opencc.OpenCC方法代碼示例

本文整理匯總了Python中opencc.OpenCC方法的典型用法代碼示例。如果您正苦於以下問題:Python opencc.OpenCC方法的具體用法?Python opencc.OpenCC怎麽用?Python opencc.OpenCC使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在opencc的用法示例。


在下文中一共展示了opencc.OpenCC方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: traditional2simple

# 需要導入模塊: import opencc [as 別名]
# 或者: from opencc import OpenCC [as 別名]
def traditional2simple(input_path, output_path):
    """繁體轉簡體

    :param input_path:
    :param output_path:
    :return:
    """
    # input check
    assert os.path.exists(input_path)
    # output check
    path = Path(output_path).resolve()
    path.parent.mkdir(exist_ok=True)
    # convert
    f_in = open(input_path, 'r', encoding='utf8')
    f_out = open(output_path, 'w', encoding='utf8')
    cc = opencc.OpenCC('t2s')
    for (i, line) in enumerate(f_in.readlines()):
        f_out.write(cc.convert(line))
        if i % 10000 == 0:
            logger.info("t2s %d lines complete" % i)
    f_in.close()
    f_out.close()
    logger.info("Finished Text T2S") 
開發者ID:EvilPsyCHo,項目名稱:TaskBot,代碼行數:25,代碼來源:traditional2simple.py

示例2: segment_lyric_convert_pinyin_mir1k

# 需要導入模塊: import opencc [as 別名]
# 或者: from opencc import OpenCC [as 別名]
def segment_lyric_convert_pinyin_mir1k():
    openCC = OpenCC('tw2s')
    folder_lyrics_mir1k = os.path.join(mir1k_root, 'Lyrics')
    filenames_lyrics_mir1k = list(set(get_filenames_in_folder(folder_lyrics_mir1k)))
    for fn in filenames_lyrics_mir1k:
        fn_txt = os.path.join(folder_lyrics_mir1k, fn+'.txt')
        try:
            list_line = read_mir1k_lyrics(fn_txt)
            line_simplified = openCC.convert(list_line[0])
            line_pinyin = pinyin.get(line_simplified, format='strip', delimiter=' ')
            line_char = ' '.join(fool.cut(line_simplified)[0])
            write_lyrics_one_line(filename=os.path.join(mir1k_root, 'annotation', fn + '_phrase_char.txt'),
                                  line=line_char)
            write_lyrics_one_line(filename=os.path.join(mir1k_root, 'annotation', fn + '_phrase_pinyin.txt'),
                                  line=line_pinyin)
        except UnicodeDecodeError:
            print(fn) 
開發者ID:ronggong,項目名稱:MIREX-2018-Automatic-Lyrics-to-Audio-Alignment,代碼行數:19,代碼來源:char_2_pinyin.py

示例3: __init__

# 需要導入模塊: import opencc [as 別名]
# 或者: from opencc import OpenCC [as 別名]
def __init__(self, infile, outfile):
        self.infile = infile
        self.outfile = outfile
        self.cc = opencc.OpenCC('t2s')
        self.t_corpus = []
        self.s_corpus = []
        self.read(self.infile)
        self.t2s()
        self.write(self.s_corpus, self.outfile) 
開發者ID:bamtercelboo,項目名稱:corpus_process_script,代碼行數:11,代碼來源:chinese_t2s.py

示例4: test_class_convert

# 需要導入模塊: import opencc [as 別名]
# 或者: from opencc import OpenCC [as 別名]
def test_class_convert():
    cc = OpenCC()
    text = '乾坤一擲'
    expect = '乾坤一擲'
    assert cc.convert(text) == expect

    text = '開放中文轉換'
    expect = '開放中文轉換'
    assert cc.convert(text) == expect 
開發者ID:lepture,項目名稱:opencc-python,代碼行數:11,代碼來源:test_ctypes.py

示例5: main

# 需要導入模塊: import opencc [as 別名]
# 或者: from opencc import OpenCC [as 別名]
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-i', '--input', metavar='<file>',
                        help='Read original text from <file>.')
    parser.add_argument('-o', '--output', metavar='<file>',
                        help='Write converted text to <file>.')
    parser.add_argument('-c', '--config', metavar='<conversion>',
                        help='Conversion')
    parser.add_argument('--in-enc', metavar='<encoding>', default='UTF-8',
                        help='Encoding for input')
    parser.add_argument('--out-enc', metavar='<encoding>', default='UTF-8',
                        help='Encoding for output')
    args = parser.parse_args()

    if args.config is None:
        print("Please specify a conversion.", file=sys.stderr)
        return 1

    cc = OpenCC(args.config)

    with io.open(args.input if args.input else 0, encoding=args.in_enc) as f:
        input_str = f.read()
    output_str = cc.convert(input_str)
    with io.open(args.output if args.output else 1, 'w',
              encoding=args.out_enc) as f:
        f.write(output_str)

    return 0 
開發者ID:yichen0831,項目名稱:opencc-python,代碼行數:31,代碼來源:__main__.py

示例6: setUp

# 需要導入模塊: import opencc [as 別名]
# 或者: from opencc import OpenCC [as 別名]
def setUp(self):
        # Unitialized convertor object
        self.openCC = OpenCC()
        # Constructor intitialized convertor object
        self.openCC2 = OpenCC('hk2s') 
開發者ID:yichen0831,項目名稱:opencc-python,代碼行數:7,代碼來源:conversion_test.py


注:本文中的opencc.OpenCC方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。