Text in search queries with examples in Perl 6

Post on 15-Jan-2015

2.440 views 2 download

Tags:

description

1. Using Perl 5.10 for parsing search queries like "99 EUR in LVL". 2. Perl 6 grammars. 3. Gearman.

Transcript of Text in search queries with examples in Perl 6

Using textin search queries

with examplesin Perl 6

It is aboutunderstanding

human language

It is aboutunderstanding

human languageat a limited scale

whoyougle.com

Perland

Wolfram|Alpha

wolframalpha.com

Search queries

Italy

Search queries

Pisa

Search queries

10 miles in km

Search queries

exchange rate of USD

Search queries

rate of PHP

Search queries

100 USD in EUR

Search queries

100 £ in €

Scanners (parsers)

Scanners (parsers)

100 £ in €Area.pm

Calendar.pm

Energy.pm

Force.pm

Length.pm

Money.pm

Mass.pm

Asking in parallel

100 £ in €Area.pm

Calendar.pm

Energy.pm

Force.pm

Length.pm

Money.pm

Mass.pm

120.71

Perl 5.10is great for parsing

Perl 6is even more

Named captures in 5.10’s regexes

Grammarsin Perl 6

Examples in Perl 5.10 (5.12)

my $sentence = qr/ ^ (?: $Infolavka::Search::Grammar::how_many \s (?<to> $currency ) \s $Infolavka::Search::Grammar::convert \s (?: (?<amount> $Infolavka::Search::Grammar::number ) \s )? (?<from> $currency ) ) | (?: (?: (?<amount> $Infolavka::Search::Grammar::number ) \s )? (?<from> $currency ) \s $Infolavka::Search::Grammar::equal ) | (?: (?: $rate \s )? (?: (?<amount> $Infolavka::Search::Grammar::number ) \s? )? (?<from> $currency ) (?: \s (?: $Infolavka::Search::Grammar::convert \s )? (?<to> $currency ) )? ) $/xop;

(?<amount> $Search::Grammar::number ) \s? )?(?<from> $currency)(?: \s (?: $Search::Grammar::convert \s )? (?<to> $currency ))?

(?<amount> $Search::Grammar::number ) \s? )?(?<from> $currency)(?: \s (?: $Search::Grammar::convert \s )? (?<to> $currency ))?

(?<amount> $Search::Grammar::number ) \s? )?(?<from> $currency)(?: \s (?: $Search::Grammar::convert \s )? (?<to> $currency ))?

100 £ in €

(?<amount> $Search::Grammar::number ) \s? )?(?<from> $currency)(?: \s (?: $Search::Grammar::convert \s )? (?<to> $currency ))?

£

100

€in

Examples in Perl 6

grammar TestGrammar { rule TOP { ^ <sign>? <digit>+ $ } token sign { '-' | '+' }

token digit { <[0..9]> }}

grammar TestGrammar { rule TOP { ^ <sign>? <digit>+ $ } token sign { '-' | '+' }

token digit { <[0..9]> }}

grammar TestGrammar { rule TOP { ^ <sign>? <digit>+ $ } token sign { '-' | '+' }

token digit { <[0..9]> }}

while my $string = prompt('> ') { if TestGrammar.parse($string) { say "OK"; } else { say "Failed"; }}

grammar CurrencyGrammar { rule TOP { ^ <rate_question> $ } rule rate_question { 'rate of' <currency_code> } token currency_code { <[A..Z]> ** 3 }}

my $result = CurrencyGrammar.parse($string); if $result { say "OK"; say $<rate_question><currency_code>; }

grammar CurrencyGrammar { rule TOP { ^ <rate_question> $ } rule rate_question { 'rate of'? <currency_code> [<ws> 'to' <ws> <currency_code>]? } token currency_code { <[A..Z]> ** 3 }}

my $result = CurrencyGrammar.parse($string); if $result { say "OK"; my ($from, $to) = $<rate_question><currency_code>; say "$from -> $to"; }

grammar CurrencyGrammar { rule TOP { ^ <rate_question> $ } rule rate_question { 'rate of'? <ws> <money> [<ws> 'to' <currency_code>]? } rule money { [<value> <ws>]? $<code>=(<currency_code>) } token value { <[0..9]>+ } token currency_code { <[A..Z]> ** 3 }}

say "OK"; my $value = $<rate_question><money><value> || 1; my $from = $<rate_question><money><code>; my $to = $<rate_question><currency_code> || 'EUR'; say "$value, $from -> $to";

rule money { [<value> <ws>]? $<code>=(<currency_code>) }

rule money { [<value> <ws>]? <code=currency_code> }

my %rate = EUR => 1, USD => 0.7564, LVL => 1.4116, RUB => 0.02539, PHP => 0.01676, UAH => 0.09587;

my $codes = %rate.keys.join('|');say $codes;

my $value = ~$<rate_question><money><value> || 1;my $from = $<rate_question><money><code>;my $to = $<rate_question><currency_code> || 'EUR';

my $ratio = 0 + %rate{~$from} / %rate{~$to}; say $ratio * $value;

my $value = ~$<rate_question><money><value> || 1;my $from = $<rate_question><money><code>;my $to = $<rate_question><currency_code> || 'EUR';

my $ratio = 0 + %rate{~$from} / %rate{~$to}; say $ratio * $value;

:-/

HTTP::Daemon

Rakudo Star

comes with

Dispatching jobs with Gearman

gearman.org

Originally written in Perl

Rewritten in C

Clients in Perl, PHP, Phython,

Java, C# (.NET)

Clients in Perl, PHP, Phython,

Java, C# (.NET),even MySQL and PostgreSQL

Application

Job

Application

Job (or task)

Application

Job

gearmand

Application

Job

gearmand

Worker

Job

Application

Job

gearmand

Worker

Job Response

Application

gearmand

Worker

Worker

Worker

Application

gearmand

Worker

Worker

gearmand

Application

gearmand

Worker

Worker

gearmand

Application

Workers are scalable

Run any number you need

Run on remote servers

Application only talks with gearmand server

Application only talks with gearmand server

(one or more)

One or more application

One or more application

Applications throw jobs

One or more job servers

One or more job servers

Job servers dispatch jobs

One or more workers

One or more workers(clones or different)

One or more workers

Workers do jobs

One or more workers

Workers do jobsand may issue new jobs

Scalable also means redundant

Really need Gearman?

Really need Gearman?

No!

Use HTTP servers instead

__END__

Andrew Shitov

talks.shitov.ru | andy@shitov.ru