mag*_*tar 3 python text nlp nltk
我有一个字符串列表,这些字符串都是以"th"结尾的早期现代英语单词.这些包括神话,任命,解除等等 - 它们都是第三人称单数的共轭.
作为一个更大的项目的一部分(使用我的计算机将Gutenberg的Gargantua和Pantagruel的翻译成更像20世纪英语的东西,以便我能够更容易地阅读它)我想删除最后两三个所有这些单词中的字符并用's替换它们',然后对仍然没有现代化的单词使用稍微修改过的函数,两者都包含在下面.
我的主要问题是我从来没有设法在Python中输入我的内容.我发现这部分语言在这一点上确实令人困惑.
这是删除th的功能:
from __future__ import division
import nltk, re, pprint
def ethrema(word):
if word.endswith('th'):
return word[:-2] + 's'
Run Code Online (Sandbox Code Playgroud)
这是删除多余e的函数:
def ethremb(word):
if word.endswith('es'):
return word[:-2] + 's'
Run Code Online (Sandbox Code Playgroud)
因此,'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'
Run Code Online (Sandbox Code Playgroud)
所以,是的,这确实是打字的问题.这些是我用Python编写的第一个函数,我不知道如何将它们应用于实际的对象.
ethrema()不是该类型的方法str,您必须使用以下内容:
eth1 = [ethrema(w) for w in text]
#AND
eth2 = [ethremb(w) for w in text]
Run Code Online (Sandbox Code Playgroud)
编辑(回答评论):
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
Run Code Online (Sandbox Code Playgroud)