Text in search queries with examples in Perl 6

2,751 views
2,682 views

Published on

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

2. Perl 6 grammars.

3. Gearman.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,751
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Text in search queries with examples in Perl 6

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

×