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

  • SEARCH
  • Home
  • 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
  • 2 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

Related Questions

Let's say I have a string Hello and a list words = ['hello', 'Hallo',
Say I have the string 1,2,3,4,5 and I want to convert this to an
hey guys, i have a string which contains say 100 words, now i want
Lets say I have a list of words and I want to print them
Say I have a string: the quick brown fox jumped over the moon.this text
I have a string that if it is longer than lets say 10 words,
Let's say that I have string: -dog--cat--d-- I would like to find all words
Let's say I have a list of words: resign resins redyed resist reeded I
Say I have the following string: s := 'This , is, the Delphi ,
Let's say I have a vector string: words <- c(Guardian,ia,librarian) If I grep for

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.