From 7fb4d73e8bde139dbb5a46c970ea932429a63ec7 Mon Sep 17 00:00:00 2001 From: Eric Bryant Date: Sun, 20 Feb 2022 21:50:56 -0500 Subject: [PATCH] LABEL_SELFREF improvements (DataProcessing) * inspect colocated routes, detect FPs, catecorize what's left * more micro-optimizations where practicable * require banner match * cope with banner after slash * flag named routes after slash * ignore banners starting with `-`, e.g. FRA depts * cope with closed waypoints * ignore same number different prefix * account for abbrev * ignore "Old" designations --- siteupdate/cplusplus/Makefile | 1 + .../classes/Route/compute_stats_r.cpp | 1 + .../cplusplus/classes/Route/read_wpt.cpp | 1 - .../cplusplus/classes/Waypoint/Waypoint.cpp | 122 +++++++++++++----- .../cplusplus/classes/Waypoint/Waypoint.h | 7 +- siteupdate/cplusplus/functions/strd.cpp | 16 +++ siteupdate/cplusplus/functions/strd.h | 2 + siteupdate/python-teresco/siteupdate.py | 98 ++++++++++---- 8 files changed, 189 insertions(+), 59 deletions(-) create mode 100644 siteupdate/cplusplus/functions/strd.cpp create mode 100644 siteupdate/cplusplus/functions/strd.h diff --git a/siteupdate/cplusplus/Makefile b/siteupdate/cplusplus/Makefile index 0e904de2..b09acba8 100644 --- a/siteupdate/cplusplus/Makefile +++ b/siteupdate/cplusplus/Makefile @@ -40,6 +40,7 @@ CommonObjects = \ functions/format_clinched_mi.o \ functions/lower.o \ functions/split.o \ + functions/strd.o \ functions/upper.o \ functions/valid_num_str.o diff --git a/siteupdate/cplusplus/classes/Route/compute_stats_r.cpp b/siteupdate/cplusplus/classes/Route/compute_stats_r.cpp index 3bd04b24..15dc06d4 100644 --- a/siteupdate/cplusplus/classes/Route/compute_stats_r.cpp +++ b/siteupdate/cplusplus/classes/Route/compute_stats_r.cpp @@ -64,4 +64,5 @@ void Route::compute_stats_r() } else if (city.empty()) Datacheck::add(this, "", "", "", "ABBREV_NO_CITY", CSV_LINE); #undef CSV_LINE + for (Waypoint* w : point_list) w->label_selfref(); } diff --git a/siteupdate/cplusplus/classes/Route/read_wpt.cpp b/siteupdate/cplusplus/classes/Route/read_wpt.cpp index 7989bf9b..1f88a2fc 100644 --- a/siteupdate/cplusplus/classes/Route/read_wpt.cpp +++ b/siteupdate/cplusplus/classes/Route/read_wpt.cpp @@ -92,7 +92,6 @@ void Route::read_wpt(WaypointQuadtree *all_waypoints, ErrorList *el, bool usa_fl w->label_invalid_ends(); w->label_looks_hidden(); w->label_parens(); - w->label_selfref(slash); w->label_slashes(slash); w->lacks_generic(); w->underscore_datachecks(slash); diff --git a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp index 5e8eabf0..1ba93ed6 100644 --- a/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp +++ b/siteupdate/cplusplus/classes/Waypoint/Waypoint.cpp @@ -3,6 +3,7 @@ #include "../DBFieldLength/DBFieldLength.h" #include "../HighwaySystem/HighwaySystem.h" #include "../Route/Route.h" +#include "../../functions/strd.h" #include "../../functions/valid_num_str.h" #include #include @@ -311,6 +312,55 @@ bool Waypoint::label_references_route(Route *r) return 0; } +Route* Waypoint::coloc_same_number(const char* digits) +{ if (colocated) + for (Waypoint* w : *colocated) + { if (w == this) continue; + const char* d = w->route->route.data(); + while (!isdigit(*d) && *d) ++d; + std::string other(d); + if (w->route->banner[0] == '-') other += w->route->banner; + if (digits == other) return w->route; + } + return 0; +} + +Route* Waypoint::coloc_same_designation(const std::string& rte) +{ if (colocated) + for (Waypoint* w : *colocated) + if ( w != this && rte == (w->route->banner[0] == '-' ? w->route->route : w->route->name_no_abbrev()) ) + return w->route; + return 0; +} + +Route* Waypoint::self_intersection() +{ if (colocated) + for (Waypoint* w : *colocated) + if (w != this && w->route == route) + return w->route; + return 0; +} + +bool Waypoint::banner_after_slash(const char* slash) +{ // return whether colocated with a bannered route of same designation whose banner + // is contained in this waypoint's label after a slash, before underscore if exists + if (colocated) + for (Waypoint* w : *colocated) + if (w != this && w->route->banner.size() && w->route->route == route->route && strdstr(slash+1, w->route->banner.data(), '_')) + return 1; + return 0; +} + +Route* Waypoint::coloc_banner_matches_abbrev() +{ // return whether colocated with a bannered route of same designation whose banner + // matches this route's abbrev + if (colocated) + for (Waypoint* w : *colocated) + if (w != this && w->route->route == route->route && w->route->banner == route->abbrev) + return w->route; + return 0; +} + /* Datacheck */ void Waypoint::distance_update(char *fstr, double &vis_dist, Waypoint *prev_w) @@ -426,42 +476,48 @@ void Waypoint::label_parens() Datacheck::add(route, label, "", "", "LABEL_PARENS", ""); } -void Waypoint::label_selfref(const char *slash) -{ // looking for the route within the label - // partially complete "references own route" -- too many FP +void Waypoint::label_selfref() +{ // "label references own route" + #define FLAG(SUBTYPE) Datacheck::add(route, label, "", "", "LABEL_SELFREF", SUBTYPE) + const char* l = label.data() + (label[0] == '*'); + std::string rte = route->banner[0] == '-' ? route->route : route->name_no_abbrev(); // first check for number match after a slash, if there is one - if (slash) - { char* digit_starts = isdigit(route->route.back()) - ? &route->route.back() - : isdigit(route->route[route->route.size()-2]) && isalpha(route->route.back()) ? &route->route.back()-1 : 0; - if (digit_starts) - { while (digit_starts > route->route.data() && isdigit(digit_starts[-1])) digit_starts--; - if (!strcmp(slash+1, digit_starts) || !strcmp(slash+1, route->route.data())) - { Datacheck::add(route, label, "", "", "LABEL_SELFREF", ""); - return; - } - const char *underscore = strchr(slash+1, '_'); - if (underscore) - { std::string postslash(slash+1, underscore-slash-1); - if (postslash == digit_starts || postslash == route->route) - { Datacheck::add(route, label, "", "", "LABEL_SELFREF", ""); - return; - } - } - } + const char* slash = strchr(l, '/'); + if (slash++ && strncmp(l, "Old", 3)) + { const char* digits = rte.data(); + while (!isdigit(*digits) && *digits) ++digits; + if ( digits != rte.data()+rte.size() + && !strdcmp(slash, digits, '_') && !coloc_same_number(digits) + || !strdcmp(slash, rte.data(), '_') && !coloc_same_designation(rte) + ) return FLAG("NO_COLOC"); } // now the remaining checks - std::string rte_ban = route->route + route->banner; - if ( strncmp(label.data(), rte_ban.data(), rte_ban.size()) ) return; - const char *c = label.data()+rte_ban.size(); - if (*c == 0 || *c == '/') - Datacheck::add(route, label, "", "", "LABEL_SELFREF", ""); - else if (*c == '_') - if (*++c != 'U') - Datacheck::add(route, label, "", "", "LABEL_SELFREF", ""); - else { while (isdigit(*++c)); - if (*c) Datacheck::add(route, label, "", "", "LABEL_SELFREF", ""); - } + if ( strncmp(l, rte.data(), rte.size()) ) return; + const char *c = l+rte.size(); + switch(*c) + { case 0: { Route* r = coloc_same_designation(rte); + if (!r) FLAG("NO_COLOC"); + else if (r == route) FLAG("NO_SUFFIX"); + else if (r->abbrev.size()) FLAG("NO_ABBREV"); + } return; + case '_': { if (*++c == 'U') + { while (isdigit(*++c)); + if (!*c) return; + } + Route* r = coloc_same_designation(rte); + if (!r) FLAG("NO_COLOC"); + else if (r->abbrev.size()) FLAG("NO_ABBREV"); + } return; + case '/': if ((route->banner.size() && route->banner[0] != '-' || !banner_after_slash(slash)) && !coloc_same_designation(rte)) + FLAG("NO_COLOC"); + return; + default: if ( route->abbrev.size() + && !strncmp(c, route->abbrev.data(), route->abbrev.size()) + && (!self_intersection() || !c[route->abbrev.size()]) + && !coloc_banner_matches_abbrev() + ) FLAG(strchr("_/", c[route->abbrev.size()]) ? "TRUE_ERROR" : "FULL_MATCH"); + } + #undef FLAG } void Waypoint::label_slashes(const char *slash) diff --git a/siteupdate/cplusplus/classes/Waypoint/Waypoint.h b/siteupdate/cplusplus/classes/Waypoint/Waypoint.h index f400287d..75c7b30f 100644 --- a/siteupdate/cplusplus/classes/Waypoint/Waypoint.h +++ b/siteupdate/cplusplus/classes/Waypoint/Waypoint.h @@ -44,6 +44,11 @@ class Waypoint void nmplogs(std::unordered_set &, std::ofstream &, std::list &); Waypoint* hashpoint(); bool label_references_route(Route *); + Route* coloc_same_number(const char*); + Route* coloc_same_designation(const std::string&); + Route* self_intersection(); + bool banner_after_slash(const char*); + Route* coloc_banner_matches_abbrev(); // Datacheck void distance_update(char *, double &, Waypoint *); @@ -55,7 +60,7 @@ class Waypoint void label_invalid_ends(); void label_looks_hidden(); void label_parens(); - void label_selfref(const char *); + void label_selfref(); void label_slashes(const char *); void lacks_generic(); void underscore_datachecks(const char *); diff --git a/siteupdate/cplusplus/functions/strd.cpp b/siteupdate/cplusplus/functions/strd.cpp new file mode 100644 index 00000000..4e3d8caf --- /dev/null +++ b/siteupdate/cplusplus/functions/strd.cpp @@ -0,0 +1,16 @@ +const char* strdstr(const char* h, const char* n, const char& d) +{ for (; *h && *h != d; h++) + { const char* a = h; + const char* b = n; + while (*a != d) + if (*a++ != *b++) break; + else if (!*b) return h; + } + return 0; +} + +int strdcmp(const char* a, const char* b, const char& d) +{ do if (!*b) return *a == d ? 0 : *a-*b; + while (*a++ == *b++); + return *--a - *--b; +} diff --git a/siteupdate/cplusplus/functions/strd.h b/siteupdate/cplusplus/functions/strd.h new file mode 100644 index 00000000..7fa8abd5 --- /dev/null +++ b/siteupdate/cplusplus/functions/strd.h @@ -0,0 +1,2 @@ +const char* strdstr(const char*, const char*, const char&); +int strdcmp(const char*, const char*, const char&); diff --git a/siteupdate/python-teresco/siteupdate.py b/siteupdate/python-teresco/siteupdate.py index 946d7103..7ae5d2bd 100755 --- a/siteupdate/python-teresco/siteupdate.py +++ b/siteupdate/python-teresco/siteupdate.py @@ -785,7 +785,7 @@ def is_or_colocated_with_active_or_preview(self): return False def hashpoint(self): - # return a canonical waypoint for graph vertex hashtable lookup + """return a canonical waypoint for graph vertex hashtable lookup""" if self.colocated is None: return self return self.colocated[0] @@ -808,32 +808,82 @@ def label_references_route(self, r, datacheckerrors): #datacheckerrors.append(DatacheckEntry(route, [self.label], "UNEXPECTED_DESIGNATION", self.label[len(no_abbrev)+len(r.abbrev)+1:])) return False + def coloc_same_number(self, digits): + if self.colocated: + for w in self.colocated: + if w == self: + continue; + d = 0 + while d < len(w.route.route) and not w.route.route[d].isdigit(): + d += 1 + other = w.route.route[d:] + if w.route.banner.startswith('-'): + other += w.route.banner + if digits == other: + return True + return False + + def coloc_same_designation(self, rte): + if self.colocated: + for w in self.colocated: + if w != self and rte == (w.route.route if w.route.banner.startswith('-') else w.route.name_no_abbrev()): + return w.route; + return None; + + def banner_after_slash(self, postslash): + """return whether colocated with a bannered route of same designation whose banner + is contained in this waypoint's label after a slash, before underscore if exists""" + if self.colocated is not None: + for w in self.colocated: + if w != self and len(w.route.banner) and w.route.route == self.route.route and w.route.banner in postslash: + return True + return False + def label_selfref(self): - # partially complete "references own route" -- too many FP + """label references own route""" + l = self.label[(self.label[0] == '*'):] + rte = r.route if r.banner.startswith('-') else r.name_no_abbrev() # first check for number match after a slash, if there is one - if '/' in self.label: - digit_starts = len(r.route)-1 if r.route[-1].isdigit() else \ - len(r.route)-2 if r.route[-2].isdigit() and r.route[-1].isalpha() else 0 - if digit_starts: - while digit_starts >= 0 and r.route[digit_starts].isdigit(): - digit_starts-=1 - digits = r.route[digit_starts+1:] - postslash = self.label[self.label.index('/')+1:] - if postslash == digits or postslash == r.route: - datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF')) - return - if '_' in postslash: - postslash = postslash[:postslash.index('_')] - if postslash == digits or postslash == r.route: - datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF')) - return + if '/' in l and not l.startswith("Old"): + digit_starts = 0 + while digit_starts < len(rte) and not rte[digit_starts].isdigit(): + digit_starts+=1 + digits = rte[digit_starts:] + postslash = l[l.index('/')+1:] + if '_' in postslash: + postslash = postslash[:postslash.index('_')] + if len(digits) \ + and postslash == digits and not self.coloc_same_number(digits) \ + or postslash == rte and not self.coloc_same_designation(rte): + return datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_COLOC')) # now the remaining checks - rte_ban = r.route+r.banner - if self.label.startswith(rte_ban): - c = len(rte_ban) - l = self.label - if len(l) == c or l[c] == '_' and (len(l) == c+1 or l[c+1] != 'U' or len(l) > c+2 and not l[c+2:].isdigit()) or l[c] == '/': - datacheckerrors.append(DatacheckEntry(r,[l],'LABEL_SELFREF')) + if l.startswith(rte): + c = len(rte) + if c == len(l): + route = self.coloc_same_designation(rte) + if route is None: + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_COLOC')) + elif route == r: + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_SUFFIX')) + elif len(route.abbrev): + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_ABBREV')) + elif l[c] == '_': + if len(l) == c+1 or l[c+1] != 'U' or len(l) > c+2 and not l[c+2:].isdigit(): + route = self.coloc_same_designation(rte) + if route is None: + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_COLOC')) + elif len(route.abbrev): + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_ABBREV')) + elif l[c] == '/': + if (len(r.banner) and r.banner[0] != '-' or not self.banner_after_slash(postslash)) and not self.coloc_same_designation(rte): + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF','NO_COLOC')) + elif len(r.abbrev) \ + and l[c:].startswith(r.abbrev) \ + and (self.colocated is None \ + or (r not in [p.route for p in self.colocated if p != self] or len(l) == c+len(r.abbrev)) \ + and r.abbrev not in [p.route.banner for p in self.colocated if p != self]): + datacheckerrors.append(DatacheckEntry(r,[self.label],'LABEL_SELFREF', \ + "TRUE_ERROR" if len(l) == c+len(r.abbrev) or l[c+len(r.abbrev)] in "_/" else "FULL_MATCH")) class HighwaySegment: """This class represents one highway segment: the connection between two