File Coverage

blib/lib/Geo/Parser/Text.pm
Criterion Covered Total %
statement 29 77 37.6
branch 0 12 0.0
condition n/a
subroutine 10 18 55.5
pod 4 8 50.0
total 43 115 37.3


line stmt bran cond sub pod time code
1             package Geo::Parser::Text;
2              
3 1     1   13119 use 5.006;
  1         3  
4 1     1   3 use strict;
  1         1  
  1         19  
5 1     1   2 use warnings;
  1         4  
  1         20  
6              
7 1     1   702 use XML::Simple;
  1         6034  
  1         5  
8 1     1   649 use LWP::UserAgent;
  1         31262  
  1         27  
9 1     1   6 use HTTP::Request;
  1         1  
  1         18  
10 1     1   3 use URI;
  1         1  
  1         16  
11 1     1   530 use Data::Dumper;
  1         4772  
  1         58  
12              
13              
14 1     1   5 use constant DEBUG => 0;
  1         1  
  1         49  
15 1     1   4 use constant GEO_HOST => q{http://geocode.xyz};
  1         1  
  1         439  
16              
17             sub new {
18 0     0 1   my $class = shift;
19              
20 0           my $self = {
21             geo_host => shift
22             };
23              
24 0           bless ($self, $class);
25 0           return $self;
26             }
27              
28             sub geodata {
29 0     0 0   my $self = shift;
30 0 0         $self->{geodata} = $_[0] if $_[0];
31 0           return $self->{geodata};
32             }
33              
34             sub geoit {
35 0     0 0   my $self = shift;
36 0 0         $self->{geoit} = $_[0] if $_[0];
37 0           return $self->{geoit};
38             }
39              
40             sub scantext {
41 0     0 1   my $self = shift;
42 0 0         $self->{scantext} = $_[0] if $_[0];
43 0           return $self->{scantext};
44             }
45              
46             sub locate {
47 0     0 1   my $self = shift;
48 0 0         $self->{locate} = $_[0] if $_[0];
49 0           return $self->{locate};
50             }
51             sub geo_result {
52 0     0 0   my $self = shift;
53 0 0         $self->{geo_result} = $_[0] if $_[0];
54 0           return $self->{geo_result};
55             }
56              
57             sub geocode {
58 0     0 1   my $self = shift;
59              
60 0           my %form_values;
61 0           foreach my $param (qw(scantext locate)) {
62 0           $form_values{$param} = $self->$param;
63             }
64 0           $form_values{geoit} = 'XML';
65              
66 0           warn Data::Dumper->Dump([\%form_values],['Form Values']) if DEBUG;
67              
68 0           $self->process_request(%form_values);
69              
70 0           my $geodata = $self->geodata;
71 0           my @a = split(//,$geodata);
72 0           my $geod = pop @a;
73 0           $geod = '' . $geod;
74 0           my $ref = XMLin($geod);
75 0           warn Data::Dumper->Dump([$ref],['Response']) if DEBUG;
76 0           $self->geo_result($ref);
77              
78 0           return $self->geo_result;
79             }
80              
81             sub process_request {
82 0     0 0   my $self = shift;
83 0           my %form_values = @_;
84 0 0         $self->{geo_host} = GEO_HOST unless $self->{geo_host};
85              
86 0           my $uri = URI->new;
87 0           $uri->query_form(%form_values);
88 0           my $ua = LWP::UserAgent->new;
89 0           my $req = HTTP::Request->new(POST => $self->{geo_host});
90 0           $req->content_type('application/x-www-form-urlencoded');
91 0           $req->content($uri->query);
92              
93 0           my $res = $ua->request($req);
94 0           my $result = $res->as_string;
95              
96 0           warn $result if DEBUG;
97              
98 0           my $geodata = $result;
99              
100              
101 0           return $self->geodata($geodata);
102             }
103              
104              
105              
106             =head1 NAME
107              
108             Geo::Parser::Text - Perl extension for parsing and geocoding locations from free form text. (See Geocode.xyz or Geolytica.com or addrs.xyz for coverage details)
109              
110             =head1 VERSION
111              
112             Version 0.03
113              
114             =cut
115              
116             our $VERSION = '0.03';
117              
118              
119             =head1 SYNOPSIS
120              
121             use Geo::Parser::Text;
122            
123             # initialize with your host
124             my $g = Geo::Parser::Text->new([geo_host]);
125              
126             # Scan text for locations...
127             $g->scantext($text);
128            
129             # Geocode a single location...
130             $g->locate($string);
131             #
132             if ($g->geocode) {
133             my $hashref = $g->geocode;
134             }
135            
136             # Example...
137              
138             use Geo::Parser::Text;
139             use Data::Dumper;
140             my $g = Geo::Parser::Text->new('http://geocode.xyz');
141            
142             $g->scantext('The most important museums of Amsterdam are located on the Museumplein, located at the southwestern side of the Rijksmuseum.');
143             print Dumper $g->geocode;
144              
145             #or
146              
147             $g->locate('10 Downing St, London SW1A 2AA, United Kingdom');
148             print Dumper $g->geocode;
149              
150              
151            
152             =head1 DESCRIPTION
153              
154             This module provides a Perl frontend for the geocode.xyz API. It allows the programmer to extract locations containing street addresses, street intersections and city names along with their geocoded latitude,longitude from bodies of text such as microblogs or wikipedia entries. (It should work with any type of text, but dumping html text or paragraphs containing over 200 words, will slow down the response considerably. If you need faster parsing grab the geocode.xyz server image on the AWS, and run it on a faster server (it is currenly running on a micro instance and is shared as a free public API without any throttling or rate limiting in place.)
155             If you run your own instance, make sure to pass the instance ip address or domain name at invocation eg, Geo::Parser::Text->new($server). For North American locations use geolytica.com, and for Australia and New Zealand use addrs.xyz
156              
157             The api also geocodes single locations (returning the location matched with the highest probability from a string of text. If you pass a latitude,longitude pair, it will reverse geocode that point)
158              
159             For explanation on the API responses see http://geocode.xyz/api
160            
161             =head2 METHODS
162              
163             =head2 new ( host => 'geocode.xyz');
164              
165             Initialize with the default server. geocode.xyz for Europe, geocoder.ca for North America, addrs.xyz for Australia and New Zealand.
166              
167             =item scantext($text)
168              
169             Set the text to be scanned.
170              
171             =item locate($string)
172              
173             Set the string to be geocoded. ($string may contain an address, city name, post code, for forward geocoding; or latitude,longitude pair for reverse geocoding)
174              
175             =item geocode()
176              
177             Send the text/string to geocode.xyz and return the hash reference with the response.
178             You are required to set either the scantext or locate method before calling geocode().
179              
180            
181             =head1 EXPORT
182              
183             None by default.
184              
185              
186              
187             =head1 REQUIREMENTS
188              
189             XML::Simple,
190             LWP::UserAgent,
191             HTTP::Request,
192             URI
193              
194             =head1 AUTHOR
195              
196             Ervin Ruci, C<< >>
197              
198             =head1 BUGS
199              
200             Please report any bugs or feature requests to C, or through
201             the web interface at L. I will be notified, and then you'll
202             automatically be notified of progress on your bug as I make changes.
203              
204              
205              
206              
207             =head1 SUPPORT
208              
209             You can find documentation for this module with the perldoc command.
210              
211             perldoc Geo::Parser::Text
212              
213              
214             You can also look for information at:
215              
216             =over 4
217              
218             =item * RT: CPAN's request tracker (report bugs here)
219              
220             L
221              
222             =item * AnnoCPAN: Annotated CPAN documentation
223              
224             L
225              
226             =item * CPAN Ratings
227              
228             L
229              
230             =item * Search CPAN
231              
232             L
233              
234             =back
235              
236              
237             =head1 ACKNOWLEDGEMENTS
238              
239              
240             =head1 LICENSE AND COPYRIGHT
241              
242             Copyright 2016 Ervin Ruci.
243              
244             This program is free software; you can redistribute it and/or modify it
245             under the terms of the the Artistic License (2.0). You may obtain a
246             copy of the full license at:
247              
248             L
249              
250             Any use, modification, and distribution of the Standard or Modified
251             Versions is governed by this Artistic License. By using, modifying or
252             distributing the Package, you accept this license. Do not use, modify,
253             or distribute the Package, if you do not accept this license.
254              
255             If your Modified Version has been derived from a Modified Version made
256             by someone other than you, you are nevertheless required to ensure that
257             your Modified Version complies with the requirements of this license.
258              
259             This license does not grant you the right to use any trademark, service
260             mark, tradename, or logo of the Copyright Holder.
261              
262             This license includes the non-exclusive, worldwide, free-of-charge
263             patent license to make, have made, use, offer to sell, sell, import and
264             otherwise transfer the Package with respect to any patent claims
265             licensable by the Copyright Holder that are necessarily infringed by the
266             Package. If you institute patent litigation (including a cross-claim or
267             counterclaim) against any party alleging that the Package constitutes
268             direct or contributory patent infringement, then this Artistic License
269             to you shall terminate on the date that such litigation is filed.
270              
271             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
272             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
273             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
274             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
275             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
276             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
277             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
278             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
279              
280              
281             =head1 SEE ALSO
282              
283             Geo::Coder::Canada
284             Geo::Parse::OSM
285             Text::NLP
286             =cut
287              
288             1; # End of Geo::Parser::Text