How to use the opencc.OpenCC function in OpenCC

To help you get started, we’ve selected a few OpenCC examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github baohaojun / system-config / gcode / playground / test-opencc.py View on Github external
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import opencc
import sys
import os

progname = os.path.basename(sys.argv[0])
if progname != 'test-opencc.py':
    cc = opencc.OpenCC(progname + '.json')
else:
    cc = opencc.OpenCC('s2j.json')

if len(sys.argv) == 1:
    print (cc.convert("亚"))
else:
    print (cc.convert(sys.argv[1]))
github baohaojun / system-config / gcode / playground / test-opencc.py View on Github external
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import opencc
import sys
import os

progname = os.path.basename(sys.argv[0])
if progname != 'test-opencc.py':
    cc = opencc.OpenCC(progname + '.json')
else:
    cc = opencc.OpenCC('s2j.json')

if len(sys.argv) == 1:
    print (cc.convert("亚"))
else:
    print (cc.convert(sys.argv[1]))
github baohaojun / system-config / gcode / scim-cs / ime-py / bhj_ime.py View on Github external
def init():
    global _g_ime_reverse
    _g_ime_reverse = ime_reverse()

    global _g_ime_single_mode
    _g_ime_single_mode = os.path.exists(os.path.join(os.environ["HOME"], ".sdim-single"))

    global _g_ime_s2t_mode
    _g_ime_s2t_mode = os.path.exists(os.path.join(os.environ["HOME"], ".sdim-s2t"))
    global _g_opencc_s2t
    global _g_opencc_s2j
    try:
        import opencc
        _g_opencc_s2t = opencc.OpenCC("s2t.json")
        _g_opencc_s2j = opencc.OpenCC("s2j.json")
    except:
        pass

    global _g_ime_trans
    _g_ime_trans = ime_trans()

    global _g_ime_quail
    _g_ime_quail = ime_quail()

    global _g_ime_history
    _g_ime_history = ime_history()
    print('ime init complete')
    sys.stdout.flush()
if __name__ == '__main__':
github jjgod / opf-cc / opf-cc.py View on Github external
os.path.basename(output_file_path))
        # KindleGen introduced redundant data, use kindlestrip to remove that.
        data_file = file(original_output_path, 'rb').read()
        strippedFile = kindlestrip.SectionStripper(data_file)
        outf = file(output_file_path, 'wb')
        outf.write(strippedFile.getResult())
        outf.close()

    print "Removing temporary directory %s" % input_path
    shutil.rmtree(input_path)

if len(sys.argv) < 2:
    print "usage: %s "
    sys.exit(1)

with opencc.OpenCC(config="t2s.json") as converter:
    (input_file_path, extracted_path, output_file_path) = find_paths(converter)
    opf_path = find_opf_path(extracted_path)

    if opf_path:
        files = find_files_to_convert(extracted_path, opf_path)
        if len(files):
            convert_files_in_place(converter, files)
        repack_files(extracted_path, output_file_path, opf_path)
    else:
        print "%s is not in Open Packaging Format, abort." % extracted_path
        sys.exit(1)
github amowu / alfred-chinese-converter / workflow / chineseconverter.py View on Github external
Given a Chinese language string, return a list of alfred items for each of the results
  '''
  index = 0
  results = []

  config_list = [
    ('t2s.json', u'繁體到簡體', 'SimplifiedChinese.png'),
    ('s2t.json', u'簡體到繁體', 'TraditionalChinese.png'),
    ('s2tw.json', u'簡體到臺灣正體', 'TW_taiwan.png'),
    ('tw2s.json', u'臺灣正體到簡體', 'CN_china.png'),
    ('s2hk.json', u'簡體到香港繁體', 'HK_hongKong.png'),
    ('hk2s.json', u'香港繁體(香港小學學習字詞表標準)到簡體', 'CN_china.png'),
    ('s2twp.json', u'簡體到繁體(臺灣正體標準)並轉換爲臺灣常用詞彙', 'TW_taiwan.png'),
  ]
  for config_file, description, icon in config_list:
    converter = opencc.OpenCC(config=config_file, opencc_path='/usr/local/bin/opencc')
    item_value = converter.convert(query_str)
    results.append(alfred.Item(
      title=item_value,
      subtitle=description,
      attributes={
        'uid': alfred.uid(index),
        'arg': item_value,
      },
      icon=icon,
    ))
    index += 1

  return results
github victoresque / ML2017FALL / final / src / NoMLModel / preprocessing.py View on Github external
# -*- coding: utf-8 -*-
import json
import jieba
import pickle
import numpy as np
from tqdm import tqdm
from gensim.models.word2vec import Word2Vec
from opencc import OpenCC
jieba.set_dictionary('dict/dict.txt.big')

w2v = Word2Vec.load('word2vec/zh.bin')
s2t, t2s = OpenCC('s2twp'), OpenCC('tw2sp')
def toW2V(s):
    offset = 0
    offsets = []
    for i, w in enumerate(s):
        ws = t2s.convert(w)
        wt = s2t.convert(w)
        if w in w2v.wv:
            s[i] = w2v.wv[w]
        elif ws in w2v.wv:
            s[i] = w2v.wv[ws]
        elif wt in w2v.wv:
            s[i] = w2v.wv[wt]
        else:
            s[i] = np.zeros((300, ))
        offsets.append(offset)
        offset += len(w)
github yichen0831 / opencc-python / opencc / __main__.py View on Github external
help='Read original text from .')
    parser.add_argument('-o', '--output', metavar='',
                        help='Write converted text to .')
    parser.add_argument('-c', '--config', metavar='',
                        help='Configuration file')
    parser.add_argument('--in-enc', metavar='', default='UTF-8',
                        help='Encoding for input')
    parser.add_argument('--out-enc', metavar='', default='UTF-8',
                        help='Encoding for output')
    args = parser.parse_args()

    if args.config is None:
        print("Please specify a configuration file.", 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
github lepture / opencc-python / opencc.py View on Github external
def convert(text, config='t2s.json'):
    cc = OpenCC(config)
    return cc.convert(text)
github jjgod / opf-cc / opencc.py View on Github external
pass
	
	def convert(self, text):
		retv_c = self.libopencc.opencc_convert_utf8(self.od, text, len(text))
		if retv_c == -1:
			self.__perror('OpenCC error:')
			raise ConvertError()
		retv_c = cast(retv_c, c_char_p)
		str_buffer = retv_c.value
		self.libopencc.opencc_convert_utf8_free(retv_c);
		return str_buffer

if __name__ == "__main__":
	with sys.stdin as fp:
		text = fp.read()
	with OpenCC() as converter:
		print converter.convert(text)

OpenCC

Conversion between Traditional and Simplified Chinese

Apache-2.0
Latest version published 4 months ago

Package Health Score

85 / 100
Full package analysis