rley 0.5.06 → 0.5.07

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 330046979feed860a18faeb226de2c2aab9ca8ca
4
- data.tar.gz: 3e8b7ee124edb3db95f6c95c56754a71375dd703
3
+ metadata.gz: 4ce368c99ffa556898d9a89788786e47b5e8b115
4
+ data.tar.gz: 97a691b869089989f601556ed13dd24d729b7ad0
5
5
  SHA512:
6
- metadata.gz: 518f9fb02230a1953c71ac4334cfa71d4c72c3f9e94b546a7dd164a32aa67d0ab2769563cd5cd72b46891cbce08fe9d4319f6645de7db8bd2ddb5d8fab67034a
7
- data.tar.gz: b468cda9c0d4c0e49537f0fc4cdac5882ea21ee176810c0d529fda1f080e13428f73a328607afff640f51ded1de8c83e4a5715da008d4295a276064d78490ca5
6
+ metadata.gz: b50595273bf5f75e25b6e609de197be31a4f8583f1c1f93ef8494a8e5367facdbafd13326bd2097c9da657096c2f15e9dc8d439d16212f4e578134ce538984b2
7
+ data.tar.gz: 8be6b6bdef48de1cbd19b2785530a5bc17e43804a43ff01f287a89a129a83897f8ea05ecb951018c99f888bbfefbbc33e89ee9cfb0adb2e86eba2e3971703c1d
@@ -2,9 +2,9 @@ language: ruby
2
2
  rvm:
3
3
  - 2.0.0-p648
4
4
  - 2.1.10
5
- - 2.2.7
6
- - 2.3.4
7
- - 2.4.1
5
+ - 2.2.8
6
+ - 2.3.5
7
+ - 2.4.2
8
8
  - ruby-head
9
9
  - jruby-9.1.9.0
10
10
  - jruby-head
@@ -1,3 +1,8 @@
1
+ ### 0.5.07 / 2017-11-11
2
+ * [NEW] File `benchmark_mini_en.rb` added in `examples/NLP` folder for parsing performance measurements.
3
+ * [CHANGE] Demo calculator in `examples/general/calc_iter2`: added support for log10 and cbrt functions. README.md slightly reworked.
4
+ * [FIX] File `README.md` sample code was broken by changes in 0.5.06 (thanks to arjunmenon for pointing this).
5
+
1
6
  ### 0.5.06 / 2017-11-08
2
7
  * [CHANGE] Demo calculator in `examples/general/calc_iter2` does much than the basic arithmetic operators, it now
3
8
  support trigonometric functions and their inverse, square root, exponential and natural logarithm functions!.
data/README.md CHANGED
@@ -148,7 +148,7 @@ The subset of English grammar is based on an example from the NLTK book.
148
148
  term_name = Lexicon[word]
149
149
  raise StandardError, "Word '#{word}' not found in lexicon" if term_name.nil?
150
150
  terminal = aGrammar.name2symbol[term_name]
151
- Rley::Tokens::Token.new(word, terminal)
151
+ Rley::Lexical::Token.new(word, terminal)
152
152
  end
153
153
 
154
154
  return tokens
@@ -0,0 +1,92 @@
1
+ # File: benchmark_mini_en.rb
2
+ # Purpose: benchmark the parse speed
3
+ require 'benchmark'
4
+ require 'rley' # Load Rley library
5
+
6
+ # Instantiate a builder object that will build the grammar for us
7
+ builder = Rley::Syntax::GrammarBuilder.new do
8
+
9
+ add_terminals('Noun', 'Proper-Noun', 'Verb')
10
+ add_terminals('Determiner', 'Preposition')
11
+
12
+ # Here we define the productions (= grammar rules)
13
+ rule 'S' => %w[NP VP]
14
+ rule 'NP' => 'Proper-Noun'
15
+ rule 'NP' => %w[Determiner Noun]
16
+ rule 'NP' => %w[Determiner Noun PP]
17
+ rule 'VP' => %w[Verb NP]
18
+ rule 'VP' => %w[Verb NP PP]
19
+ rule 'PP' => %w[Preposition NP]
20
+ end
21
+
22
+ # And now, let's build the grammar...
23
+ grammar = builder.grammar
24
+
25
+ ########################################
26
+ # Step 2. Creating a lexicon
27
+ # To simplify things, lexicon is implemented as a Hash with pairs of the form:
28
+ # word => terminal symbol name
29
+ Lexicon = {
30
+ 'man' => 'Noun',
31
+ 'dog' => 'Noun',
32
+ 'cat' => 'Noun',
33
+ 'telescope' => 'Noun',
34
+ 'park' => 'Noun',
35
+ 'saw' => 'Verb',
36
+ 'ate' => 'Verb',
37
+ 'walked' => 'Verb',
38
+ 'John' => 'Proper-Noun',
39
+ 'Mary' => 'Proper-Noun',
40
+ 'Bob' => 'Proper-Noun',
41
+ 'a' => 'Determiner',
42
+ 'an' => 'Determiner',
43
+ 'the' => 'Determiner',
44
+ 'my' => 'Determiner',
45
+ 'in' => 'Preposition',
46
+ 'on' => 'Preposition',
47
+ 'by' => 'Preposition',
48
+ 'with' => 'Preposition'
49
+ }.freeze
50
+
51
+ ########################################
52
+ # Step 3. Creating a tokenizer
53
+ # A tokenizer reads the input string and converts it into a sequence of tokens
54
+ # Highly simplified tokenizer implementation.
55
+ def tokenizer(aTextToParse, aGrammar)
56
+ tokens = aTextToParse.scan(/\S+/).map do |word|
57
+ term_name = Lexicon[word]
58
+ raise StandardError, "Word '#{word}' not found in lexicon" if term_name.nil?
59
+ terminal = aGrammar.name2symbol[term_name]
60
+ Rley::Lexical::Token.new(word, terminal)
61
+ end
62
+
63
+ return tokens
64
+ end
65
+
66
+ ########################################
67
+ # Step 4. Create a parser for that grammar
68
+ # Easy with Rley...
69
+ parser = Rley::Parser::GFGEarleyParser.new(grammar)
70
+
71
+ ########################################
72
+ # Step 5. Parsing the input
73
+ input_to_parse = 'John saw Mary with a telescope'
74
+
75
+ # Convert input text into a sequence of token objects...
76
+ tokens = tokenizer(input_to_parse, grammar)
77
+
78
+ # Use Benchmark mix-in
79
+ include Benchmark
80
+
81
+ bm(6) do |meter|
82
+ meter.report("Parse 100 times") { 100.times { parser.parse(tokens) } }
83
+ meter.report("Parse 1000 times") { 1000.times { parser.parse(tokens) } }
84
+ meter.report("Parse 10000 times") { 10000.times { parser.parse(tokens) } }
85
+ meter.report("Parse 1000000 times") { 100000.times { parser.parse(tokens) } }
86
+ end
87
+
88
+ # puts "Parsing successful? #{result.success?}"
89
+ # unless result.success?
90
+ # puts result.failure_reason.message
91
+ # exit(1)
92
+ # end
@@ -97,11 +97,18 @@ class CalcNegateNode < CalcUnaryOpNode
97
97
  end # class
98
98
 
99
99
  class CalcUnaryFunction < CalcCompositeNode
100
+ @@name_mapping = begin
101
+ map = Hash.new { |me, key| me[key] = key }
102
+ map['ln'] = 'log'
103
+ map['log'] = 'log10'
104
+ map
105
+ end
100
106
  attr_accessor(:func_name)
101
107
 
108
+
102
109
  def interpret()
103
110
  argument = children[0].interpret
104
- internal_name = @func_name == 'ln' ? 'log' : @func_name
111
+ internal_name = @@name_mapping[@func_name] # @func_name == 'ln' ? 'log' : @func_name
105
112
  return Math.send(internal_name.to_sym, argument)
106
113
  end
107
114
  end
@@ -24,7 +24,8 @@ class CalcLexer
24
24
  @@unary_functions = [
25
25
  'sin', 'cos', 'tan',
26
26
  'asin', 'acos', 'atan',
27
- 'sqrt', 'exp', 'ln'
27
+ 'sqrt', 'cbrt', 'exp',
28
+ 'ln', 'log'
28
29
  ].freeze
29
30
 
30
31
  class ScanError < StandardError; end
@@ -168,8 +168,18 @@ describe 'Calculator' do
168
168
 
169
169
  # Some special functions
170
170
  it 'should evaluate square root of expressions' do
171
+ expect_expr('sqrt(0)').to eq(0)
172
+ expect_expr('sqrt(1)').to eq(1)
171
173
  expect_expr('sqrt(1 + 1)').to eq(Math.sqrt(2))
172
- end
174
+ expect_expr('sqrt(5 * 5)').to eq(5)
175
+ end
176
+
177
+ it 'should evaluate cubic root of expressions' do
178
+ expect_expr('cbrt(0)').to eq(0)
179
+ expect_expr('cbrt(1)').to eq(1)
180
+ expect_expr('cbrt(1 + 1)').to eq(Math.cbrt(2))
181
+ expect_expr('cbrt(5 * 5 * 5)').to eq(5)
182
+ end
173
183
 
174
184
  it 'should evaluate exponential of expressions' do
175
185
  expect_expr('exp(-1)').to eq(1/Math::E)
@@ -183,7 +193,14 @@ describe 'Calculator' do
183
193
  expect_expr('ln(1)').to eq(0)
184
194
  expect_expr('ln(E)').to eq(1)
185
195
  expect_expr('ln(E * E)').to eq(2)
186
- end
196
+ end
197
+
198
+ it 'should evaluate the logarithm base 10 of expressions' do
199
+ expect_expr('log(1/10)').to eq(-1)
200
+ expect_expr('log(1)').to eq(0)
201
+ expect_expr('log(10)').to eq(1)
202
+ expect_expr('log(10 * 10 * 10)').to eq(3)
203
+ end
187
204
 
188
205
  # Trigonometric functions
189
206
 
@@ -3,7 +3,7 @@
3
3
 
4
4
  module Rley # Module used as a namespace
5
5
  # The version number of the gem.
6
- Version = '0.5.06'.freeze
6
+ Version = '0.5.07'.freeze
7
7
 
8
8
  # Brief description of the gem.
9
9
  Description = "Ruby implementation of the Earley's parsing algorithm".freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rley
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.06
4
+ version: 0.5.07
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-08 00:00:00.000000000 Z
11
+ date: 2017-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coveralls
@@ -130,6 +130,7 @@ files:
130
130
  - README.md
131
131
  - Rakefile
132
132
  - appveyor.yml
133
+ - examples/NLP/benchmark_mini_en.rb
133
134
  - examples/NLP/mini_en_demo.rb
134
135
  - examples/data_formats/JSON/cli_options.rb
135
136
  - examples/data_formats/JSON/json_ast_builder.rb