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"]
} }
@ -325,37 +329,46 @@ Another example is to search for species and trees:
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 ?? []) {
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)) { if (searchFor.endsWith(postfix)) {
searchFor = searchFor.substring(0, searchFor.length - postfix.length) clipped = searchFor.substring(0, searchFor.length - postfix.length)
break; break;
} }
} }
for (const prefix of prefixes ?? []) { for (const prefix of prefixesUnrwapped) {
if (searchFor.startsWith(prefix)) { if (searchFor.startsWith(prefix)) {
searchFor = searchFor.substring(prefix.length) clipped = searchFor.substring(prefix.length)
break; 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
}) })

View file

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