ruby - Given a String and an array of Strings, how do I efficiently calculate the occurrences of the array in String? -
let's assume text
string , contains text. tags
array of strings.
text = <<-eos lorem ipsum dolor sit amet, consectetur adipisicing elit, sed eiusmod tempor incididunt ut labore et dolore magna aliqua. ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. eos tags = [ "some", "in", "dolor", "laborum", "missing" ]
the algorithm should return tags contained @ least once in text
. in example above
[ "in", "dolor", "laborum" ]
the resulting array not required sorted. also, don't need know number of occurrences of each tag in text
.
i came few solutions, none of them convinced me. suggestion?
text.gsub!(/[[:punct:]]/,"").split p tags.select{|x| x if text.include?(x)}
Comments
Post a Comment