From: Peter Szinek Date: 2006-04-06T21:11:10+09:00 Subject: String generalization Hello all, I am having the following problem: I have to implement a method which accepts a string and returns a generalized form of the string. Some examples: '12345' -> \d+ 'ABCDE' -> [A-Z]+ 'john.smith@my.super.server.rb' -> [a-z]+\.[a-z]+@([a-z]+\.)+[a-z] '123-45-678-90' -> (\d+-)+\d+ 'item:' -> [a-z]+: 'Peter, SZINEK': -> [A-Z][a-z]+, [A-Z]+ 'http://www.google.com' -> [a-z]+://([a-z]+\.)+[a-z] 'jd�;d:L348kddd3' -> .* So, given an example, the function should generate a generic regexp which is then used to match the instances of the same class (i.e. based on an e-mail, you create a regexp which can be used to match other emails). Of course this problem is not solvable in general. You would need more examples (both positive and negative ones, as it is proven that just using positive examples you can not generalize a pattern (generate a regular grammar desribibg it) and then machine learn/induce a grammar/etc based on that. Unfortunately i have only one positive example. So i do not need a perfect solution (which is not possible anyway) just a mostly working one. I have implemented it in java, and it works pretty well but it is ugly as hell (as any java code dealing with stuff where you need regexps, slicing, maps etc). Any ideas for a nice Ruby code solving this? Then i would call it via JRuby (and killed by the other colleagues using java but not Ruby ;-). thx, Peter