Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,179 questions

56,071 answers

573 users

How to remove stop words from a string in Ruby

1 Answer

0 votes
require "set"

STOP_WORDS = Set[
  "i", "me", "my", "myself", "we", "our", "ours", "ourselves", "you", "your",
  "yours", "yourself", "yourselves", "he", "him", "his", "himself", "she", "her",
  "hers", "herself", "it", "its", "itself", "they", "them", "their", "theirs",
  "themselves", "what", "which", "who", "whom", "this", "that", "these", "those",
  "am", "is", "are", "was", "were", "be", "been", "being", "have", "has", "had",
  "having", "do", "does", "did", "doing", "a", "an", "the", "and", "but", "if",
  "or", "because", "as", "until", "while", "of", "at", "by", "for", "with",
  "about", "against", "between", "into", "through", "to", "from", "in", "out",
  "on", "off", "over", "further", "then", "here", "there", "when", "where",
  "why", "how", "all", "any", "both", "each", "few", "more", "most", "other",
  "some", "such", "no", "nor", "not", "only", "own", "same", "so", "than",
  "too", "very", "can", "will", "just", "don", "should", "now"
]

def remove_stop_words(str)
  words = str.split
  filtered = words.reject { |w| STOP_WORDS.include?(w.downcase) }
  filtered.join(" ")
end

input = "a c++ and java to python a we if c# then a and aa"
puts "before remove: " + input
puts "after remove: " + remove_stop_words(input)


# run:
#
# before remove: a c++ and java to python a we if c# then a and aa
# after remove: c++ java python c# aa
#

 



answered 6 days ago by avibootz
...