ruby - Remove duplicate text from multiple strings -
i have:
a = "this product property b , propery c. buy now!" b = "this product b property x , propery y. buy now!" c = "this product c having no properties. buy now!"
i'm looking algorithm can do:
> magic(a, b, c) => ['a property b , propery c', 'b property x , propery y', 'c having no properties']
i have find duplicates in 1000+ texts. super performance isn't must, nice.
-- update
i'm looking sequence of words. if:
d = 'this product d text engraving: "buy". buy now!'
the first "buy" should not duplicate. i'm guessing have use threshold of n words following eachother in order seen duplicate.
def common_prefix_length(*args) first = args.shift (0..first.size).find_index { |i| args.any? { |a| a[i] != first[i] } } end def magic(*args) = common_prefix_length(*args) args = args.map { |a| a[i..-1].reverse } = common_prefix_length(*args) args.map { |a| a[i..-1].reverse } end
a = "this product property b , propery c. buy now!" b = "this product b property x , propery y. buy now!" c = "this product c having no properties. buy now!" magic(a,b,c) # => ["a property b , propery c", # "b property x , propery y", # "c having no properties"]
Comments
Post a Comment