@@ -27,7 +27,8 @@ using namespace GeoNLP;
2727#define ADDRESS_PARSER_LABEL_COUNTRY_REGION " country_region"
2828#define ADDRESS_PARSER_LABEL_COUNTRY " country"
2929
30- #define PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE " post:"
30+ #define PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_INPUT " post:"
31+ #define PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY " h-postcode"
3132
3233// ////////////////////////////////////////////////////////////////////
3334// / Helper string functions
@@ -311,29 +312,28 @@ bool Postal::parse(const std::string &input, std::vector<Postal::ParseResult> &r
311312 {
312313 std::vector<std::string> hier;
313314 split_tokens (input, ' ,' , hier);
315+ if (hier.empty ()) hier.push_back (input);
314316
315- if (!hier.empty ())
317+ ParseResult prim;
318+ int shift = 0 ;
319+ size_t np = strlen (PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_INPUT);
320+ for (size_t j = 0 ; j < hier.size (); j++)
316321 {
317- ParseResult prim ;
318- int shift = 0 ;
319- size_t np = strlen (PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE );
320- for ( size_t j = 0 ; j < hier. size (); j++ )
322+ std::string v = hier[hier. size ()-j- 1 ] ;
323+ std::string key = primitive_key (j-shift) ;
324+ v = trim (v );
325+ if (v. compare ( 0 , np, PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_INPUT) == 0 )
321326 {
322- std::string v = hier[hier.size ()-j-1 ];
323- std::string key = primitive_key (j-shift);
324- if (v.compare (0 , np, PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE) == 0 )
325- {
326- v = v.substr (np+1 );
327- v = trim (v);
328- key = PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE;
329- shift += 1 ;
330- }
331- std::vector<std::string> pc; pc.push_back (v);
332- prim[key] = pc;
327+ v = v.substr (np);
328+ v = trim (v);
329+ key = PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY;
330+ shift += 1 ;
333331 }
334-
335- expand ( prim, result) ;
332+ std::vector<std::string> pc; pc. push_back (v);
333+ prim[key] = pc ;
336334 }
335+
336+ expand (prim, result);
337337 }
338338
339339 if (m_initialize_for_every_call) drop ();
@@ -370,7 +370,7 @@ void Postal::expand(const Postal::ParseResult &input, std::vector<Postal::ParseR
370370 {
371371 std::vector< std::string > norm;
372372 // no need to keep postal code in normalized and expanded
373- if (i.first == ADDRESS_PARSER_LABEL_POSTAL_CODE)
373+ if (i.first == ADDRESS_PARSER_LABEL_POSTAL_CODE || i. first == PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY )
374374 norm.push_back (tonorm);
375375 else
376376 {
@@ -492,6 +492,11 @@ void Postal::result2hierarchy(const std::vector<ParseResult> &p, std::vector<Hie
492492 }
493493 }
494494
495+ // overwrite with the primitive parser postal code if needed
496+ ParseResult::const_iterator it = r.find (PRIMITIVE_ADDRESS_PARSER_POSTAL_CODE_KEY);
497+ if (it != r.end () && it->second .size ())
498+ postal_code = normalize_postalcode (it->second [0 ]);
499+
495500 h.push_back (h_result);
496501 }
497502}
0 commit comments