BERT多语言版本可以用于处理混合了中英文单词的文本,但是在处理之前有个小坑——需要用空格对句子进行分字处理,即对英文保留原原装,中文以字为单位进行分割。
经过下面trans方法处理过的句子,可以用于后续预训练模型tokenizer方法中。
def trans(sen):
buf = ''
for word in sen:
if '\u4e00' <= word <= '\u9fff':
buf += f' {word} ' # 中文字符前后加上空格
else:
buf += word
return buf.replace(' ', ' ')
if __name__ == "__main__":
chline = '国无德不兴,人无德不立。'
enline = 'The use of citations in evaluative scientometrics as a proxy for quality'
chenline = '无德不兴The,无德不立 use of无德不立citations in evaluative scientometrics as a proxy for quality'
print(trans(chline))
print(trans(enline))
print(trans(chenline))