NLP - truecase
admin
2024-02-12 04:00:58

文章目录

    • 关于 truecase
      • 安装
    • 简单使用
    • 训练 Train


关于 truecase

Truecasing is the process of restoring case information to badly-cased or non- cased text.

  • 相关论文:https://www.cs.cmu.edu/~llita/papers/lita.truecasing-acl2003.pdf
  • 本文基于这个repo 实现 : https://github.com/daltonfury42/truecase

安装

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?''

训练 Train

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(四)

相关内容

热门资讯

浪潮信息:投资者询问存储涨价影... 投资者提问:尊敬的董秘您好:随着 DRAM/NAND 等存储产品价格持续上行,想请教:1)公司如何看...
物流脱碳,货主企业应该怎么做? 来源:@经济观察报微博2024年,交通运输业占全球温室气体排放总量的15%,成为全球二氧化碳主要来源...
万科未来之光 :三期提前15天... 转自:财联社家的承诺,是穿越时光的坚守;生活的圆满,是超越预期的惊喜。当蓝图化为实景,期待落定成诗,...
一... 魔音变声器是一款功能强大的语音变声软件,适用于各种场景,如微信聊天、语音通话、视频通话等。它还能在游...
全国多地张灯结彩、灯光璀璨“欢... 转自:央视网云南丽江:古城欢腾 民族打跳迎新年为迎接新年到来,丽江古城开展了云南特色的“打跳”舞蹈。...