#!perl # This program massages the driving directions (in USA) for plain tex +t # printing (preferably in monospace font, w/ a blank line between eac +h non # empty line), with these priorities (in no particular order) ... # - up-case road names, exit numbers, places # - low-case everything else # - expand n, w, e, s to north, west, east, south # - shorten road type (like mailing address), e.g. 'road' to 'rd', ' +lane' to # 'ln', etc. # - removes the annoying 'go' from 'go <this much>' use warnings; use strict; my ( $geo_pos , $roads ) = ( get_geo_pos() , get_road() ); my @relative_pos = map uc( $_ ) , qw(left middle center right) ; # Got a better name for this variable? my $less_important_than_roads_places = qr{ \b ( bear | stay | head | continue | follow | go | take | turn | entry | ramp | (?: to | fore? )w[oa]rds? | on.?to | on | to | for | at | off? | then | t?here | from | an?d? | (?: h(?:ou)?r | min(?:ute)? | second | moment )s? | ph(?:one)? | fax | e.?mail | home | office | work | cell | mo +bile ) \b }xi; my $misc_edits = { 'and' => '&' , 'hour' => 'hr' , 'minutes?' => 'min' , 'seconds' => 'second' , 'phone' => 'ph' , 'fore?w[oa]rd' => 'forward' , 'toword' => 'toward' } ; { local $^I = '' ; while ( <> ) { s{ \s+ the \s+ }/ /xig; s{ \b go (?: \s+ for )? \s+ (\d) }/ $1/xige; $_ = uc $_; s{ (\d) \W* ( mi(?:le)? | ft) \b }/"$1~" . lc $2/xige; s{ $less_important_than_roads_places }/lc $1/xige; for my $map ( $roads , $geo_pos , $misc_edits ) { while ( my ( $k , $v ) = each %$map ) { s[ \b $k \b ]/$v/xig; } } for my $r ( values %$roads ) { s/\b $r \./$r/xig; } s/^[ \t]+//g; s/[ \t]+$//g; s/[ \t]+/ /g; print; } } sub get_geo_pos { my @points = qw( east west north south north-east north-west south-east south-west east-north east-south west-north west-south ) ; my %points ; foreach ( @points ) { m{^ ([a-z])[a-z]+ (?: \W+ ( [a-z])[a-z]+ )? $}xi; $points{ $2 ? "$1$2" : $1 } = uc $_ ; } return { %points }; } sub get_road { my %roads = ( 'avenue' => 'ave' , 'av' => 'ave' , 'circle' => 'crl' , 'court' => 'ct' , 'crt' => 'ct' , 'drive' => 'dr' , 'lane' => 'ln' , 'pk' => 'pike' , 'parkway' => 'pkwy' , 'pky' => 'pkwy' , 'road' => 'rd' , 'route' => 'rt' , 'street' => 'st' , 'tpk' => 'turnpike' , 'turn.?pk' => 'turnpike' ); $_ = uc $_ for values %roads; return { %roads }; }

In reply to Massage the driving directions (in USA) by parv

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.