Ignore extra words in phrase (close #4)

master
Skylar Ittner 3 years ago
parent 90a0a17093
commit feed3d2918

@ -1,4 +1,5 @@
<?php <?php
/* Copyright 2021 Netsyms Technologies. /* Copyright 2021 Netsyms Technologies.
Redistribution and use in source and binary forms, with or without modification, are permitted Redistribution and use in source and binary forms, with or without modification, are permitted
@ -23,6 +24,7 @@
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
class FixPhrase { class FixPhrase {
private const WORDLIST = [ private const WORDLIST = [
@ -7676,31 +7678,43 @@ class FixPhrase {
return implode(" ", [self::WORDLIST[$groups[0]], self::WORDLIST[$groups[1]], self::WORDLIST[$groups[2]], self::WORDLIST[$groups[3]]]); return implode(" ", [self::WORDLIST[$groups[0]], self::WORDLIST[$groups[1]], self::WORDLIST[$groups[2]], self::WORDLIST[$groups[3]]]);
} }
/**
* Convert a phrase into coordinates.
* @param string $words
* @return array [latitude, longitude, accuracy (degrees), phrase]
* @throws Exception If the phrase is not valid.
*/
public static function decode(string $words): array { public static function decode(string $words): array {
$indexes = [-1, -1, -1, -1]; $indexes = [-1, -1, -1, -1];
$words = explode(" ", strtolower($words)); $words = explode(" ", strtolower($words));
$canonicalphrase = ["", "", "", ""];
if (count($words) < 2) { if (count($words) < 2) {
throw new Exception("Not enough words!"); throw new Exception("Not enough words!");
} else if (count($words) > 4) { } else if (count($words) > 4) {
throw new Exception("Too many words!"); //throw new Exception("Too many words!");
} }
// Convert words back into indices and do the math to make them coordinate chunks // Convert words back into indices and do the math to make them coordinate chunks
foreach ($words as $word) { foreach ($words as $word) {
$index = array_search($word, self::WORDLIST); $index = array_search($word, self::WORDLIST);
if ($index === false) { if ($index === false) {
throw new Exception("Unrecognized word found!"); //throw new Exception("Unrecognized word found!");
} }
if ($index >= 0 && $index < 2000) { if ($index >= 0 && $index < 2000) {
$indexes[0] = $index; $indexes[0] = $index;
$canonicalphrase[0] = self::WORDLIST[$index];
} else if ($index >= 2000 && $index < 5610) { } else if ($index >= 2000 && $index < 5610) {
$indexes[1] = $index - 2000; $indexes[1] = $index - 2000;
$canonicalphrase[1] = self::WORDLIST[$index];
} else if ($index >= 5610 && $index < 6610) { } else if ($index >= 5610 && $index < 6610) {
$indexes[2] = $index - 5610; $indexes[2] = $index - 5610;
$canonicalphrase[2] = self::WORDLIST[$index];
} else if ($index >= 6610 && $index < 7610) { } else if ($index >= 6610 && $index < 7610) {
$indexes[3] = $index - 6610; $indexes[3] = $index - 6610;
$canonicalphrase[3] = self::WORDLIST[$index];
} }
} }
@ -7748,7 +7762,7 @@ class FixPhrase {
break; break;
} }
return [round($latitude, 4), round($longitude, 4), $accuracy]; return [round($latitude, 4), round($longitude, 4), $accuracy, trim(implode(" ", $canonicalphrase))];
} }
} }

@ -221,12 +221,6 @@
} }
}); });
// Check for coordinates in hash
parseWindowHash();
window.addEventListener('hashchange', function () {
parseWindowHash();
}, false);
$("body").on("click", ".copyonclick", function () { $("body").on("click", ".copyonclick", function () {
var copyitem = $(this); var copyitem = $(this);
var copytext = copyitem.text(); var copytext = copyitem.text();
@ -258,6 +252,12 @@
date.setTime(date.getTime() + (90 * 24 * 60 * 60 * 1000)); date.setTime(date.getTime() + (90 * 24 * 60 * 60 * 1000));
document.cookie = "mapstyle=" + $(this).data("style") + "; expires=" + date.toUTCString(); document.cookie = "mapstyle=" + $(this).data("style") + "; expires=" + date.toUTCString();
}); });
// Check for coordinates in hash
parseWindowHash();
window.addEventListener('hashchange', function () {
parseWindowHash();
}, false);
}; };
function dolookup(words) { function dolookup(words) {
@ -266,7 +266,7 @@
var coords = FixPhrase.decode(words); var coords = FixPhrase.decode(words);
location.hash = "#map"; location.hash = "#map";
showLocationPopup(coords[0], coords[1], words, coords[2]); showLocationPopup(coords[0], coords[1], coords[3], coords[2]);
var zoomlevel = 18; var zoomlevel = 18;
switch (coords[2]) { switch (coords[2]) {

@ -63,27 +63,33 @@ var FixPhrase = {
var indexes = [-1, -1, -1, -1]; var indexes = [-1, -1, -1, -1];
words = words.toLowerCase(); words = words.toLowerCase();
words = words.split(" "); words = words.split(" ");
// Gather the real phrase in correct order, with any extra words removed, to return for display
var canonicalphrase = ["", "", "", ""];
if (words.length < 2) { if (words.length < 2) {
throw "Not enough words!"; throw "Not enough words!";
} else if (words.length > 4) { } else if (words.length > 4) {
throw "Too many words!"; console.error("Warning: Too many words!");
} }
// Convert words back into indices and do the math to make them coordinate chunks // Convert words back into indices and do the math to make them coordinate chunks
for (var i = 0; i < words.length; i++) { for (var i = 0; i < words.length; i++) {
var index = this.wordlist.indexOf(words[i]); var index = this.wordlist.indexOf(words[i]);
if (index == -1) { if (index == -1) {
throw "Unrecognized word found: " + words[i]; console.error("Warning: Unrecognized word found: " + words[i] + ", ignoring.");
} }
if (index >= 0 && index < 2000) { if (index >= 0 && index < 2000) {
indexes[0] = index; indexes[0] = index;
canonicalphrase[0] = this.wordlist[index];
} else if (index >= 2000 && index < 5610) { } else if (index >= 2000 && index < 5610) {
indexes[1] = index - 2000; indexes[1] = index - 2000;
canonicalphrase[1] = this.wordlist[index];
} else if (index >= 5610 && index < 6610) { } else if (index >= 5610 && index < 6610) {
indexes[2] = index - 5610; indexes[2] = index - 5610;
canonicalphrase[2] = this.wordlist[index];
} else if (index >= 6610 && index < 7610) { } else if (index >= 6610 && index < 7610) {
indexes[3] = index - 6610; indexes[3] = index - 6610;
canonicalphrase[3] = this.wordlist[index];
} }
} }
@ -128,7 +134,7 @@ var FixPhrase = {
break; break;
} }
return [latitude, longitude, accuracy]; return [latitude, longitude, accuracy, canonicalphrase.join(" ").trim()];
}, },
wordlist: [ wordlist: [
"abacus", "abacus",

Loading…
Cancel
Save