Class: Linguify::Linguified

Inherits:
Object
  • Object
show all
Defined in:
lib/linguify/linguified.rb,
lib/linguify/translators/javascript.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, bind) ⇒ Linguified

Lingquify a sentence

Parameters:

  • string (String)

    A plain English string, or a plain English string with reductions in it.

  • binding (Binding)

    See Kernel#eval



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/linguify/linguified.rb', line 38

def initialize str,bind

  #
  # reduction loop
  #
  @sentence = str.dup
  @bind = bind
  @stack = [ str ]
  loop do
    rule = find_rule(str)

    reduction = rule[:proc].to_reduction :returns  => rule[:result],
                                         :lang     => rule[:lang],
                                         :inline   => rule[:inline],
                                         :regexp   => rule[:match].inspect,
                                         :args     => rule[:match].match(str).to_a

    str = Linguified.reduce_string(str,rule[:match],reduction.to_rexp)
    @stack << str

    break if /^{:[0-9]*}$/ =~ str
  end

  @merged_code = []

  #
  # successfully reduced entire string, compile it
  #

  code = Reduction::parse(str).compile

  Sexp.inline_keyword_inlined!(code)

  #if @dispatch_exceptions
  #  @sexy = Sexp.debug_envelope(code)
  #else
    @sexy = Sexp.lambda_envelope(code)
  #end

  # let ruby compile a proc out of the actual code
  # the trampoline function will receive the compiled Proc
  @@me = self
  eval to_ruby(
      Sexp.new(:call,
        Sexp.new(:colon2, Sexp.new(:const, :Linguify), :Linguified), :trampoline, Sexp.new(:arglist, Sexp.new(:lvar, :code))
      )
    ),bind
end

Instance Attribute Details

#procObject

Returns the value of attribute proc.



31
32
33
# File 'lib/linguify/linguified.rb', line 31

def proc
  @proc
end

#sentenceObject

Returns the value of attribute sentence.



31
32
33
# File 'lib/linguify/linguified.rb', line 31

def sentence
  @sentence
end

Class Method Details

.cacheObject



214
215
216
# File 'lib/linguify/linguified.rb', line 214

def self.cache
  @@cache ||= {}
end

.has_needle_on_word_boundary?(splitted) ⇒ Boolean

Test if a informative split contains needles on word boundaries

Parameters:

  • the (Array)

    splitted string

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/linguify/linguified.rb', line 147

def self.has_needle_on_word_boundary? splitted
  return true if splitted.size == 1 and splitted.first == :needle

  prev_ends_with_space = true
  following_needle     = true

  splitted.each_with_index do |split,i|
    if split.kind_of? String
      return true if following_needle and prev_ends_with_space and split[0] == ' '

      prev_ends_with_space = (split[-1] == ' ')
      following_needle = false
    else # :needle
      following_needle = true
    end
  end
  following_needle && prev_ends_with_space
end

.informative_split(str, needle) ⇒ Object

Split a string by given search needle into an array with split indicators

Parameters:

  • the (String)

    string to search (haystack)

  • needle (String)

    (search needle)



127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/linguify/linguified.rb', line 127

def self.informative_split str,needle
  splitted = str.split(needle)
  if str.index(needle) > 0
    splitted.map{ |m| [m,:needle] }.flatten[0..splitted.size+str.scan(needle).size-1]
  else
    if splitted.size > 0
      splitted.map{ |m| [m,:needle] }.flatten[1..-2]
    elsif str == needle
      [ :needle ]
    else
      []
    end
  end
end

.reduce_string(str, match_expression, reduction) ⇒ Object

Reduce a string with a matching reduction expression

Parameters:

  • the (String)

    sentence to reduce (haystack)

  • the (Regexp)

    reduction expression (search needle)

  • the (String)

    replacement



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/linguify/linguified.rb', line 94

def self.reduce_string str,match_expression,reduction
  match = match_expression.match(str).to_a
  needle = match.first
  splitted = Linguified.informative_split(str,needle)
  if match.size == 1
    prev = ' '
    i = -1
    splitted.map! do |split|
      prev = splitted[i] if i>=0
      nxt  = splitted[i+2] || ' '
      i += 1
      if split.kind_of?(Symbol)
        if prev[-1] == ' ' and nxt[0] == ' '
          reduction
        else
          needle
        end
      else
        split
      end
    end
    splitted.join
  else
    splitted.map{ |split| split.kind_of?(Symbol) ? reduction : split }.join
  end
end

.trampoline(code) ⇒ Object



210
211
212
# File 'lib/linguify/linguified.rb', line 210

def self.trampoline code
  @@me.register_code code
end

Instance Method Details

#dentObject



530
531
532
533
# File 'lib/linguify/translators/javascript.rb', line 530

def dent
  @indenture ||= ''
  @indenture = @indenture[2..-1]
end

#find_rule(str) ⇒ Object

Find a reduction rule for the string

Parameters:

  • string (String)

    A plain English string, or a plain English string with reductions in it.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/linguify/linguified.rb', line 170

def find_rule str
  found = Linguify.rules.select do |rule|
    if rule[:match] =~ str
      # ok, it matched, but only alow matches with word boundaries
      match = rule[:match].match(str).to_a
      needle = match.first
      Linguified.has_needle_on_word_boundary? Linguified.informative_split(str,needle)
    else
      false
    end
  end
  if found.size == 0
    pp @stack
    raise "no step definition for #{str}"
  end
  found[0]
end

#indentObject



516
517
518
519
# File 'lib/linguify/translators/javascript.rb', line 516

def indent
  @indenture ||= ''
  @indenture += '  '
end

#indentureObject



520
521
522
523
# File 'lib/linguify/translators/javascript.rb', line 520

def indenture
  @indenture ||= ''
  @indenture
end

#indenture=(str) ⇒ Object



524
525
526
# File 'lib/linguify/translators/javascript.rb', line 524

def indenture= str
  @indenture = str
end

#new_lineObject



527
528
529
# File 'lib/linguify/translators/javascript.rb', line 527

def new_line
  "\n" + indenture
end

#register_code(code) ⇒ Object



206
207
208
# File 'lib/linguify/linguified.rb', line 206

def register_code code
  @proc = code
end

#runObject



198
199
200
201
202
203
204
# File 'lib/linguify/linguified.rb', line 198

def run
  begin
    @proc.call
  rescue Exception => e
    $stderr.puts e
  end
end

#to_js(sexy = @sexy) ⇒ Object



535
536
537
# File 'lib/linguify/translators/javascript.rb', line 535

def to_js sexy = @sexy
  Ruby2Js.new.process(sexy)
end

#to_ruby(additional = nil) ⇒ Object



192
193
194
195
196
# File 'lib/linguify/linguified.rb', line 192

def to_ruby additional=nil
  clone = Marshal.load(Marshal.dump(@sexy)) # sexy is not cleanly duplicated
  clone << additional if additional
  Ruby2Ruby.new.process(clone)
end

#to_sexpObject



188
189
190
# File 'lib/linguify/linguified.rb', line 188

def to_sexp
  @sexy
end