summaryrefslogtreecommitdiff
path: root/scripts/acronyms.pl
blob: de79b81f82732536f273e47b36789ff6d0d5644c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;
use Encode qw(decode encode);
use List::Util qw(max sum);
use List::MoreUtils qw(true);

say <<'EOF';
package Travel::Status::DE::IRIS::Stations;

use strict;
use warnings;
use 5.014;
use utf8;

use List::Util qw(min);
use List::MoreUtils qw(firstval pairwise);
use Text::LevenshteinXS qw(distance);

# TODO switch to Text::Levenshtein::XS once AUR/Debian packages become available

our $VERSION = '1.02';

my @stations = (
EOF

my @buf;

sub process_block {
	my @histogram;
	my @borders = (0);
	my $run = 0;

	my $length = max (map { length($_) } @buf);

	for my $i (0 .. $length) {
		$histogram[$i] = true { length($_) < $i or substr($_, $i, 1) eq q{ } } @buf;

		if ($histogram[$i] == @buf) {
			if (not $run) {
				push(@borders, $i);
				$run = 1;
			}
		}
		else {
			$run = 0;
		}
	}
	for my $i (0 .. $#borders / 2) {
		for my $line (@buf) {
			my $station_offset = $borders[2 * $i];
			my $name_offset = $borders[2 * $i + 1];
			my $station_length = $name_offset - $station_offset;
			my $name_length = $borders[2 * $i + 2] ? ($borders[2 * $i + 2] - $name_offset) : undef;

			if (length($line) < $station_offset) {
				next;
			}

			my $station = substr($line, $station_offset, $station_length);
			my $name = $name_length ? substr($line, $name_offset, $name_length) : substr($line, $name_offset);

			$station =~ s{^\s+}{};
			$station =~ s{\s+$}{};
			$station =~ s{\s+}{ }g;
			$name =~ s{!}{ }g;
			$name =~ s{^\s+}{};
			$name =~ s{\s+$}{};
			$name =~ s{\s+}{ }g;
			$name =~ s{'}{\\'}g;

			if (length($station) == 0) {
				next;
			}

			printf("\t['%s','%s'],\n", encode('UTF-8', $station), encode('UTF-8', $name));
		}
	}
}

while (my $line = <STDIN>) {
	chomp $line;
	$line = decode('UTF-8', $line);

	if (length($line) == 0 and @buf) {
		process_block();
		@buf = ();
	}

	if ($line !~ m{ ^ [A-Z]{2} }x and $line !~ m{ \s [A-Z]{2,5} \s }x) {
		next;
	}

	$line =~ s{RB-Gr km}{RB-Gr!km}g;
	$line =~ s{RB-Gr!km\s++}{RB-Gr!km!}g;
	$line =~ s{Bad }{Bad!}g;

	push(@buf, $line);
}
if (@buf) {
	process_block();
}

say <<'EOF';
);

sub get_stations {
	return @stations;
}

sub normalize {
	my ($val) = @_;

	$val =~ s{Ä}{Ae}g;
	$val =~ s{Ö}{Oe}g;
	$val =~ s{Ü}{Ue}g;
	$val =~ s{ä}{ae}g;
	$val =~ s{ö}{oe}g;
	$val =~ s{ß}{sz}g;
	$val =~ s{ü}{ue}g;

	return $val;
}

sub get_station {
	my ( $name ) = @_;

	my $ds100_match = firstval { $name eq $_->[0] } @stations;

	if ($ds100_match) {
		return ($ds100_match);
	}

	return get_station_by_name($name);
}

sub get_station_by_name {
	my ( $name ) = @_;

	my $nname = lc($name);
	my $actual_match = firstval { $nname eq lc($_->[1]) } @stations;

	if ($actual_match) {
		return ($actual_match);
	}

	$nname = normalize($nname);
	$actual_match = firstval { $nname eq normalize(lc($_->[1])) } @stations;
	if ($actual_match) {
		return ($actual_match);
	}

	my @distances   = map { distance( $nname, $_->[1] ) } @stations;
	my $min_dist    = min(@distances);
	my $minp1_dist  = min( grep { $_ != $min_dist } @distances );
	my @station_map = pairwise { [ $a, $b ] } @stations, @distances;

	# arbitrary selection: edit distance < 5 is probably a typo, >= 5
	# probably means the station does not exist / has an odd name
	if ( $min_dist < 5 ) {
		return map { $_->[0] } grep { $_->[1] == $min_dist } @station_map;
	}

	# always return a list when the edit distance is large
	return map { $_->[0] } grep { $_->[1] <= $minp1_dist } @station_map;
}

1;

__END__

=head1 NAME

Travel::Status::DE::IRIS::Stations - Station name to station code mapping

=head1 SYNOPSIS

    use Travel::Status::DE::IRIS::Stations;

    my $name = 'Essen Hbf';
    my @stations = Travel::Status::DE::IRIS::Stations::get_station_by_name(
      $name);

    if (@stations < 1) {
      # no matching stations
    }
    elsif (@stations > 1) {
      # too many matches
    }
    else {
      printf("Input '%s' matched station code %s (as '%s')\n",
        $name, @{$stations[0]});
    }

=head1 VERSION

version 0.00

=head1 DESCRIPTION

This module contains a mapping of DeutscheBahn station names to station codes.
A station name is a (perhaps slightly abbreviated) string naming a particular
station; a station code is a two to five character denoting a station for the
IRIS web service.

Example station names (code in parentheses) are:
"Essen HBf" (EE), "Aachen Schanz" (KASZ), "Do UniversitE<auml>t" (EDUV).

B<Note:> Station codes may contain whitespace.

=head1 METHODS

=over

=item Travel::Status::DE::IRIS::get_stations

Returns a list of [station code, station name] listrefs lexically sorted by
station name.

=item Travel::Status::DE::IRIS::get_station(I<$in>)

Returns a list of [station code, station name] listrefs matching I<$in>.

If a I<$in> is a valid station code, only one element ([I<$in>, related name])
is returned. Otherwise, it is passed to get_station_by_name(I<$in>) (see
below).

Note that station codes matching is case sensitive and must be exact.

=item Travel::Status::DE::IRIS::get_station_by_name(I<$name>)

Returns a list of [station code, station name] listrefs where the station
name matches I<$name>.

Matching happens in two steps: If a case-insensitive exact match exists, only
this one is returned. Otherwise, all stations whose name contains I<$name> as
a substring (also case-insensitive) are returned.

This two-step behaviour makes sure that not prefix-free stations can still be
matched directly. For instance, both "Essen-Steele" and "Essen-Steele Ost"
are valid station names, but "essen-steele" will only return "Essen-Steele".

=back

=head1 DIAGNOSTICS

None.

=head1 DEPENDENCIES

=over

=item * List::MoreUtils(3pm)

=item * List::Util(3pm)

=item * Text::LevenshteinXS(3pm)

=back

=head1 BUGS AND LIMITATIONS

There is no support for intelligent whitespaces (to also match "-" and similar)
yet.

=head1 SEE ALSO

Travel::Status::DE::IRIS(3pm).

=head1 AUTHOR

Copyright (C) 2014-2015 by Daniel Friesel E<lt>derf@finalrewind.orgE<gt>

=head1 LICENSE

This module is licensed under the same terms as Perl itself.

EOF