DEV Community

Akshat
Akshat

Posted on

alternate way of doing word split/phrase segmentation in python

Doing word split with recursion felt a bit complex to me , so I tried to do it in an easier way.



Image description



  • The Hard Way (recursion) --

def word_split(phrase,list_of_words, output = None):

    if output is None:    #Base Case / Initial call
        output = []


    for word in list_of_words:        


        if phrase.startswith(word):                       

            output.append(word)

            return word_split(phrase[len(word):],list_of_words,output)    # Recursive Call


    return output        # Result

Enter fullscreen mode Exit fullscreen mode

gives

Image description


  • The Easy Way (indexing/for loop) -
def word_split_2(phrase, word_list):
    output = []

    for i in word_list:
        if i in phrase:
            output.append(i)

    return output


Enter fullscreen mode Exit fullscreen mode

Image description


this approach might be wrong , please correct me if it is

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

đź‘Ą Ideal for solo developers, teams, and cross-company projects

Learn more