@@ -27,7 +27,8 @@ using namespace GeoNLP;
27
27
#define ADDRESS_PARSER_LABEL_COUNTRY_REGION " country_region"
28
28
#define ADDRESS_PARSER_LABEL_COUNTRY " country"
29
29
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"
31
32
32
33
// ////////////////////////////////////////////////////////////////////
33
34
// / Helper string functions
@@ -311,29 +312,28 @@ bool Postal::parse(const std::string &input, std::vector<Postal::ParseResult> &r
311
312
{
312
313
std::vector<std::string> hier;
313
314
split_tokens (input, ' ,' , hier);
315
+ if (hier.empty ()) hier.push_back (input);
314
316
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++)
316
321
{
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 )
321
326
{
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 ;
333
331
}
334
-
335
- expand ( prim, result) ;
332
+ std::vector<std::string> pc; pc. push_back (v);
333
+ prim[key] = pc ;
336
334
}
335
+
336
+ expand (prim, result);
337
337
}
338
338
339
339
if (m_initialize_for_every_call) drop ();
@@ -370,7 +370,7 @@ void Postal::expand(const Postal::ParseResult &input, std::vector<Postal::ParseR
370
370
{
371
371
std::vector< std::string > norm;
372
372
// 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 )
374
374
norm.push_back (tonorm);
375
375
else
376
376
{
@@ -492,6 +492,11 @@ void Postal::result2hierarchy(const std::vector<ParseResult> &p, std::vector<Hie
492
492
}
493
493
}
494
494
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
+
495
500
h.push_back (h_result);
496
501
}
497
502
}
0 commit comments