Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding similar number patterns in table

Tags:

php

mysql

Ok, let's suppose we have members table. There is a field called, let's say, about_member. There will be a string like this 1-1-2-1-2 for everybody. Let's suppose member_1 has this string 1-1-2-2-1 and he searches who has the similar string or as much similar as possible. For example if member_2 has string 1-1-2-2-1 it will be 100% match, but if member_3 has string like this 2-1-1-2-1 it will be 60% match. And it has to be ordered by match percent. What is the most optimal way to do it with MYSQL and PHP? It's really hard to explain what I mean, but maybe you got it, if not, ask me. Thanks.

Edit: Please give me ideas without Levenshtein method. That answer will get bounty. Thanks. (bounty will be announced when I will be able to do that)

like image 397
good_evening Avatar asked Aug 08 '10 22:08

good_evening


People also ask

Can you spot patterns in the number grid?

Number grids can be used to explore number patterns. For example, children can start at zero and count by 2s. If they color each box as they go, they will have colored all the even numbers. If they start at one and count by 2s they will color all the odd numbers.


1 Answers

convert your number sequences to bit masks and use BIT_COUNT(column ^ search) as similarity function, ranged from 0 (= 100% match, strings are equal) to [bit length] (=0%, strings are completely different). To convert this similarity function to the percent value use

100 * (bit_length - similarity) / bit_length

For example, "1-1-2-2-1" becomes "00110" (assuming you have only two states), 2-1-1-2-1 is "10010", bit_count(00110 ^ 10010) = 2, bit-length = 5, and 100 * (5 - 2) / 5 = 60%.

like image 185
user187291 Avatar answered Sep 29 '22 22:09

user187291