Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 192017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:23:26+00:00 2026-05-11T16:23:26+00:00

Say I have a string of words: ‘a b c d e f’ .

  • 0

Say I have a string of words: 'a b c d e f'. I want to generate a list of multi-word terms from this string.

Word order matters. The term 'f e d' shouldn’t be generated from the above example.

Edit: Also, words should not be skipped. 'a c', or 'b d f' shouldn’t be generated.

What I have right now:

doc = 'a b c d e f'
terms= []
one_before = None
two_before = None
for word in doc.split(None):
    terms.append(word)
    if one_before:
        terms.append(' '.join([one_before, word]))
    if two_before:
        terms.append(' '.join([two_before, one_before, word]))
    two_before = one_before
    one_before = word

for term in terms:
    print term

Prints:

a
b
a b
c
b c
a b c
d
c d
b c d
e
d e
c d e
f
e f
d e f

How would I make this a recursive function so that I can pass it a variable maximum number of words per term?

Application:

I’ll be using this to generate multi-word terms from readable text in HTML documents. The overall goal is a latent semantic analysis of a large corpus (about two million documents). This is why keeping word order matters (Natural Language Processing and whatnot).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:23:26+00:00Added an answer on May 11, 2026 at 4:23 pm

    This isn’t recursive, but I think it does what you want.

    doc = 'a b c d e f'
    words = doc.split(None)
    max = 3          
    
    
    for index in xrange(len(words)):    
        for n in xrange(max):
            if index + n < len(words):           
                print ' '.join(words[index:index+n+1])   
    

    And here’s a recursive solution:

    def find_terms(words, max_words_per_term):       
        if len(words) == 0: return []
        return [" ".join(words[:i+1]) for i in xrange(min(len(words), max_words_per_term))] + find_terms(words[1:], max_words_per_term)
    
    
    doc = 'a b c d e f'
    words = doc.split(None) 
    for term in find_terms(words, 3):
        print term
    

    Here’s the recursive function again, with some explaining variables and comments.

    def find_terms(words, max_words_per_term):   
    
        # If there are no words, you've reached the end. Stop.    
        if len(words) == 0:
            return []      
    
        # What's the max term length you could generate from the remaining 
        # words? It's the lesser of max_words_per_term and how many words 
        # you have left.                                                         
        max_term_len = min(len(words), max_words_per_term)       
    
        # Find all the terms that start with the first word.
        initial_terms = [" ".join(words[:i+1]) for i in xrange(max_term_len)]
    
        # Here's the recursion. Find all of the terms in the list 
        # of all but the first word.
        other_terms = find_terms(words[1:], max_words_per_term)
    
        # Now put the two lists of terms together to get the answer.
        return initial_terms + other_terms 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's at google. VS doesn't come with <inttypes.h> May 12, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer When you lock bits using a rectangle with an offset,… May 12, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer jQuery Forms plugin and an ASP.NET MVC action should do… May 12, 2026 at 1:07 am

Related Questions

Say I have a string of words: 'a b c d e f' .
I'm looking for an algorithm, or at least theory of operation on how you
I have a function that has several outputs, all of which native, i.e. integers
Is there any way to split a long string of HTML after N words?
Say I have these two strings: Some Text here and some text Here And

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.