Skip to content

Latest commit

 

History

History
70 lines (58 loc) · 6.23 KB

File metadata and controls

70 lines (58 loc) · 6.23 KB

Grammar

Many languages - all Slavic (Russian, Ukrainian, Polish, Bulgarian, etc), Finnic (Finnish, Estonian) and others - have grammatical case feature that could be supported in OSRM Text Instructions too. Originally street names are being inserted into instructions as they're in OSM map - in nominative case. To be grammatically correct, street names should be changed according to target language rules and instruction context before insertion.

Actually grammatical case applying is not the simple and obvious task due to real-life languages complexity. It even looks so hard so, for example, all known native Russian navigation systems don't speak street names in their pronounceable route instructions at all.

But fortunately street names have restricted lexicon and naming rules and so this task could be relatively easily solved for this particular case.

Implementation details

The quite universal and simplier solution is the changing street names with the prepared set of regular expressions grouped by required grammatical case. The required grammatical case should be specified right in instruction's substitution variables:

  • {way_name} and {rotary_name} variables in translated instructions should be appended with required grammar case name after colon: {way_name:accusative} for example
  • This folder should contain language-specific JSON file with regular expressions for specified grammatical case:
    {
        "v5": {
            "accusative": [
                ["^ (\\S+)ая-(\\S+)ая [Уу]лица ", " $1ую-$2ую улицу "],
                ["^ (\\S+)ая [Уу]лица ", " $1ую улицу "],
                ...
            ]
        }
    }
  • All such JSON files should be registered in common languages.js
  • Instruction text formatter (index.js in this module) should:
    • check {way_name} and {rotary_name} variables for optional grammar case after colon: {way_name:accusative}
    • find appropriate regular expressions block for target language and specified grammar case
    • call standard string replace with regular expression for each expression in block passing result from previous call to the next; the first call should enclose original street name with whitespaces to make parsing several words inside name a bit simplier.
  • Strings replacement with regular expression is available in almost all other programming language and so this should not be the problem for other code used OSRM Text Instructions' data only.
  • Grammar JSON could have regular expression flags in JS notation:
    {
        "meta": {
            "regExpFlags": "ig"
        }
    }
  • Please note, not all JS regular expression flags could be supported in other languages. For example, OSRM Text Instructions for Swift don't support "non-global match" and so always supposes g flag turned on. So if some regular expressions suppose stopping after their match, please include ^ and/or $ into patterns for exact matching or return "finished" string in replace expression without enclosing whitespaces.
  • If there is no regular expression matched source name (that's for names from foreign country for example), original name is returned without changes. This is also expected behavior of standard string replace with regular expression. And the same behavior is expected in case of missing grammar JSON file or grammar case inside it.

Example

Russian "Большая Монетная улица" street from St Petersburg (Big Monetary Street in rough translation) after processing with Russian grammar rules will look in following instructions as:

  • "Turn left onto {way_name}" => ru:"Поверните налево на {way_name:accusative}" => "Поверните налево на Большую Монетную улицу"
  • "Continue onto {way_name}" => ru:"Продолжите движение по {way_name:dative}" => "Продолжите движение по Большой Монетной улице"
  • "Make a U-turn onto {way_name} at the end of the road" => ru:"Развернитесь в конце {way_name:genitive}" => "Развернитесь в конце Большой Монетной улицы"
  • "Make a U-turn onto {way_name}" => ru:"Развернитесь на {way_name:prepositional}" => "Развернитесь на Большой Монетной улице"

Design goals

  • Cross platform - uses the same data-driven approach as OSRM Text Instructions
  • Test suite - has prepared test to check available expressions automatically and has easily extendable language-specific names testing pattern
  • Customization - could be easily extended for other languages with adding new regular expressions blocks into this grammar support folder and modifying {way_name} and other variables in translated instructions only with necessary grammatical case labels

Notes