Python 使用NLTK将早期现代英语转换为20世纪的拼写
我有一个字符串列表,这些字符串都是以“th”结尾的早期现代英语单词,包括has、appointeth、demandeth等,它们都是第三人称单数的共轭形式 作为一个更大项目的一部分(使用我的计算机将Gargantua和Pantagruel的Gutenberg etext转换成更像20世纪英语的东西,以便我能够更容易地阅读),我想从所有这些单词中删除最后两三个字符,并用“s”替换它们,然后对仍然没有现代化的单词使用稍微修改的功能,这两个词都包括在下面 我的主要问题是,我从来没有设法在Python中正确地键入内容。在这一点上,我发现语言的这一部分确实令人困惑 以下是删除th的函数:Python 使用NLTK将早期现代英语转换为20世纪的拼写,python,text,nlp,nltk,Python,Text,Nlp,Nltk,我有一个字符串列表,这些字符串都是以“th”结尾的早期现代英语单词,包括has、appointeth、demandeth等,它们都是第三人称单数的共轭形式 作为一个更大项目的一部分(使用我的计算机将Gargantua和Pantagruel的Gutenberg etext转换成更像20世纪英语的东西,以便我能够更容易地阅读),我想从所有这些单词中删除最后两三个字符,并用“s”替换它们,然后对仍然没有现代化的单词使用稍微修改的功能,这两个词都包括在下面 我的主要问题是,我从来没有设法在Python中
from __future__ import division
import nltk, re, pprint
def ethrema(word):
if word.endswith('th'):
return word[:-2] + 's'
以下是删除无关e的函数:
def ethremb(word):
if word.endswith('es'):
return word[:-2] + 's'
因此,“abateth”和“Accountth”这两个词会通过ethrema,但不会通过ethremb(ethrema),而“abhorreth”这两个词则需要同时通过
如果有人能想出一个更有效的方法来做这件事,我洗耳恭听
以下是我非常业余地尝试在需要现代化的标记化单词列表上使用这些函数的结果:
>>> eth1 = [w.ethrema() for w in text]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'ethrema'
>eth1=[w.ethrema()表示文本中的w]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“str”对象没有属性“ethrema”
所以,是的,这真的是一个打字的问题。这些是我用Python编写的第一个函数,我不知道如何将它们应用于实际对象。ethrema()
不是str
类型的方法,您必须使用以下方法:
eth1 = [ethrema(w) for w in text]
#AND
eth2 = [ethremb(w) for w in text]
编辑(回答评论):
ethremb(ethrema(word))
在您对函数进行一些小更改之前无法工作:
def ethrema(word):
if word.endswith('th'):
return word[:-2] + 's'
else
return word
def ethremb(word):
if word.endswith('es'):
return word[:-2] + 's'
else
return word
#OR
def ethrema(word):
if word.endswith('th'):
return word[:-2] + 's'
elif word.endswith('es'):
return word[:-2] + 's'
else
return word
伟大的做一些像ethremb(ethrema(word))的事情怎么样?