Split wikidata helper into languages, fixes #893

This commit is contained in:
Pieter Vander Vennet 2022-06-23 03:06:51 +02:00
parent a839e6e820
commit b8e5800df3
3 changed files with 173 additions and 150 deletions

View file

@ -154,7 +154,7 @@ export default class TagRenderingConfig {
}
if (!ValidatedTextField.ForType(this.freeform.key) === undefined) {
if (this.freeform.type !== undefined && ValidatedTextField.AvailableTypes().indexOf(this.freeform.type) < 0) {
const knownKeys = ValidatedTextField.AvailableTypes().join(", ");
throw `Freeform.key ${this.freeform.key} is an invalid type. Known keys are ${knownKeys}`
}

View file

@ -26,6 +26,7 @@ import InputElementMap from "./InputElementMap";
import Translations from "../i18n/Translations";
import {Translation} from "../i18n/Translation";
import BaseLayer from "../../Models/BaseLayer";
import Locale from "../i18n/Locale";
export class TextFieldDef {
@ -74,7 +75,7 @@ export class TextFieldDef {
location?: [number /*lat*/, number /*lon*/],
mapBackgroundLayer?: UIEventSource</*BaseLayer*/ any>,
unit?: Unit,
args?: (string | number | boolean)[] // Extra arguments for the inputHelper,
args?: (string | number | boolean | any)[] // Extra arguments for the inputHelper,
feature?: any,
} = {}): InputElement<string> {
@ -249,8 +250,8 @@ class WikidataTextField extends TextFieldDef {
["options", new Combine(["A JSON-object of type `{ removePrefixes: string[], removePostfixes: string[] }`.",
new Table(
["subarg", "doc"],
[["removePrefixes", "remove these snippets of text from the start of the passed string to search"],
["removePostfixes", "remove these snippets of text from the end of the passed string to search"],
[["removePrefixes", "remove these snippets of text from the start of the passed string to search. This is either a list OR a hash of languages to a list"],
["removePostfixes", "remove these snippets of text from the end of the passed string to search. This is either a list OR a hash of languages to a list"],
["instanceOf","A list of Q-identifier which indicates that the search results _must_ be an entity of this type, e.g. [`Q5`](https://www.wikidata.org/wiki/Q5) for humans"],
["notInstanceof","A list of Q-identifiers which indicates that the search results _must not_ be an entity of this type, e.g. [`Q79007`](https://www.wikidata.org/wiki/Q79007) to filter away all streets from the search results"]
]
@ -266,13 +267,16 @@ class WikidataTextField extends TextFieldDef {
"helperArgs": [
"name",
{
"removePostfixes": [
"removePostfixes": {"en": [
"street",
"boulevard",
"path",
"square",
"plaza",
],
"nl": ["straat","plein","pad","weg",laan"]
},
"#": "Remove streets and parks from the search results:"
"notInstanceOf": ["Q79007","Q22698"]
}
@ -325,37 +329,46 @@ Another example is to search for species and trees:
const args = inputHelperOptions.args ?? []
const searchKey = args[0] ?? "name"
let searchFor = <string>(inputHelperOptions.feature?.properties[searchKey]?.toLowerCase() ?? "")
const searchFor = <string>(inputHelperOptions.feature?.properties[searchKey]?.toLowerCase() ?? "")
let searchForValue: UIEventSource<string> = new UIEventSource(searchFor);
const options: any = args[1]
if (searchFor !== undefined && options !== undefined) {
const prefixes = <string[]>options["removePrefixes"]
const postfixes = <string[]>options["removePostfixes"]
for (const postfix of postfixes ?? []) {
const prefixes = <string[] | Record<string, string[]>>options["removePrefixes"] ?? []
const postfixes = <string[] | Record<string, string[]>>options["removePostfixes"] ?? []
Locale.language.map(lg => {
const prefixesUnrwapped: string[] = prefixes[lg] ?? prefixes
const postfixesUnwrapped: string[] = postfixes[lg] ?? postfixes
let clipped = searchFor;
console.log("Pref", prefixesUnrwapped," post", postfixesUnwrapped)
for (const postfix of postfixesUnwrapped) {
if (searchFor.endsWith(postfix)) {
searchFor = searchFor.substring(0, searchFor.length - postfix.length)
clipped = searchFor.substring(0, searchFor.length - postfix.length)
break;
}
}
for (const prefix of prefixes ?? []) {
for (const prefix of prefixesUnrwapped) {
if (searchFor.startsWith(prefix)) {
searchFor = searchFor.substring(prefix.length)
clipped = searchFor.substring(prefix.length)
break;
}
}
return clipped;
}).addCallbackAndRun(clipped => searchForValue.setData(clipped))
}
let instanceOf : number[] = Utils.NoNull((options?.instanceOf ?? []).map(i => Wikidata.QIdToNumber(i)))
let notInstanceOf : number[] = Utils.NoNull((options?.notInstanceOf ?? []).map(i => Wikidata.QIdToNumber(i)))
console.log("Instance of", instanceOf)
return new WikidataSearchBox({
value: currentValue,
searchText: new UIEventSource<string>(searchFor),
searchText: searchForValue,
instanceOf,
notInstanceOf
})

View file

@ -60,7 +60,8 @@
"Q79007",
"Q22698"
],
"removePrefixes": [
"removePrefixes": {
"fr": [
"allée de",
"allée du",
"allée",
@ -154,38 +155,47 @@
"villa de",
"villa du",
"villa"
],
"removePostfixes": [
]
},
"removePostfixes": {
"nl": [
"baan",
"boulevard",
"dreef",
"church",
"heirbaan",
"gasse",
"grundschule",
"gymnasium",
"kaai",
"kerk",
"laan",
"lei",
"pad",
"park",
"parque",
"path",
"platz",
"plaza",
"plein",
"ring",
"schule",
"square",
"steenweg",
"straat",
"straße",
"street",
"weg",
"wegel"
],
"fr": [
"parque"
],
"de": [
"straße",
"platz",
"gasse",
"grundschule",
"gymnasium",
"schule"
],
"en": [
"street",
"path",
"plaza",
"square",
"church"
]
}
}
]
},
"render": {