Python:在没有空格的字符串中查找单词 [英] Python : find words in string without white space
问题描述
我正在尝试创建一个函数来查找没有空格的字符串中的单词:'Daysaregood'。
我迭代每个字母,直到我通过比较基于已经迭代的字母的列表,使用附魔模块附魔来查找该单词是否存在。
这就是我试过的:
I'm trying to make a function to look for words in a string without white space : 'Daysaregood' . i iterate for every letter until i find if the word exists by comparing with list based on already iterated letter, using enchant the module enchant. and this is what i tried:
import enchant
import time
fulltext =[]
def work(out):
if len(out)>0:
word = ''
wd = ""
# iterate for every Letter
for i in out:
word = word + i
print word
d = enchant.Dict('en_US')
# a list of words to compare to
list = d.suggest(word.title())
print list
#check if word exists
if word.title() in list :
print 'Word found'
wd = word
else:
print 'Word not found'
print '\n'+wd
fulltext.append(str(wd))
time.sleep(2)
work(out[len(wd):])
else:
print '\n fulltext : '
print fulltext
word="Daysaregood"
work(word)
现在对于这个文本脚本运行就像我想要的,我得到一个列表像这样:
['天','是','好']。
但是当我尝试像'spaceshuttle'这样的东西时,这个功能会与'space'混淆并在'shuttle'中加入's',所以我得到了这个:
['spaces','hut', 't','l','e']。
我的目标是单独返回每个单词并将它们存储到列表中。
任何帮助都表示赞赏。
Now for this text the scripts runs like i want, i get a list like this : ['Days', 'are', 'good']. But when i try something like 'spaceshuttle', the function gets confused with 'space' and steels the 's' in 'shuttle' so i get this : ['spaces', 'hut', 't', 'l', 'e']. My goal is to take return every word by itself and store them into a list. Any help is appreciated.
推荐答案
您的任务的问题是所需的输出不符合严格的规则,本身。如果你输入'菠萝'
,你会期望 ['pine','apple']
或 [ '菠萝']
?要预测这一点是相当困难/不可能的。
The issue with your task is that the desired output doesn't follow strict rules, per se. If you were to input 'pineapple'
, would you expect ['pine', 'apple']
or ['pineapple']
? It would be rather difficult / impossible to have it predict this.
这篇关于Python:在没有空格的字符串中查找单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!