Split wikidata helper into languages, fixes #893

This commit is contained in:
pietervdvn 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(", "); const knownKeys = ValidatedTextField.AvailableTypes().join(", ");
throw `Freeform.key ${this.freeform.key} is an invalid type. Known keys are ${knownKeys}` 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 Translations from "../i18n/Translations";
import {Translation} from "../i18n/Translation"; import {Translation} from "../i18n/Translation";
import BaseLayer from "../../Models/BaseLayer"; import BaseLayer from "../../Models/BaseLayer";
import Locale from "../i18n/Locale";
export class TextFieldDef { export class TextFieldDef {
@ -74,7 +75,7 @@ export class TextFieldDef {
location?: [number /*lat*/, number /*lon*/], location?: [number /*lat*/, number /*lon*/],
mapBackgroundLayer?: UIEventSource</*BaseLayer*/ any>, mapBackgroundLayer?: UIEventSource</*BaseLayer*/ any>,
unit?: Unit, unit?: Unit,
args?: (string | number | boolean)[] // Extra arguments for the inputHelper, args?: (string | number | boolean | any)[] // Extra arguments for the inputHelper,
feature?: any, feature?: any,
} = {}): InputElement<string> { } = {}): InputElement<string> {
@ -249,8 +250,8 @@ class WikidataTextField extends TextFieldDef {
["options", new Combine(["A JSON-object of type `{ removePrefixes: string[], removePostfixes: string[] }`.", ["options", new Combine(["A JSON-object of type `{ removePrefixes: string[], removePostfixes: string[] }`.",
new Table( new Table(
["subarg", "doc"], ["subarg", "doc"],
[["removePrefixes", "remove these snippets of text from the start 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"], ["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"], ["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"] ["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": [ "helperArgs": [
"name", "name",
{ {
"removePostfixes": [ "removePostfixes": {"en": [
"street", "street",
"boulevard", "boulevard",
"path", "path",
"square", "square",
"plaza", "plaza",
], ],
"nl": ["straat","plein","pad","weg",laan"]
},
"#": "Remove streets and parks from the search results:" "#": "Remove streets and parks from the search results:"
"notInstanceOf": ["Q79007","Q22698"] "notInstanceOf": ["Q79007","Q22698"]
} }
@ -324,38 +328,47 @@ Another example is to search for species and trees:
public inputHelper(currentValue, inputHelperOptions) { public inputHelper(currentValue, inputHelperOptions) {
const args = inputHelperOptions.args ?? [] const args = inputHelperOptions.args ?? []
const searchKey = args[0] ?? "name" 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] const options: any = args[1]
if (searchFor !== undefined && options !== undefined) { if (searchFor !== undefined && options !== undefined) {
const prefixes = <string[]>options["removePrefixes"] const prefixes = <string[] | Record<string, string[]>>options["removePrefixes"] ?? []
const postfixes = <string[]>options["removePostfixes"] const postfixes = <string[] | Record<string, string[]>>options["removePostfixes"] ?? []
for (const postfix of postfixes ?? []) {
if (searchFor.endsWith(postfix)) {
searchFor = searchFor.substring(0, searchFor.length - postfix.length)
break;
}
}
for (const prefix of prefixes ?? []) { Locale.language.map(lg => {
if (searchFor.startsWith(prefix)) { const prefixesUnrwapped: string[] = prefixes[lg] ?? prefixes
searchFor = searchFor.substring(prefix.length) const postfixesUnwrapped: string[] = postfixes[lg] ?? postfixes
break; let clipped = searchFor;
console.log("Pref", prefixesUnrwapped," post", postfixesUnwrapped)
for (const postfix of postfixesUnwrapped) {
if (searchFor.endsWith(postfix)) {
clipped = searchFor.substring(0, searchFor.length - postfix.length)
break;
}
} }
}
for (const prefix of prefixesUnrwapped) {
if (searchFor.startsWith(prefix)) {
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 instanceOf : number[] = Utils.NoNull((options?.instanceOf ?? []).map(i => Wikidata.QIdToNumber(i)))
let notInstanceOf : number[] = Utils.NoNull((options?.notInstanceOf ?? []).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({ return new WikidataSearchBox({
value: currentValue, value: currentValue,
searchText: new UIEventSource<string>(searchFor), searchText: searchForValue,
instanceOf, instanceOf,
notInstanceOf notInstanceOf
}) })
@ -868,7 +881,7 @@ export default class ValidatedTextField {
public static ForType(type: string = "string"): TextFieldDef { public static ForType(type: string = "string"): TextFieldDef {
const def = ValidatedTextField.allTypes.get(type) const def = ValidatedTextField.allTypes.get(type)
if(def === undefined){ if(def === undefined){
console.warn("Something tried to load a validated text field named",type, "but this type does not exist") console.warn("Something tried to load a validated text field named",type, "but this type does not exist")
return this.ForType() return this.ForType()
} }
return def return def

View file

@ -60,131 +60,141 @@
"Q79007", "Q79007",
"Q22698" "Q22698"
], ],
"removePrefixes": [ "removePrefixes": {
"allée de", "fr": [
"allée du", "allée de",
"allée", "allée du",
"autoroute de", "allée",
"autoroute du", "autoroute de",
"autoroute", "autoroute du",
"avenue de", "autoroute",
"avenue du", "avenue de",
"avenue", "avenue du",
"bibliothèque de", "avenue",
"bibliothèque du", "bibliothèque de",
"bibliothèque", "bibliothèque du",
"boulevard de", "bibliothèque",
"boulevard du", "boulevard de",
"boulevard", "boulevard du",
"centre culturel de", "boulevard",
"centre culturel du", "centre culturel de",
"centre culturel", "centre culturel du",
"centre de", "centre culturel",
"centre du", "centre de",
"centre", "centre du",
"chaussée de", "centre",
"chaussée du", "chaussée de",
"chaussée", "chaussée du",
"chemin de", "chaussée",
"chemin du", "chemin de",
"chemin", "chemin du",
"collège de", "chemin",
"collège du", "collège de",
"collège", "collège du",
"complexe sportif de", "collège",
"complexe sportif du", "complexe sportif de",
"complexe sportif", "complexe sportif du",
"école élémentaire de", "complexe sportif",
"école élémentaire du", "école élémentaire de",
"école élémentaire", "école élémentaire du",
"école maternelle de", "école élémentaire",
"école maternelle du", "école maternelle de",
"école maternelle", "école maternelle du",
"école primaire de", "école maternelle",
"école primaire du", "école primaire de",
"école primaire", "école primaire du",
"école de", "école primaire",
"école du", "école de",
"école", "école du",
"esplanade de", "école",
"esplanade du", "esplanade de",
"esplanade", "esplanade du",
"groupe scolaire de", "esplanade",
"groupe scolaire du", "groupe scolaire de",
"groupe scolaire", "groupe scolaire du",
"gymnase de", "groupe scolaire",
"gymnase du", "gymnase de",
"gymnase", "gymnase du",
"impasse de", "gymnase",
"impasse du", "impasse de",
"impasse", "impasse du",
"lycée de", "impasse",
"lycée du", "lycée de",
"lycée", "lycée du",
"mail de", "lycée",
"mail du", "mail de",
"mail", "mail du",
"médiathèque de", "mail",
"médiathèque du", "médiathèque de",
"médiathèque", "médiathèque du",
"musée de", "médiathèque",
"musée du", "musée de",
"musée", "musée du",
"parc de", "musée",
"parc du", "parc de",
"parc", "parc du",
"place de", "parc",
"place du", "place de",
"place", "place du",
"résidence de", "place",
"résidence du", "résidence de",
"résidence", "résidence du",
"route de", "résidence",
"route du", "route de",
"route", "route du",
"rue de", "route",
"rue du", "rue de",
"rue", "rue du",
"square de", "rue",
"square du", "square de",
"square", "square du",
"stade de", "square",
"stade du", "stade de",
"stade", "stade du",
"villa de", "stade",
"villa du", "villa de",
"villa" "villa du",
], "villa"
"removePostfixes": [ ]
"baan", },
"boulevard", "removePostfixes": {
"dreef", "nl": [
"church", "baan",
"heirbaan", "boulevard",
"gasse", "dreef",
"grundschule", "heirbaan",
"gymnasium", "kaai",
"kaai", "kerk",
"kerk", "laan",
"laan", "lei",
"lei", "pad",
"pad", "park",
"park", "plein",
"parque", "ring",
"path", "steenweg",
"platz", "straat",
"plaza", "weg",
"plein", "wegel"
"ring", ],
"schule", "fr": [
"square", "parque"
"steenweg", ],
"straat", "de": [
"straße", "straße",
"street", "platz",
"weg", "gasse",
"wegel" "grundschule",
] "gymnasium",
"schule"
],
"en": [
"street",
"path",
"plaza",
"square",
"church"
]
}
} }
] ]
}, },