Skip to content

Commit

Permalink
using 3 index lengths #4
Browse files Browse the repository at this point in the history
  • Loading branch information
retog committed Dec 14, 2019
1 parent 8577bfc commit 5b906d9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<label for="species-input">Species</label> <input type="text" id="species-input"/>
<script>
let input = document.getElementById("species-input");
let taxomplete = new Taxomplete(input, "https://plazi.factsmission.org/sparql");
let taxomplete = new Taxomplete(input);
taxomplete.action = function(value) {
alert(value);
}
Expand Down
41 changes: 35 additions & 6 deletions src/taxomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ export default class Taxomplete {
* @returns A promise for an array of matching genera
*/
function getGenusSuggestions(prefix) {
console.log(self);
let query = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
"PREFIX dwc: <http://rs.tdwg.org/dwc/terms/>\n" +
"PREFIX dwcfp: <http://filteredpush.org/ontologies/oa/dwcFP#>\n" +
"PREFIX tp: <https://vocab.plazi.org/taxomplete/>\n" +
"SELECT DISTINCT ?genus WHERE {\n" +
"?sub dwc:genus ?genus .\n" +
"?sub rdf:type dwcfp:TaxonName.\n" +
"?sub tp:genusPrefix \"" + prefix.toLowerCase().substr(0,2) + "\".\n" +
"FILTER REGEX(?genus, \"^" + prefix + "\",\"i\")\n" +
self.genusFilter(prefix) +
"} ORDER BY UCASE(?genus) LIMIT 10";
return self._sparqlEndpoint.getSparqlResultSet(query).then(json => {
return json.results.bindings.map(binding => binding.genus.value);
Expand All @@ -121,8 +121,7 @@ export default class Taxomplete {
"?sub dwc:genus \"" + genus + "\" .\n" +
"?sub dwc:species ?species .\n" +
"?sub rdf:type dwcfp:TaxonName.\n" +
(prefix.length > 1 ? "?sub tp:speciesPrefix \"" + prefix.toLowerCase().substr(0,2) + "\".\n" : "") +
"FILTER REGEX(?species, \"^" + prefix + "\",\"i\")\n" +
self.speciesFilter(prefix) +
"} ORDER BY UCASE(?species) LIMIT 10";
return self._sparqlEndpoint.getSparqlResultSet(query).then(json => {
return json.results.bindings.map(binding => binding.species.value);
Expand All @@ -138,15 +137,45 @@ export default class Taxomplete {
"?sub dwc:genus ?genus .\n" +
"?sub dwc:species ?species .\n" +
"?sub rdf:type dwcfp:TaxonName.\n" +
(prefix.length > 1 ? "?sub tp:speciesPrefix \"" + prefix.toLowerCase().substr(0,2) + "\".\n" : "") +
"FILTER REGEX(?species, \"^" + prefix + "\",\"i\")\n" +
self.speciesFilter(prefix) +
"} ORDER BY UCASE(?species) LIMIT 10";
return self._sparqlEndpoint.getSparqlResultSet(query).then(json => {
return json.results.bindings.map(binding => binding.genus.value + " " + binding.species.value);
});
}
}

speciesFilter(prefix) {
return this.filterPattern("species", prefix);
}

genusFilter(prefix) {
return this.filterPattern("genus", prefix);
}

basicFilterPattern(propPart, prefix) {
return "FILTER REGEX(?"+propPart+", \"^" + prefix + "\",\"i\")\n";
}

filterPattern(propPart, prefix) {
let result = "";
if (prefix.length > 3) {
result += "?sub tp:"+propPart+"Prefix4 \"" + prefix.toLowerCase().substr(0,4) + "\".\n";
result += this.basicFilterPattern(propPart, prefix);
} else {
if (prefix.length > 2) {
result += "?sub tp:"+propPart+"Prefix3 \"" + prefix.toLowerCase().substr(0,3) + "\".\n";
} else {
if (prefix.length > 1) {
result += "?sub tp:"+propPart+"Prefix2 \"" + prefix.toLowerCase().substr(0,2) + "\".\n";
} else {
result += this.basicFilterPattern(propPart, prefix);
}
}
}
return result;
}

lookup() {
this.action(this._input.value.toString());
}
Expand Down

0 comments on commit 5b906d9

Please sign in to comment.