Python : find words in string without white space

2019-09-15 15:52发布

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)

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.

1条回答
Emotional °昔
2楼-- · 2019-09-15 16:41

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.

查看更多
登录 后发表回答