gtoubiana / acte

Une librairie JavaScript qui simplifie la recherche généalogique.
http://gtoubiana.github.io/acte/
MIT License
0 stars 0 forks source link

Reconnaissance des chiffres en toutes lettres #307

Closed gtoubiana closed 4 years ago

gtoubiana commented 7 years ago

Exemple:

var lettresEnChiffres = function(str) {
  var array = {
    "neuf mil(le(s)?)?": "9000+",
    "huit mil(le(s)?)?": "8000+",
    "sept mil(le(s)?)?": "7000+",
    "six mil(le(s)?)?": "6000+",
    "cinq mil(le(s)?)?": "5000+",
    "quatre mil(le(s)?)?": "4000+",
    "trois mil(le(s)?)?": "3000+",
    "deux mil(le(s)?)?": "2000+",
    "dix(-| )neuf cent(s)?": "1900+",
    "dix(-| )huit cent(s)?": "1800+",
    "dix(-| )sept cent(s)?": "1700+",
    "seize cent(s)?": "1600+",
    "quinze cent(s)?": "1500+",
    "quatorze cent(s)?": "1400+",
    "treize cent(s)?": "1300+",
    "douze cent(s)?": "1200+",
    "onze cent(s)?": "1100+",
    "mil(le(s)?)?": "1000+",
    "neuf cent(s)?": "900+",
    "huit cent(s)?": "800+",
    "sept cent(s)?": "700+",
    "six cent(s)?": "600+",
    "cinq cent(s)?": "500+",
    "quatre cent(s)?": "400+",
    "trois cent(s)?": "300+",
    "deux cent(s)?": "200+",
    "cent": "100+",
    "quatre(s)?(-| )vingt(s)?(-| )dix": "90+",
    "quatre(s)?(-| )vingt(s)?": "80+",
    "soixante(s)?(-| )dix": "70+",
    "soixante(s)?": "60+",
    "cinquante(s)?": "50+",
    "quarante(s)?": "40+",
    "trente(s)?": "30+",
    "vingt(s)?": "20+",
    "dix(-| )neuf": "19+",
    "dix(-| )huit": "18+",
    "dix(-| )sept": "17+",
    "seize": "16+",
    "quinze": "15+",
    "quatorze": "14+",
    "treize": "13+",
    "douze": "12+",
    "onze": "11+",
    "dix": "10+",
    "neuf": "9+",
    "huit": "8+",
    "sept": "7+",
    "six": "6+",
    "cinq": "5+",
    "quatre": "4+",
    "trois": "3+",
    "deux": "2+",
    "un": "1+",
    "\\+$": "",
    "[^-()\\d/*+.]": "",
    "-": ""
  };
  for (var val in array) {
    str = str.replace(new RegExp(val, "gi"), array[val]);
  }
  var EVAL_IS_DANGEROUS__AVOID_THIS = eval; /* for jslint */
  //str = str.replace("-", "");
  str = EVAL_IS_DANGEROUS__AVOID_THIS(str);
  return str;
};

console.log(lettresEnChiffres("mille huit cent quatre-vingt dix-huit"));
gtoubiana commented 7 years ago

Voir aussi ce qui a été fait pour les chiffres romaines (afin d'éviter d'utiliser eval().

const romainVersArabe = (romain) => {
  const lookup = {
    I: 1,
    V: 5,
    X: 10,
    L: 50,
    C: 100,
    D: 500,
    M: 1000,
  };
  let arabe = 0;
  let i = romain.length - 1;
  const tempRomain = romain.toUpperCase();

  for (i; i >= 0; i -= 1) {
    if (lookup[tempRomain[i]] < lookup[tempRomain[i + 1]]) {
      arabe -= lookup[tempRomain[i]];
    } else {
      arabe += lookup[tempRomain[i]];
    }
  }

  return arabe;
};

Il faudrait ajouter les chiffres dans un tableau et additionner le tout une fois le processus de reconnaissance fini.

gtoubiana commented 7 years ago

Version sans eval()

var lettresEnChiffres = function(str) {
  var array = {
    "neuf mil(le(s)?)?": "9000+",
    "huit mil(le(s)?)?": "8000+",
    "sept mil(le(s)?)?": "7000+",
    "six mil(le(s)?)?": "6000+",
    "cinq mil(le(s)?)?": "5000+",
    "quatre mil(le(s)?)?": "4000+",
    "trois mil(le(s)?)?": "3000+",
    "deux mil(le(s)?)?": "2000+",
    "dix(-| )neuf cent(s)?": "1900+",
    "dix(-| )huit cent(s)?": "1800+",
    "dix(-| )sept cent(s)?": "1700+",
    "seize cent(s)?": "1600+",
    "quinze cent(s)?": "1500+",
    "quatorze cent(s)?": "1400+",
    "treize cent(s)?": "1300+",
    "douze cent(s)?": "1200+",
    "onze cent(s)?": "1100+",
    "mil(le(s)?)?": "1000+",
    "neuf cent(s)?": "900+",
    "huit cent(s)?": "800+",
    "sept cent(s)?": "700+",
    "six cent(s)?": "600+",
    "cinq cent(s)?": "500+",
    "quatre cent(s)?": "400+",
    "trois cent(s)?": "300+",
    "deux cent(s)?": "200+",
    "cent": "100+",
    "quatre(s)?(-| )vingt(s)?(-| )dix": "90+",
    "quatre(s)?(-| )vingt(s)?": "80+",
    "soixante(s)?(-| )dix": "70+",
    "soixante(s)?": "60+",
    "cinquante(s)?": "50+",
    "quarante(s)?": "40+",
    "trente(s)?": "30+",
    "vingt(s)?": "20+",
    "dix(-| )neuf": "19+",
    "dix(-| )huit": "18+",
    "dix(-| )sept": "17+",
    "seize": "16+",
    "quinze": "15+",
    "quatorze": "14+",
    "treize": "13+",
    "douze": "12+",
    "onze": "11+",
    "dix": "10+",
    "neuf": "9+",
    "huit": "8+",
    "sept": "7+",
    "six": "6+",
    "cinq": "5+",
    "quatre": "4+",
    "trois": "3+",
    "deux": "2+",
    "un": "1+",
    "\\+$-": "",
    "[^-()\\d/*+.]": "",
    "-": ""
  };
  for (var val in array) {
    str = str.replace(new RegExp(val, "gi"), array[val]);
  }
  // https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Array/map
  return str.split("+").map(Number).reduce(function (a, b) {return a + b;}, 0);
};

console.log(lettresEnChiffres("mille six cent quatre-vingt dix-huit"));