使用NLTK将早期现代英语转化为20世纪拼写
我有一串字符串,都是一些早期现代英语的单词,结尾都是“th”。比如有“hath”、“appointeth”、“demandeth”等等,它们都是第三人称单数的形式。
作为一个更大项目的一部分(我想用我的电脑把《巨人传》和《潘塔格鲁埃尔》这本书的古腾堡电子文本转换成更像20世纪英语的样子,这样我就能更容易地阅读),我想把这些单词的最后两个或三个字母去掉,然后用一个“s”替换掉,再对那些还没有现代化的单词使用稍微修改过的函数,下面都有列出。
我主要的问题是,我在Python中总是打错字。我发现这部分语言现在对我来说真的很困惑。
这是一个用来去掉“th”的函数:
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”和“accuseth”这样的单词会通过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'
所以,问题确实在于打字。这是我第一次在Python中写的函数,我完全不知道怎么把它们应用到实际的对象上。
1 个回答
6
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