Forever-Young / mrab-regex-hg

Automatically exported from code.google.com/p/mrab-regex-hg
0 stars 0 forks source link

= for fuzzy matches #41

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
= operator could be pretty handy for fuzzy matches, finding only erroneous 
text. For example, in a list of hotmail email accounts, you could search for 
misspells like '@(hotmail\.com){e=1}'. This will save the user an extra "grep 
-v" for filtering out correct emails in the list of matches.

Original issue reported on code.google.com by jfcga...@gmail.com on 17 Jan 2012 at 12:49

GoogleCodeExporter commented 9 years ago
or even something like this would be more powerful:
'@(hotmail\.com){0<e<3}'
matching only texts with 1 or 2 errors

Original comment by jfcga...@gmail.com on 17 Jan 2012 at 12:53

GoogleCodeExporter commented 9 years ago
A regex such as:

    @(hotmail\.com){e=1}

can be we written as:

    @(?>(hotmail\.com){e<=1})(?<!@hotmail\.com)

although that's not quite as convenient, I admit! :-)

Note that the fuzzy part needs to be in an atomic group in order to stop it 
backtracking to find a worse match. For example, given the string 
"@hotmail.comb", the fuzzy part will match "@hotmail.com" with 0 errors, then 
the negative look-behind will reject it, so the fuzzy part will match 
"@hotmail.comb" with 1 error.

I'm not sure how easy it'll be to add a lower limit; such a problem could still 
occur.

Original comment by re...@mrabarnett.plus.com on 18 Jan 2012 at 3:37

GoogleCodeExporter commented 9 years ago
I think I've figured out how to do it, but how much demand is for it? You gave 
an example, but is that a real use case?

Original comment by re...@mrabarnett.plus.com on 18 Jan 2012 at 6:25

GoogleCodeExporter commented 9 years ago
I am fixing tags for 25k+ text documents for a web site, so I do have a real 
(different) use case. That was just an example. But I think it would be a 
really nice feature for regex module...

Original comment by jfcga...@gmail.com on 18 Jan 2012 at 7:55

GoogleCodeExporter commented 9 years ago
Could you provide a few test cases?

Original comment by re...@mrabarnett.plus.com on 18 Jan 2012 at 8:06

GoogleCodeExporter commented 9 years ago
here is a real example translated into english

3 servic detection
1 service detect
5 service detecti
46 service detection
1 in service detection

The site has manually entered tags, and their frequencies from 25k+ 
(non-english) text documents. Most of the time the correct one has a high 
frequency, and anything that is close enough to a correct one (except itself) 
should probably get fixed..

Original comment by jfcga...@gmail.com on 18 Jan 2012 at 8:21

GoogleCodeExporter commented 9 years ago
What fuzzy regex would you use to match the incorrect strings in your example? 
Would it be this:

(?:^\d+ service detection$){1<=e<=3}

Original comment by re...@mrabarnett.plus.com on 18 Jan 2012 at 9:26

GoogleCodeExporter commented 9 years ago
no no, the first part is the frequency of a tag, not part of it. I would search 
a match with:
r = compile(r'(?:service detection){0<e<5}')
m = r.match(str)
if m:

Original comment by jfcga...@gmail.com on 18 Jan 2012 at 9:33

GoogleCodeExporter commented 9 years ago
or '(?:service detection){0<e<5}$' is also a possibility..

Original comment by jfcga...@gmail.com on 18 Jan 2012 at 9:47

GoogleCodeExporter commented 9 years ago
Added in regex 0.1.20120119.

Note that it supports only constraints of the form e<=3 or 1<=e<=3 ("<" is also 
allowed), but not "=".

Original comment by re...@mrabarnett.plus.com on 19 Jan 2012 at 3:54

GoogleCodeExporter commented 9 years ago
thanks ^_^

Original comment by jfcga...@gmail.com on 19 Jan 2012 at 8:31