Python将输出转换为senten

2024-04-27 13:47:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚开始学python。我试着把一个句子分成几个单词再重新连接起来。文件大.txt有一些词如青年,管理员等。问题是在最后的程序:活套,这每行产生一个输出。在

Correct是在此代码之前定义的另一个过程,用于更正每个单词

代码如下:

zebra = 'Yout caretak taking care of something'

count = len(re.findall(r'\w+', zebra))

def looper(a,count):
words = nltk.word_tokenize(zebra)
for i in range(len(words)):
    X = correct(words[i])
    print (X)

final = looper(zebra)

它产生的输出:

^{pr2}$

我应该如何获取所有的个人输出并造句:

预期结果:

^{3}$

如果你需要更多的细节,请告诉我。在

提前谢谢


Tags: 文件代码程序txtlen定义管理员count
3条回答
zebra = 'Yout caretak taking care of something'

count = len(re.findall(r'\w+', zebra))

def looper(a,count):
words = nltk.word_tokenize(zebra)
for i in range(len(words)):
    X = correct(words[i])
    print X,    
final = looper(zebra)

只需在X->;print X,之后添加

>>> import nltk
>>> zebra = 'Yout caretak taking care of something'
>>> for word in nltk.word_tokenize(zebra):
...     print word
... 
Yout
caretak
taking
care
of
something

然后$ sudo pip install pyenchant(参见https://pythonhosted.org/pyenchant/api/enchant.html)和:

^{pr2}$

然后尝试:

>>> for word in nltk.word_tokenize(zebra):
...     print [i for i in dictionary.suggest(word) if word in i]
... 
['Youth']
['caretaker']
['takings', 'staking']
['cares', 'scare', 'carer', 'caret', 'cared']
['oft', 'off']
['somethings', 'something']

所以:

>>> " ".join([[word if dictionary.check(word) else i for i in dictionary.suggest(word) if word in i][0] for word in nltk.word_tokenize(zebra)])
'Youth caretaker taking care of something'

使用列表理解:

print " ".join([ correct(words[i]) for i in range(len(words)) ])

应该是这样的:

^{pr2}$

单词应该在函数之外,你不需要每次循环都得到单词。在

你也可以用这个:

print " ".join([ correct(i) for i in words ])

以下是正确的方法:

zebra = 'Yout caretak taking care of something'
words = nltk.word_tokenize(zebra)
print " ".join([ correct(i) for i in words ])

这里不需要函数,因为单词是单词的列表,所以可以迭代和连接。在

在您的代码中:

zebra = 'Yout caretak taking care of something'
words = nltk.word_tokenize(zebra)
for x in words:
    print correct(x),

演示:

>>> zebra = 'Yout caretak taking care of something'
>>> words = nltk.word_tokenize(zebra)
>>> words
['Yout', 'caretak', 'taking', 'care', 'of', 'something']

正如您所见,nltk.word_tokenize给您一个单词列表,这样您可以轻松地遍历它们

相关问题 更多 >