Add pseudo-departement for generating at a level of region or country, and not just departement

This commit is contained in:
Feufochmar 2019-11-06 11:53:50 +01:00
parent 398cac33ad
commit 0722a0102b
1 changed files with 119 additions and 25 deletions

View File

@ -34,6 +34,12 @@ genercommunes.Distribution.prototype.generate = function () {
}
return pick
}
// Merge from another distribution
genercommunes.Distribution.prototype.addFromDistribution = function (dist) {
for (var [item, count] of dist.items) {
this.addTo(item, count)
}
}
// Parse a distribution from the serialized model
genercommunes.Distribution.parse = function (dist) {
@ -73,6 +79,22 @@ genercommunes.ChainGenerator.prototype.generate = function () {
}
return next(this, current, new Array())
}
// Merge from a Chain Generator
genercommunes.ChainGenerator.prototype.addFromChainGenerator = function (chainGen) {
if (this.order === chainGen.order) {
for (var [seq, dist] of chainGen.nextItems) {
if (this.nextItems.has(seq)) {
this.nextItems.get(seq).addFromDistribution(dist)
} else {
var newDist = new genercommunes.Distribution()
newDist.addFromDistribution(dist)
this.nextItems.set(seq, newDist)
}
}
} else {
throw "Cannot merge ChainGenerator with different orders."
}
}
// Parse a Chain generator from the serialized model
genercommunes.ChainGenerator.parse = function (gen) {
@ -84,23 +106,71 @@ genercommunes.ChainGenerator.parse = function (gen) {
}
// Departement
// Build from a serialized model
genercommunes.Departement = function (dep) {
this.id = dep.id
this.name = dep.name
genercommunes.Departement = function (id, name, localityOrder, areaOrder, riverOrder) {
this.id = id
this.name = name
// Distributions
this.patterns = genercommunes.Distribution.parse(dep['patterns'])
this.wordMasculine = genercommunes.Distribution.parse(dep['word-masculine'])
this.wordFeminine = genercommunes.Distribution.parse(dep['word-feminine'])
this.wordPlural = genercommunes.Distribution.parse(dep['word-plural'])
this.wordMasculinePlural = genercommunes.Distribution.parse(dep['word-masculine-plural'])
this.wordFemininePlural = genercommunes.Distribution.parse(dep['word-feminine-plural'])
this.personNameMasculine = genercommunes.Distribution.parse(dep['person-name-masculine'])
this.personNameFeminine = genercommunes.Distribution.parse(dep['person-name-feminine'])
this.patterns = new genercommunes.Distribution()
this.wordMasculine = new genercommunes.Distribution()
this.wordFeminine = new genercommunes.Distribution()
this.wordPlural = new genercommunes.Distribution()
this.wordMasculinePlural = new genercommunes.Distribution()
this.wordFemininePlural = new genercommunes.Distribution()
this.personNameMasculine = new genercommunes.Distribution()
this.personNameFeminine = new genercommunes.Distribution()
// Chain generators
this.localityName = genercommunes.ChainGenerator.parse(dep['locality-name'])
this.areaName = genercommunes.ChainGenerator.parse(dep['area-name'])
this.riverName = genercommunes.ChainGenerator.parse(dep['river-name'])
this.localityName = new genercommunes.ChainGenerator(localityOrder)
this.areaName = new genercommunes.ChainGenerator(areaOrder)
this.riverName = new genercommunes.ChainGenerator(riverOrder)
}
// Build from a serialized model
genercommunes.Departement.parse = function (dep) {
var ret = new genercommunes.Departement(dep.id, dep.name, 1, 1, 1)
// Distributions
ret.patterns = genercommunes.Distribution.parse(dep['patterns'])
ret.wordMasculine = genercommunes.Distribution.parse(dep['word-masculine'])
ret.wordFeminine = genercommunes.Distribution.parse(dep['word-feminine'])
ret.wordPlural = genercommunes.Distribution.parse(dep['word-plural'])
ret.wordMasculinePlural = genercommunes.Distribution.parse(dep['word-masculine-plural'])
ret.wordFemininePlural = genercommunes.Distribution.parse(dep['word-feminine-plural'])
ret.personNameMasculine = genercommunes.Distribution.parse(dep['person-name-masculine'])
ret.personNameFeminine = genercommunes.Distribution.parse(dep['person-name-feminine'])
// Chain generators
ret.localityName = genercommunes.ChainGenerator.parse(dep['locality-name'])
ret.areaName = genercommunes.ChainGenerator.parse(dep['area-name'])
ret.riverName = genercommunes.ChainGenerator.parse(dep['river-name'])
return ret
}
// Merge from a departement
genercommunes.Departement.prototype.addFromDepartement = function (dep) {
this.patterns.addFromDistribution(dep.patterns)
this.wordMasculine.addFromDistribution(dep.wordMasculine)
this.wordFeminine.addFromDistribution(dep.wordFeminine)
this.wordPlural.addFromDistribution(dep.wordPlural)
this.wordMasculinePlural.addFromDistribution(dep.wordMasculinePlural)
this.wordFemininePlural.addFromDistribution(dep.wordFemininePlural)
this.personNameMasculine.addFromDistribution(dep.personNameMasculine)
this.personNameFeminine.addFromDistribution(dep.personNameFeminine)
this.localityName.addFromChainGenerator(dep.localityName)
this.areaName.addFromChainGenerator(dep.areaName)
this.riverName.addFromChainGenerator(dep.riverName)
}
// Create from a map of departements
genercommunes.Departement.fromMap = function (id, name, mapDepartements) {
// Get the order of chain generators from the first departement of the map
var departements = mapDepartements.values()
var firstDep = departements.next().value
// Build departement
var ret = new genercommunes.Departement(id, name, firstDep.localityName.order, firstDep.areaName.order, firstDep.riverName.order)
// fill the distributions
for (var [depid, dep] of mapDepartements) {
ret.addFromDepartement(dep)
}
// Return
return ret
}
// Format a generated word from a chain generator into a string
@ -149,7 +219,7 @@ genercommunes.Departement.prototype.replacePatterns = function (pat) {
// Check if a string starts with a vowel
genercommunes.isStartingWithVowel = function (str) {
var nfd = str.normalize('NFD'); // Normalize to remove the accents on the first character
return ['A', 'E', 'I', 'O', 'U', 'Y'].includes(nfd.charAt(0)) // Note: the generated words start with an uppercase.
return ['A', 'E', 'H', 'I', 'O', 'U', 'Y'].includes(nfd.charAt(0)) // Note: the generated words start with an uppercase.
}
// Transform an array tree of the patterns to a string
@ -249,17 +319,27 @@ genercommunes.Departement.prototype.generateName = function () {
}
// Region
// Build from a serialized model
genercommunes.Region = function (region) {
this.id = region.id
this.name = region.name
// Each region contains a map of departements
genercommunes.Region = function (id, name) {
this.id = id
this.name = name
this.departements = new Map()
}
// Build from a serialized model
genercommunes.Region.parse = function (region) {
var ret = new genercommunes.Region(region.id, region.name)
for (var departement of region.departements) {
this.departements.set(departement.id, new genercommunes.Departement(departement))
ret.departements.set(departement.id, genercommunes.Departement.parse(departement))
}
// Add a pseudo-departement, for combining at region-level
// TODO
// Only if there is more than one departement
if (ret.departements.size > 1) {
ret.departements.set('0', genercommunes.Departement.fromMap('0', '(' + region.name + ')', ret.departements))
}
// Sort the map
ret.departements = new Map([...ret.departements].sort())
return ret
}
// Model of the genercommunes generator
@ -301,10 +381,24 @@ genercommunes.loadModel = function (model) {
// Regions
genercommunes.model.regions = new Map()
for (var region of model) {
genercommunes.model.regions.set(region.id, new genercommunes.Region(region))
genercommunes.model.regions.set(region.id, genercommunes.Region.parse(region))
}
// Add a pseudo-region with a pseudo-departement combining all regions
// TODO
var pseudoReg = new genercommunes.Region('0', '(France)')
// To get the orders of the Chain Generator, get the first departement of the first region
var firstReg = genercommunes.model.regions.values().next().value
var firstDep = firstReg.departements.values().next().value
var pseudoDep = new genercommunes.Departement('0', '(France)', firstDep.localityName.order, firstDep.areaName.order, firstDep.riverName.order)
for (var [regid, reg] of genercommunes.model.regions) {
for (var [depid, dep] of reg.departements) {
pseudoDep.addFromDepartement(dep)
}
}
pseudoReg.departements.set(pseudoDep.id, pseudoDep)
//
genercommunes.model.regions.set(pseudoReg.id, pseudoReg)
// Sort the map
genercommunes.model.regions = new Map([...genercommunes.model.regions].sort())
// Draw the genercommunes <div>
genercommunes.drawDiv(genercommunes.model)
// Generate a first name