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 7884569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:50:06+00:00 2026-06-03T04:50:06+00:00

I have an array of keywords, which can have a variable length. For this

  • 0

I have an array of keywords, which can have a variable length. For this example imagine there are 50:

keywords = ['dog','cat','monkey'...'bird']

I have an array of sentences (again of a variable length) I want to loop through, searching for each of the keywords.

sentences = [ ['My dog ate cat food'], ['I went to the store.'], ... ]

If the sentence contains any of the keywords, then I’m moving it to a new “matched” array. So in Ruby, my code looks something like this:

sentences.each do |sentence|
  keywords.each do |keyword|
    if sentence.match(/\b#{keyword}\b/i)
      matched << sentence
    end
  end
end

This takes quite a while and seems really inefficient–especially if I have a large keyword list and a large sentence list. I’m the first to admit my Ruby development isn’t that great yet–is there an easier, more efficient way to do this?

I’m using MongoDB to store the keywords and sentences. If there is a better method using the database, I’d love to explore it.

  • 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-06-03T04:50:08+00:00Added an answer on June 3, 2026 at 4:50 am

    I’ve not used MonogDB before, but you can optimize your ruby code a bit. Since you only care if there is a match of any keyword in the sentence, I would push the logic into the Ruby regexp engine:

    regexp = keywords.map { |keyword| Regexp.quote(keyword) }.join('|')
    
    matched = sentences.select do |sentence|
      sentence[0].match(/\\b(?:#{regexp})\\b/i)
    end
    

    What that does is makes one regexp that combines all your keywords. That way you’re only looping over the sentences rather than each keyword.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php multidimensional array which looks like this: $fields = array( array('input',
There are two places where I have observed this. Both are AJAX calls which
I have an array with keywords and I have a string, which may contain
Basically, I have an array of keywords, and a piece of text. I am
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
I have array result like this: Array ( [0] => stdClass Object ( [id_global_info]
Hello i have array like this [0] => Array ( [ExamMonth] => Array (
I have a string where different predefined keywords introduce different data. Is there a
I have a property which is an array of items, but when I come
is it possible to declare a variable length array with global scope in objective-c?

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.