Skip to main content

geocoder_core/profile/fr/
mod.rs

1//! France, configured like the address index of the Géoplateforme geocoder
2//! (IGN), which serves the BAN API: addok-fr 1.1.0 + addok-france 1.2.0 on
3//! addok 1.3, with `indexes/address/config/addok.conf` of
4//! `geoplateforme/geocodage/geocodeur`. The versions are inferred from the
5//! API behaviour (multi-value filters, labels for former municipalities,
6//! addok-fr 1.1 phonetics), the configuration is the published one.
7
8pub(crate) mod france;
9mod phonemicize;
10
11use once_cell::sync::Lazy;
12
13use super::{Boost, FieldSpec, Profile};
14use crate::doc::Document;
15use crate::text::{Synonyms, Token};
16
17pub use phonemicize::phonemicize;
18
19/// addok-fr synonyms, also the base of the Benelux profiles.
20pub(crate) const SYNONYMS_SOURCE: &str = include_str!("synonyms.txt");
21
22static SYNONYMS: Lazy<Synonyms> = Lazy::new(|| Synonyms::parse(SYNONYMS_SOURCE));
23
24/// Default fields plus the Géoplateforme `EXTRA_FIELDS` (`citycode`).
25pub(crate) const FIELDS: &[FieldSpec] = &[
26    FieldSpec {
27        key: "name",
28        boost: Boost::Const(4.0),
29        required: true,
30    },
31    FieldSpec::new("street"),
32    FieldSpec {
33        key: "postcode",
34        boost: Boost::ByType {
35            doc_type: "municipality",
36            matched: 1.2,
37            otherwise: 1.0,
38        },
39        required: false,
40    },
41    FieldSpec::new("city"),
42    FieldSpec::new("context"),
43    FieldSpec::new("citycode"),
44];
45
46const FILTERS: &[&str] = &["source", "type", "citycode", "postcode"];
47
48pub struct FrProfile {
49    _private: (),
50}
51
52impl FrProfile {
53    pub fn new() -> Self {
54        Self { _private: () }
55    }
56}
57
58impl Default for FrProfile {
59    fn default() -> Self {
60        Self::new()
61    }
62}
63
64impl Profile for FrProfile {
65    fn name(&self) -> &'static str {
66        "fr"
67    }
68
69    fn version(&self) -> u32 {
70        3
71    }
72
73    fn min_score(&self) -> f64 {
74        0.3
75    }
76
77    /// The Géoplateforme asks addok for at least 10 results
78    /// (`api/indexes/address.js`: `Math.max(params.limit || 10, 10)`).
79    fn min_limit(&self) -> usize {
80        10
81    }
82
83    fn fields(&self) -> &[FieldSpec] {
84        FIELDS
85    }
86
87    fn filters(&self) -> &[&'static str] {
88        FILTERS
89    }
90
91    fn synonyms(&self) -> Option<&Synonyms> {
92        Some(&SYNONYMS)
93    }
94
95    /// `extract_address`, `clean_query`, `remove_leading_zeros`.
96    fn process_query(&self, query: &str) -> String {
97        let query = france::extract_address(query);
98        let query = france::clean_query(&query);
99        france::remove_leading_zeros(&query)
100    }
101
102    /// `glue_ordinal`, `fold_ordinal`, `flag_housenumber`, `synonymize`,
103    /// `phonemicize`.
104    fn process_tokens(&self, tokens: Vec<Token>) -> Vec<Token> {
105        let tokens = france::glue_ordinal(tokens);
106        let tokens: Vec<Token> = tokens.into_iter().map(france::fold_ordinal).collect();
107        let tokens = france::flag_housenumber(tokens);
108        let tokens = SYNONYMS.apply(tokens);
109        tokens
110            .into_iter()
111            .map(|t| t.with_value(phonemicize(&t.value)))
112            .collect()
113    }
114
115    fn make_labels(&self, doc: &Document, housenumber: Option<&str>) -> Vec<String> {
116        france::make_labels(doc, housenumber)
117    }
118}