Text in search queries with examples in Perl 6

  • 2,417 views
Uploaded on

1. Using Perl 5.10 for parsing search queries like "99 EUR in LVL". …

1. Using Perl 5.10 for parsing search queries like "99 EUR in LVL".

2. Perl 6 grammars.

3. Gearman.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,417
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
12
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Using text in search queries with examples in Perl 6
  • 2. It is about understanding human language
  • 3. It is about understanding human language at a limited scale
  • 4. whoyougle.com
  • 5. Perl and Wolfram|Alpha
  • 6. wolframalpha.com
  • 7. Search queries Italy
  • 8. Search queries Pisa
  • 9. Search queries 10 miles in km
  • 10. Search queries exchange rate of USD
  • 11. Search queries rate of PHP
  • 12. Search queries 100 USD in EUR
  • 13. Search queries 100 £ in €
  • 14. Scanners (parsers)
  • 15. Scanners (parsers)
  • 16. 100 £ in € Area.pm Length.pm Calendar.pm Money.pm Energy.pm Mass.pm Force.pm Asking in parallel
  • 17. 100 £ in € Area.pm Length.pm Calendar.pm Money.pm Energy.pm Mass.pm Force.pm 120.71
  • 18. Perl 5.10 is great for parsing
  • 19. Perl 6 is even more
  • 20. Named captures in 5.10’s regexes
  • 21. Grammars in Perl 6
  • 22. Examples in Perl 5.10 (5.12)
  • 23. 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;
  • 24. (?<amount> $Search::Grammar::number ) s? )? (?<from> $currency) (?: s (?: $Search::Grammar::convert s )? (?<to> $currency ) )?
  • 25. (?<amount> $Search::Grammar::number ) s? )? (?<from> $currency) (?: s (?: $Search::Grammar::convert s )? (?<to> $currency ) )?
  • 26. (?<amount> $Search::Grammar::number ) s? )? (?<from> $currency) (?: s 100 £ in € (?: $Search::Grammar::convert s )? (?<to> $currency ) )?
  • 27. (?<amount> 100 $Search::Grammar::number ) s? )? (?<from> $currency) £ (?: s (?: $Search::Grammar::convert in s )? )? (?<to> $currency ) €
  • 28. Examples in Perl 6
  • 29. grammar TestGrammar { rule TOP { ^ <sign>? <digit>+ $ } token sign { '-' | '+' } token digit { <[0..9]> } }
  • 30. grammar TestGrammar { rule TOP { ^ <sign>? <digit>+ $ } token sign { '-' | '+' } token digit { <[0..9]> } }
  • 31. grammar TestGrammar { rule TOP { ^ <sign>? <digit>+ $ } token sign { '-' | '+' } token digit { <[0..9]> } }
  • 32. while my $string = prompt('> ') { if TestGrammar.parse($string) { say "OK"; } else { say "Failed"; } }
  • 33. grammar CurrencyGrammar { rule TOP { ^ <rate_question> $ } rule rate_question { 'rate of' <currency_code> } token currency_code { <[A..Z]> ** 3 } }
  • 34. my $result = CurrencyGrammar.parse($string); if $result { say "OK"; say $<rate_question><currency_code>; }
  • 35. grammar CurrencyGrammar { rule TOP { ^ <rate_question> $ } rule rate_question { 'rate of'? <currency_code> [<ws> 'to' <ws> <currency_code>]? } token currency_code { <[A..Z]> ** 3 } }
  • 36. my $result = CurrencyGrammar.parse($string); if $result { say "OK"; my ($from, $to) = $<rate_question><currency_code>; say "$from -> $to"; }
  • 37. 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 } }
  • 38. 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";
  • 39. rule money { [<value> <ws>]? $<code>=(<currency_code>) }
  • 40. rule money { [<value> <ws>]? <code=currency_code> }
  • 41. 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;
  • 42. 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;
  • 43. 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; :-/
  • 44. Rakudo Star comes with HTTP::Daemon
  • 45. Dispatching jobs with Gearman
  • 46. gearman.org
  • 47. Originally written in Perl
  • 48. Rewritten in C
  • 49. Clients in Perl, PHP, Phython, Java, C# (.NET)
  • 50. Clients in Perl, PHP, Phython, Java, C# (.NET), even MySQL and PostgreSQL
  • 51. Application Job
  • 52. Application Job (or task)
  • 53. Application Job gearmand
  • 54. Application Job gearmand Job Worker
  • 55. Application Job gearmand Job Response Worker
  • 56. Application gearmand Worker Worker Worker
  • 57. Application gearmand gearmand Worker Worker
  • 58. Application Application gearmand gearmand Worker Worker
  • 59. Workers are scalable
  • 60. Run any number you need
  • 61. Run on remote servers
  • 62. Application only talks with gearmand server
  • 63. Application only talks with gearmand server (one or more)
  • 64. One or more application
  • 65. One or more application Applications throw jobs
  • 66. One or more job servers
  • 67. One or more job servers Job servers dispatch jobs
  • 68. One or more workers
  • 69. One or more workers (clones or different)
  • 70. One or more workers Workers do jobs
  • 71. One or more workers Workers do jobs and may issue new jobs
  • 72. Scalable also means redundant
  • 73. Really need Gearman?
  • 74. Really need Gearman? No!
  • 75. Use HTTP servers instead
  • 76. __END__ Andrew Shitov talks.shitov.ru | andy@shitov.ru