Truecasing is the process of restoring case information to badly-cased or non- cased text.
pip3 install truecase
对英文进行 truecase
>>> import truecase
>>> truecase.get_true_case('hey, what is the weather in new york?')
'Hey, what is the weather in New York?''
README 中提到训练只说到:
TODO. For now refer to Trainer.py
那么我们便下载源码,来观察 Trainer.py 文件;
1、准备语料
Train.py 文件中 main 函数中的 corpus 来自 nltk,将某部分语料打印看看:
ret = nltk.corpus.brown.sents()
print(ret, type(ret))
得到这样的结果,是句子分词后的数组
[['The', 'Fulton', 'County', 'Grand', 'Jury', 'said', 'Friday', 'an', 'investigation', 'of', "Atlanta's", 'recent', 'primary', 'election', 'produced', '``', 'no', 'evidence', "''", 'that', 'any', 'irregularities', 'took', 'place', '.'], ['The', 'jury', 'further', 'said', 'in', 'term-end', 'presentments', 'that', 'the', 'City', 'Executive', 'Committee', ',', 'which', 'had', 'over-all', 'charge', 'of', 'the', 'election', ',', '``', 'deserves', 'the', 'praise', 'and', 'thanks', 'of', 'the', 'City', 'of', 'Atlanta', "''", 'for', 'the', 'manner', 'in', 'which', 'the', 'election', 'was', 'conducted', '.'], ...] '''
这里我要训练一个 越南语vi 的truecaser,简单粗暴,读取文件后使用空格分词;
def train_vi():corpus = []file_path = 'xx/vi.txt'for line in open(file_path):arr = line.strip().split(' ')corpus.append(arr)trainer = Trainer()trainer.train(corpus)trainer.save_to_file("data/vi.dist")
2、运行训练
cd xx/truecase/
python Trainer.py
训练完成后,模型文件被保存到 data/vi.dist
3、测试
def test_vi():dist_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),"data/vi.dist")caser = TrueCaser(dist_file_path)text = 'Đến thời điểm này, Bộ Xây dựng nhìn nhận, tình trạng giá đất tăng nóng cục bộ tại một số ' ret = caser.get_true_case(text.lower()) # Đến thời điểm này, Bộ Xây dựng nhìn nhận, tình trạng giá đất tăng nóng cục bộ tại một sốprint(ret)
伊织 2022-11-24(四)
上一篇:北京有哪些适合孩子玩
下一篇:什么的歌唱词语