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