Skip to content

Instantly share code, notes, and snippets.

@pangyre
Created June 27, 2012 15:49
Show Gist options
  • Save pangyre/3004966 to your computer and use it in GitHub Desktop.
Save pangyre/3004966 to your computer and use it in GitHub Desktop.
For messing around with Unidecode and Soundex easily.
#!/usr/bin/env perl
use strict;
use Encode;
use Text::Unidecode;
use Plack::Request;
use Text::Soundex;
use HTML::Entities;
=head1 Synopsis
plackup xlit.psgi
HTTP::Server::PSGI: Accepting connections at http://0:5000/
L<Plack>, L<plackup>, et cetera.
=cut
my $template = do { local $/; <DATA> };
my $token = '\w+(?:[\x{2019}\']\w+)*'; # Already the default, explicit to avoid drift.
my $token_rx = qr{$token};
sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $text = encode_entities(decode("UTF-8",$req->parameters->{text} || ""), "><&");
my $transliterated = unidecode($text);
( my $page = $template ) =~ s/{TEXT}/$text/;
$page =~ s/{XLIT}/$transliterated/;
my $soundex = join(" ", soundex( $transliterated =~ /($token_rx)/g ) );
$page =~ s/{SOUNDEX}/$soundex/;
my $length = length($template);
[ 200, [ "Content-Type" => "text/html; charset=utf-8" ],
[ encode("UTF-8",$page) ] ];
}
__DATA__
<html><head>
<title>OHAI Unidecode/Soundex</title>
<style>* { font-family:helvetica neue, helvetica; font-size: small }</style>
</head><body style="height:80%;width:90%;padding:5%;margin:0;">
<h1>Xlit and Soundex</h1>
<form style="width:45%; float:left">
<textarea name="text" style="width:100%; height:25em">{TEXT}</textarea>
<input type="submit" />
</form>
<p style="width:45%; margin-left:3%; float:left;white-space:pre-wrap">
{XLIT}
</p>
<p style="width:45%; margin-left:3%; float:left;white-space:pre-wrap">
{SOUNDEX}
</p>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment