} else {
    if ($auth_method == 'OAuth') {
        _wl(' using OAuth auth with consumerKey="' . $auth_oauth_consumer_key . '"');
        $oauth = new OSM_Auth_OAuth($auth_oauth_consumer_key, $auth_oauth_consumer_secret);
        $oauth->setAccessToken($auth_oauth_token, $auth_oauth_secret);
        $osmApi->setCredentials($oauth);
    }
}
// http://api06.dev.openstreetmap.org/api/0.6/relation/500
// http://api06.dev.openstreetmap.org/api/0.6/way/8184
// http://api06.dev.openstreetmap.org/api/0.6/node/611571
// get a node
$permissions = $osmApi->getAuthPermissions();
echo print_r($permissions, true) . "\n";
if ($auth_method == 'Basic') {
    _assert($osmApi->isAllowedToReadPrefs() === true);
    _assert($osmApi->isAllowedToWritePrefs() === true);
    _assert($osmApi->isAllowedToWriteDiary() === true);
    _assert($osmApi->isAllowedToWriteApi() === true);
    _assert($osmApi->isAllowedToReadGpx() === true);
    _assert($osmApi->isAllowedToWriteGpx() === true);
} else {
    _assert($osmApi->isAllowedToReadPrefs() === true);
    _assert($osmApi->isAllowedToWritePrefs() === true);
    _assert($osmApi->isAllowedToWriteDiary() === false);
    _assert($osmApi->isAllowedToWriteApi() === true);
    _assert($osmApi->isAllowedToReadGpx() === true);
    _assert($osmApi->isAllowedToWriteGpx() === false);
}
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
Пример #2
0
include '../../../autoloader.php';
include '../../testing.php';
use NlpTools\Similarity\CosineSimilarity;
$sim = new CosineSimilarity();
$A = array(1, 2, 3);
$A2 = array(1, 2, 3, 1, 2, 3);
$B = array(1, 2, 3, 4, 5, 6);
// triangle
// A = (2,4)
// B = (0,0)
// C = (3,2)
// 1 will be x and 2 will be y
$ba = array(1, 1, 2, 2, 2, 2);
// ba = (2,4)
$bc = array(1, 1, 1, 2, 2);
// bc = (3,2)
$bba = array('0' => 2, '1' => 4);
$bbc = array('0' => 3, '1' => 2);
$ba_to_bc = cos(0.5191461142);
// approximately 30 deg
// this is a rounding threshold because (A•A / |A|*|A|) != 1
$rt = 1.0E-10;
_assert(abs($sim->similarity($A, $A) - 1) < $rt, "Cosine similarity of a set with itsself should be 1");
_assert(abs($sim->similarity($A, $A2) - 1) < $rt, "Cosine similarity of a set with any linear combination of it should be 1");
_assert($sim->similarity($A, $B) - $sim->similarity($A2, $B) < $rt, "Parallel vectors should have the same angle with any vector B");
// Test the triangle
_assert($sim->similarity($ba, $bc) - $ba_to_bc < $rt, "CosineSim[{2,4},{3,2}]=0.8682431421244593 instead of {$sim->similarity($ba, $bc)}");
// Same as above just passing already made vectors
_assert($sim->similarity($bba, $bbc) - $ba_to_bc < $rt, "CosineSim[{2,4},{3,2}]=0.8682431421244593 instead of {$sim->similarity($bba, $bbc)}");
Пример #3
0
<?php

include '../../../autoloader.php';
include '../../testing.php';
use NlpTools\Tokenizers\WhitespaceTokenizer;
function arrays_match($a1, $a2)
{
    return count(array_diff($a1, $a2)) == 0;
}
$tok = new WhitespaceTokenizer();
$s = "This is a simple space delimited string\nwith new lines and many     spaces between the words.\nAlso\ttabs\ttabs\ttabs\ttabs";
$tokens = array('This', 'is', 'a', 'simple', 'space', 'delimited', 'string', 'with', 'new', 'lines', 'and', 'many', 'spaces', 'between', 'the', 'words.', 'Also', 'tabs', 'tabs', 'tabs', 'tabs');
_assert(arrays_match($tok->tokenize($s), $tokens), "Problem tokenizing simple ASCII whitespace with ascii content");
$s = "Ελληνικό κείμενο για παράδειγμα utf-8 χαρακτήρων";
$tokens = array('Ελληνικό', 'κείμενο', 'για', 'παράδειγμα', 'utf-8', 'χαρακτήρων');
_assert(arrays_match($tok->tokenize($s), $tokens), "Problem tokenizing simple ASCII whitespace with utf-8 content");
$s = "Here exists non-breaking space   ";
$tokens = array('Here', 'exists', 'non-breaking', 'space');
_assert(arrays_match($tok->tokenize($s), $tokens), "Problem tokenizing utf-8 whitespace");
Пример #4
0
<?php

include '../../../autoloader.php';
include '../../testing.php';
use NlpTools\Similarity\Simhash;
$sim = new Simhash(64);
//md5 is used by default
$A = array(1, 2, 3);
$B = array(1, 2, 3, 4, 5, 6);
$b = array(1, 2, 3, 4, 5);
$e = array();
_assert($sim->similarity($A, $A) == 1, "A set with an identical set should have the same exact hash");
_assert($sim->dist($A, $B) > $sim->dist($b, $B), "Set A should be further than set b is from B since they have less common elements");
//
$auth_method = '';
// Override above parameters into you own file:
include __DIR__ . '/../../secrets.php';
//
// ===================================================
$osmApi = new OSM_Api(array('url' => OSM_Api::URL_DEV_UK, 'url4Write' => OSM_Api::URL_DEV_UK));
if ($auth_method == 'Basic') {
    _wl(' using Basic auth with user="******"');
    $osmApi->setCredentials(new OSM_Auth_Basic($auth_basic_user, $auth_basic_password));
} else {
    if ($auth_method == 'OAuth') {
        _wl(' using OAuth auth with consumerKey="' . $auth_oauth_consumer_key . '"');
        $oauth = new OSM_Auth_OAuth($auth_oauth_consumer_key, $auth_oauth_consumer_secret);
        $oauth->setToken($auth_oauth_token, $auth_oauth_secret);
        $osmApi->setCredentials($oauth);
    }
}
// http://api06.dev.openstreetmap.org/api/0.6/relation/500
// http://api06.dev.openstreetmap.org/api/0.6/way/8184
// http://api06.dev.openstreetmap.org/api/0.6/node/611571
// get a node
$userPreferences = $osmApi->getUserPreferences();
//echo print_r($userPreferences,true)."\n";
$expectedValue = time();
$osmApi->setUserPreference('test', $expectedValue);
$userPreferences = $osmApi->getUserPreferences();
_assert(isset($userPreferences['test']));
_assert($userPreferences['test'] == $expectedValue);
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
$relations = $osmApi->getRelations();
_assert(count($relations) == 1);
$ways = $osmApi->getWays();
_assert(count($ways) == 8);
$nodes = $osmApi->getNodes();
_assert(count($nodes) == 680);
$objects = $osmApi->getObjects();
_assert(count($objects) == 689);
// getRelation, getWay, getNode
$relation = $osmApi->getRelation('164211');
_assert($relation != null);
_assert($relation->isDirty() == false);
$way = $osmApi->getWay('34717700');
_assert($way != null);
_assert($way->isDirty() == false);
$node = $osmApi->getNode('691558211');
_assert($node != null);
_assert($node->isDirty() == false);
// getObjectsByTags
$objects = $osmApi->getObjectsByTags(array('ref:INSEE' => '37001'));
_assert(count($objects) == 2);
$objects = $osmApi->getObjectsByTags(array('ref:INSEE' => ''));
_assert(count($objects) == 2);
$objects = $osmApi->getObjectsByTags(array('ref:INSEE' => '', 'place' => ''));
_assert(count($objects) == 1);
// test removeObject
$osmApi->removeObject(OSM_Api::OBJTYPE_NODE, '691558211');
$node = $osmApi->hasNode('691558211');
_assert($node == null);
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
Пример #7
0
$auth_oauth_consumer_key = '';
$auth_oauth_consumer_secret = '';
$auth_oauth_token = '';
$auth_oauth_secret = '';
//
$auth_method = '';
// Override above parameters into you own file:
include __DIR__ . '/../../secrets.php';
//
// ===================================================
$osmApi = new OSM_Api(array('url' => OSM_Api::URL_DEV_UK));
if ($auth_method == 'Basic') {
    _wl(' using Basic auth with user="******"');
    $osmApi->setCredentials(new OSM_Auth_Basic($auth_basic_user, $auth_basic_password));
} else {
    if ($auth_method == 'OAuth') {
        _wl(' using OAuth auth with consumerKey="' . $auth_oauth_consumer_key . '"');
        $oauth = new OSM_Auth_OAuth($auth_oauth_consumer_key, $auth_oauth_consumer_secret);
        $oauth->setAccessToken($auth_oauth_token, $auth_oauth_secret);
        $osmApi->setCredentials($oauth);
    }
}
// http://api06.dev.openstreetmap.org/api/0.6/relation/500
// http://api06.dev.openstreetmap.org/api/0.6/way/8184
// http://api06.dev.openstreetmap.org/api/0.6/node/611571
// get a node
$userDetails = $osmApi->getUserDetails();
//echo print_r($userDetails,true)."\n";
_assert($test_expected_auth_username == $userDetails->getName());
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
    if ($auth_method == 'OAuth') {
        _wl(' using OAuth auth with consumerKey="' . $auth_oauth_consumer_key . '"');
        $oauth = new OSM_Auth_OAuth($auth_oauth_consumer_key, $auth_oauth_consumer_secret);
        $oauth->setToken($auth_oauth_token, $auth_oauth_secret);
        $osmApi->setCredentials($oauth);
    }
}
// http://www.openstreetmap.org/api/0.6/node/1326928399
$xmlQuery = '
<osm-script>
 <query type="node">
  <has-kv k="name" v="Castel Fleuri"/>
  <has-kv k="tourism" v="hotel"/>
  <has-kv k="addr:street" v="Rue Groison"/>
  <has-kv k="addr:city" v="Tours"/>
 </query>
 <print/>
</osm-script>
';
$osmApi->queryOApi($xmlQuery);
$nodes = $osmApi->getNodes();
_assert(count($nodes) == 1);
$tagName = 'yapafo.net::test::oapi';
if (($node = $nodes[0]->getTag($tagName)) != null) {
    $nodes[0]->removeTag($tagName);
} else {
    $nodes[0]->addTag($tagName, '123');
}
$osmApi->saveChanges('Cyrille37 test');
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
// Check node exists and it's a relation's member.
//
$osmApi->removeAllObjects();
$relation = $osmApi->getRelation('500', true);
$nodes = $osmApi->getNodesByTags(array('A yapafo.net test' => 'add node'));
_assert($nodes != null);
$members = $relation->findMembersByTypeAndRole(OSM_Api::OBJTYPE_NODE, $memberRole);
_assert($members != null);
// remove this new member and delete the created node
foreach ($members as $member) {
    $relation->removeMember($member);
}
$osmApi->saveChanges('A yapafo.net test');
// delete the created node
$osmApi->removeAllObjects();
$relation = $osmApi->getRelation('500', true);
foreach ($members as $member) {
    $osmApi->getNode($member->getRef())->delete();
}
$osmApi->saveChanges('A yapafo.net test');
//
// Check remove members and delete object
//
$osmApi->removeAllObjects();
$relation = $osmApi->getRelation('500', true);
$nodes = $osmApi->getNodesByTags(array('A yapafo.net test' => 'add node'));
_assert(count($nodes) == 0);
$members = $relation->findMembersByTypeAndRole(OSM_Api::OBJTYPE_NODE, $memberRole);
_assert(count($members) == 0);
$time_end = microtime(true);
_wl('Test well done in ' . number_format($time_end - $time_start, 3) . ' second(s).');
Пример #10
0
<?php

include '../../../autoloader.php';
include '../../testing.php';
use NlpTools\Similarity\JaccardIndex;
$sim = new JaccardIndex();
$A = array(1, 2, 3);
$B = array(1, 2, 3, 4, 5, 6);
$e = array();
_assert($sim->similarity($A, $A) == 1, "Jaccard index of a set with itsself should be 1");
_assert($sim->similarity($A, $e) == 0, "Jaccard index of a set with an empty set should be 0");
_assert($sim->similarity($A, $B) == 0.5, "J({1,2,3},{1,2,3,4,5,6}) = 0.5");
Пример #11
0
<?php

include '../../../autoloader.php';
include '../../testing.php';
use NlpTools\Similarity\HammingDistance;
$A = "ABCDE";
$B = "FGHIJ";
$C = "10101";
$D = "11111";
$d = new HammingDistance();
_assert($d->dist($A, $B) == max(strlen($A), strlen($B)), "Two completely dissimilar strings should have distance equal to max(strlen(\$A),strlen(\$B))");
_assert($d->dist($C, $D) == 2, "10101 ~ 11111 has hamming distance 2");