aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHolden Rohrer <hr@hrhr.dev>2020-07-16 20:48:14 -0400
committerHolden Rohrer <hr@hrhr.dev>2020-07-16 20:48:14 -0400
commitb62c8fd41cf32ea33de9d4f74415c665d315ecb1 (patch)
tree8e46f8e2aae34c35807906424dc18da50df30344
parentb06268b3ab9da5140bb87fdc1ef7cde27e5ffd47 (diff)
search.js uses `space` regexes now, improving nword match
-rw-r--r--examples/jarvis.js4
-rw-r--r--examples/nword.js4
-rw-r--r--space.js38
-rw-r--r--tools/search.js2
4 files changed, 41 insertions, 7 deletions
diff --git a/examples/jarvis.js b/examples/jarvis.js
index 7268ad2..ac27cea 100644
--- a/examples/jarvis.js
+++ b/examples/jarvis.js
@@ -114,10 +114,8 @@ function protectArea(send, tiles, locs, id, space, ctrl){
main.enqueue(writelist);
}
-var search = new Space();
-search.adhoc('jarvis'); // The search space is the word jarvis, so whenever that's caught, a relevant function can be called.
var read = new Search();
-read.match(search, respond);
+read.match(['jarvis'], respond);
var expire = {};
main.on('fallback', (send, tiles, locs) => { // tries to detect the prompt ('jarvis') and calls respond if found.
diff --git a/examples/nword.js b/examples/nword.js
index 5926e42..9367362 100644
--- a/examples/nword.js
+++ b/examples/nword.js
@@ -38,9 +38,7 @@ main.on('tileUpdate', (send, source, tiles)=>{
}
});
-search.match(new Space().adhoc('nigger'), clean);
-search.match(new Space().adhoc('Nigger'), clean);
-search.match(new Space().adhoc('NIGGER'), clean);
+search.match(['[Nn] *[Ii] *[Gg] *[Gg] *[Ee] *[Rr]'], clean);
let response = new Space().adhoc(' I am a racist ');
function clean(coord, send){
diff --git a/space.js b/space.js
index 65b5355..c5a0927 100644
--- a/space.js
+++ b/space.js
@@ -138,6 +138,44 @@ function Space(){
return loc;
}
+ this.regex = function(strlines){
+ if (typeof strlines[0] !== 'string') throw ".regex needs arr of strings";
+ let lines = [];
+ for (let i = 0; i < strlines.length; i++) {
+ lines.push(new RegExp(strlines[i], 'g'));
+ }
+ let data = [];
+ for (let ln of this.data){
+ let newln = "";
+ for (c of ln){
+ if (c === '') newln += '\0';
+ else newln += c;
+ }
+ data.push(newln);
+ }
+ let loc;
+ data.forEach( (ln, ind) => {
+ if (loc || ((data.length - ind) < lines.length)) return;
+ let match;
+ while ( (match = lines[0].exec(ln)) !== null){
+ let xdisp = match.index;
+ let lnmatch = true;
+ for (let i=1; lines[i] !== undefined; i+= 1){
+ let imatch = lines[i].exec(data[ind+i].slice(xdisp));
+ if (!imatch || imatch.index !== 0){
+ lnmatch = false;
+ break;
+ }
+ }
+ if (lnmatch){
+ loc = [ind, xdisp];
+ break;
+ }
+ }
+ });
+ return (loc === undefined ? [] : loc);
+ }
+
this.subsection = function(range){ // range is a coordinate pair
if (range.length !== 2 || range[0].length !== 2) throw "not subsecting a coord pair";
// Similarly excludes this.loc
diff --git a/tools/search.js b/tools/search.js
index 378f316..56137b8 100644
--- a/tools/search.js
+++ b/tools/search.js
@@ -32,7 +32,7 @@ function Search(){ // searchBlock should be a Space object.
searchspace.comb( this.tiles[tile], comb.add );
});
for (let i=0; i<this.spaces.length; i++){
- let coords = searchspace.search(this.spaces[i]);
+ let coords = searchspace.regex(this.spaces[i]);
if (coords.length) this.calls[i](vec.add(coords, searchspace.loc), send, searchspace);
}
}