mapcomplete/scripts/thieves/stealLanguages.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-07-11 09:14:26 +02:00
/*
2022-09-08 21:40:48 +02:00
* Uses the languages in and to every translation from wikidata to generate a language question in wikidata/wikidata
* */
2022-07-11 09:14:26 +02:00
2022-09-08 21:40:48 +02:00
import WikidataUtils from "../../Utils/WikidataUtils"
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"
import { LayerConfigJson } from "../../Models/ThemeConfig/Json/LayerConfigJson"
import { MappingConfigJson } from "../../Models/ThemeConfig/Json/QuestionableTagRenderingConfigJson"
import LanguageUtils from "../../Utils/LanguageUtils"
import * as perCountry from "../../assets/language_in_country.json"
2022-09-08 21:40:48 +02:00
import { Utils } from "../../Utils"
function main() {
const sourcepath = "assets/generated/languages-wd.json"
2022-07-11 09:14:26 +02:00
console.log(`Converting language data file '${sourcepath}' into a tagMapping`)
2022-09-08 21:40:48 +02:00
const languages = WikidataUtils.extractLanguageData(
JSON.parse(readFileSync(sourcepath, "utf8")),
{}
)
const mappings: MappingConfigJson[] = []
const schoolmappings: MappingConfigJson[] = []
const countryToLanguage: Record<string, string[]> = perCountry
const officialLanguagesPerCountry = Utils.TransposeMap(countryToLanguage)
2022-07-11 09:14:26 +02:00
languages.forEach((l, code) => {
2022-09-08 21:40:48 +02:00
const then: Record<string, string> = {}
2022-07-11 09:14:26 +02:00
l.forEach((tr, lng) => {
2022-09-08 21:40:48 +02:00
const languageCodeWeblate = WikidataUtils.languageRemapping[lng] ?? lng
if (!LanguageUtils.usedLanguages.has(languageCodeWeblate)) {
return
2022-07-11 09:14:26 +02:00
}
then[languageCodeWeblate] = tr
})
2022-09-08 21:40:48 +02:00
const officialCountries = Utils.Dedup(
officialLanguagesPerCountry[code]?.map((s) => s.toLowerCase()) ?? []
)
const prioritySearch =
officialCountries.length > 0
? "_country~" + officialCountries.map((c) => "((^|;)" + c + "($|;))").join("|")
: undefined
2022-07-11 09:14:26 +02:00
mappings.push(<MappingConfigJson>{
if: "language:" + code + "=yes",
ifnot: "language:" + code + "=",
searchTerms: {
2022-09-08 21:40:48 +02:00
"*": [code],
2022-07-11 09:14:26 +02:00
},
then,
2022-09-08 21:40:48 +02:00
priorityIf: prioritySearch,
2022-07-11 09:14:26 +02:00
})
2022-09-08 21:40:48 +02:00
schoolmappings.push(<MappingConfigJson>{
2022-07-11 09:14:26 +02:00
if: "school:language=" + code,
then,
priorityIf: prioritySearch,
searchTerms: {
2022-09-08 21:40:48 +02:00
"*": [code],
},
2022-07-11 09:14:26 +02:00
})
})
2022-09-08 21:40:48 +02:00
2022-07-11 09:14:26 +02:00
const wikidataLayer = <LayerConfigJson>{
id: "wikidata",
description: {
2022-09-08 21:40:48 +02:00
en: "Various tagrenderings which are generated from Wikidata. Automatically generated with a script, don't edit manually",
},
2022-07-11 09:14:26 +02:00
"#dont-translate": "*",
2022-09-08 21:40:48 +02:00
source: {
osmTags: "id~*",
2022-07-11 09:14:26 +02:00
},
title: null,
2022-09-08 21:40:48 +02:00
mapRendering: null,
2022-07-11 09:14:26 +02:00
tagRenderings: [
{
id: "language",
// @ts-ignore
description: "Enables to pick *a single* 'language:<lng>=yes' within the mappings",
mappings,
},
{
builtin: "wikidata.language",
override: {
id: "language-multi",
// @ts-ignore
2022-09-08 21:40:48 +02:00
description:
"Enables to pick *multiple* 'language:<lng>=yes' within the mappings",
multiAnswer: true,
},
2022-07-11 09:14:26 +02:00
},
{
2022-09-08 21:40:48 +02:00
id: "school-language",
2022-07-11 09:14:26 +02:00
// @ts-ignore
description: "Enables to pick a single 'school:language=<lng>' within the mappings",
multiAnswer: true,
2022-09-08 21:40:48 +02:00
mappings: schoolmappings,
},
],
2022-07-11 09:14:26 +02:00
}
const dir = "./assets/layers/wikidata/"
2022-09-08 21:40:48 +02:00
if (!existsSync(dir)) {
2022-07-11 09:14:26 +02:00
mkdirSync(dir)
}
const path = dir + "wikidata.json"
writeFileSync(path, JSON.stringify(wikidataLayer, null, " "))
2022-09-08 21:40:48 +02:00
console.log("Written " + path)
2022-07-11 09:14:26 +02:00
}
2022-09-08 21:40:48 +02:00
main()