|
import spacy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import nltk |
|
|
|
|
|
|
|
|
|
|
|
import jieba |
|
|
|
from sacremoses import MosesTokenizer |
|
from subword_nmt import apply_bpe |
|
import codecs |
|
|
|
jieba1 = jieba.Tokenizer() |
|
jieba2 = jieba.Tokenizer() |
|
jieba2.load_userdict('model2_data/dict.zh.txt') |
|
|
|
mt_zh = MosesTokenizer(lang='zh') |
|
with codecs.open('model2_data/bpecode.zh', 'r', 'utf-8') as f: |
|
bpe_zh_f = apply_bpe.BPE(f) |
|
|
|
|
|
mt_en = MosesTokenizer(lang='en') |
|
with codecs.open('model2_data/bpecode.en', 'r', 'utf-8') as f: |
|
bpe_en_f = apply_bpe.BPE(f) |
|
|
|
def spacy_tokenize(line): |
|
|
|
|
|
|
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
def nltk_tokenize(line): |
|
|
|
|
|
|
|
|
|
return [] |
|
|
|
|
|
def jieba_tokenize(line): |
|
|
|
tokens = list(jieba1.cut(line.strip())) |
|
|
|
return tokens |
|
|
|
def tokenize(line, mode): |
|
if mode == "汉译英" : |
|
return jieba_tokenize(line) |
|
else : |
|
return nltk_tokenize(spacy_tokenize(line)) |
|
|
|
|
|
def jieba_tokenize2(line): |
|
tokens = list(jieba2.cut(line.strip())) |
|
return tokens |
|
|
|
def mt_bpe_zh(line): |
|
zh_tok = mt_zh.tokenize(line) |
|
bpe_zh = bpe_zh_f.segment_tokens(zh_tok) |
|
print(bpe_zh) |
|
return bpe_zh |
|
|
|
def mt_bpe_en(line): |
|
en_tok = mt_en.tokenize(line) |
|
bpe_en = bpe_en_f.segment_tokens(en_tok) |
|
print(bpe_en) |
|
return bpe_en |
|
|
|
def tokenize2(line, mode): |
|
if mode == "汉译英" : |
|
return mt_bpe_zh(' '.join(jieba_tokenize2(line))) |
|
else : |
|
return mt_bpe_en(line) |