質問

I am working on a travel guide website. What i want to do is read users facebook checkins and mark the locations (saved in my db with coordinates collected from google) that they visited.

My question is how can i best compare the location saved in my db with the location given by facebook. The coordinates dont match exactly and the name on facebook is in many cases saved in multiple variations.

I am using neo4j db with php.

役に立ちましたか?

解決

So this is what i`ve come up with. Hope it helps out someone.

First I selected some destinations where the difference between coordinates is smaller than 0.25. You can adjust this value as needed. My cypher query looks like this:

START n = node:Destination('*:*')
WHERE has( n.lat ) AND has( n.long ) AND ABS(n.lat - ".$value['place']['location']['latitude'].") < 0.25 AND ABS(n.long - ".$value['place']['location']['longitude'].") < 0.25
RETURN n, ABS(n.lat - ".$value['place']['location']['latitude']."), ABS(n.long - ".$value['place']['location']['longitude'].")

Then i caclulate "Levenshtein distance" (the minimal number of characters you have to replace, insert or delete to transform str1 into str2.) with levenshtein() function and select only those those that have this value smaller than length of string / 2. This value also can be adjusted for your needs. And heres the code:

foreach( $nodes as $key2 => $value2 ){
    $name1 = strtolower($value2['x']->getProperty('name'));
    $name2 = strtolower($value['place']['name']);
    $name2 = explode( ",", $name2 );
    $name2 = $name2[0];
    $similarity = levenshtein($name1, $name2);
    if( abs($similarity) <= intval(strlen($name1)/2) ){
       array_push($similarityArray,$value2);
    }
}

After this i narrowed it down by selecting the locations that was the nearest simply like this:

$minDifference = 0.4;
foreach( $similarityArray as $key2 => $value2 ){
        if( $minDifference > ($value2[1]+$value2[2]) ){
            $minDifference = ($value2[1]+$value2[2]);
            $minKey = $key2;
        }
    }

The location will most probably be the one with the key $minKey.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top