1

I have a string like that: "Men's Beech River Cable T-Shirt" how can I get category from this string?

str = "Men's Beech River Cable T-Shirt"
str2 = "MEN'S GOOSE EYE MOUNTAIN DOWN VEST"
cat1 = str1.split.last # T-Shirt
cat2 = str2.split.last # VEST

TOPS = %w(jacket vest coat blazer parka sweater shirt polo t-shirt)

Desired result:

category_str1 = "Tops" # Since T-Shirt (shirt) is in TOPS constant.
category_str2 = "Tops" # Since vest is in TOPS const.

I don't know how to describe my problem better, I hope you understand it from example provided.

2
  • TOPS has a shirt not a t-shirt, should those both be in TOPS? Commented Oct 16, 2015 at 15:41
  • Updated, but even if part of string matches - it should set category to Tops Commented Oct 16, 2015 at 15:43

3 Answers 3

3
str = "Men's Beech River Cable T-Shirt"
cat_orig = str.split.last # T-Shirt

TOPS = %w(jacket vest coat blazer parka sweater shirt polo)
RE_TOPS = Regexp.union(TOPS)
category = "Tops" if RE_TOPS =~ cat_orig.downcase

Note there are no comma's in the %w() style array syntax.

Sign up to request clarification or add additional context in comments.

1 Comment

I like this, you could also use union = Regexp.union(TOPS); pattern = Regexp.new(union.source, Regexp::IGNORECASE) instead of down casing the string.
1
str = "Men's Beech River Cable T-Shirt"
cat_orig = str.split.last # T-Shirt

TOPS = %w(jacket vest coat blazer parka sweater shirt polo) # suppressed the comma to get a clean array

category = "Tops" if !cat_orig[/(#{TOPS.join("|")})/i].nil?

The join on the TOPS Array build an alternative regex of the form:

(jacket|vest|coat|blazer|parka|sweater|shirt|polo)

If any of those word is present in cat_orig, the return will be the matched word, if not it will return nil.

Note the leading i in the regex to makes it case insensitive.

Comments

1

The best way to do this is through a hash, not an array. Let's say your caetgories look something like this

categories = { "TOPS" => ["shirt", "coat", "blazer"],
               "COOKING" => ["knife", "fork", "pan"] }

We can then loop through each category and find if their values include the word in the string

categories.each do |key, value|    
  puts key if str.downcase.split(' ').any? { |word| categories[key].include?(word) }
end

Loop through each category, and find if the category has a word that the string has.

Note: This does not yet search for substrings.

1 Comment

nice, but what about case should i do str.split.downcase.any?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.