gpt4 book ai didi

python - NLTK 中是否有任何用于文本规范化和规范化的类?

转载 作者:IT老高 更新时间:2023-10-28 20:54:52 27 4
gpt4 key购买 nike

大量的 NLTK 文档和示例专门用于词形还原和词干提取,但在规范化等问题上却很少:

  • 将所有字母转换为小写或大写
  • 删除标点符号
  • 将数字转化为文字
  • 删除重音符号和其他变音符号
  • 扩展缩写
  • 删除停用词或“太常见”的词
  • 文本规范化(tumor = tumour, it's = it is)

请指出我在 NLTK 中的哪个位置进行挖掘。欢迎任何用于上述目的的 NLTK 等价物(JAVA 或任何其他)。谢谢。

UPD。我已经为文本到语音的目的编写了一个文本规范化的 Python 库 https://github.com/soshial/text-normalization .它也可能适合你。

最佳答案

同样在 NLTK 规范中,很多(子)任务都是使用纯 python methods 来解决的。 .

a) 将所有字母转换为小写或大写

text='aiUOd'
print text.lower()
>> 'aiuod'
print text.upper()
>> 'AIUOD'

b) 删除标点符号

text='She? Hm, why not!'
puncts='.?!'
for sym in puncts:
text= text.replace(sym,' ')
print text
>> 'She Hm why not '

c) 将数字转换为单词

在这里,写几个liner并不是那么麻烦,但是如果你用谷歌搜索的话,已经有很多现有的解决方案了。 Code snippets , libraries等等

d) 删除重音符号和其他变音符号

查找点b),只需创建以变音符号为puncts

的列表

e) 扩展缩写

用缩写创建字典:

text='USA and GB are ...'
abbrevs={'USA':'United States','GB':'Great Britain'}
for abbrev in abbrevs:
text= text.replace(abbrev,abbrevs[abbrev])
print text
>> 'United States and Great Britain are ...'

f) 删除停用词或“太常见”的词

创建一个包含停用词的列表:

text='Mary had a little lamb'
temp_corpus=text.split(' ')
stops=['a','the','had']
corpus=[token for token in temp_corpus if token not in stops]
print corpus
>> ['Mary', 'little', 'lamb']

g) 文本规范化(tumor = tumour, it's = it is)

用于肿瘤-> 肿瘤使用regex .

最后但同样重要的是,请注意,以上所有示例通常都需要在真实文本上进行校准,我将它们作为前进的方向。

关于python - NLTK 中是否有任何用于文本规范化和规范化的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9227527/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com