Class: RGinger::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/rginger/parser.rb

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



7
8
# File 'lib/rginger/parser.rb', line 7

def initialize
end

Instance Method Details

#correct(input) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
# File 'lib/rginger/parser.rb', line 10

def correct(input)

  text = input.dup
  results = {"original" => input, "data" => []}

  if !text || text == ""
    results["corrected" => text]
    return results 
  end

  width = text.length
  escaped = URI.escape(text)
  url = "#{API_ENDPOINT}?lang=#{DEFAULT_LANG}&clientVersion=#{API_VERSION}&apiKey=#{API_KEY}&text=#{escaped}"

  json_data = get_json(url)["LightGingerTheTextResult"] rescue {}

  unless json_data.empty?
    new_string = text
    json_data.each do |r|
      if r["Mistakes"].size == r["Suggestions"].size
        r["Mistakes"].each_with_index do |m, i|
          from         = m["From"]
          to           = m["To"]
          reverse_from = -(width - from)
          reverse_to   = -(width - to)
          old_item = new_string[reverse_from .. reverse_to]
          new_item = r["Suggestions"][i]["Text"]
          new_string[reverse_from .. reverse_to] = new_item
          results["data"] << {
            "old"          => old_item, 
            "from"         => from, 
            "to"           => to, 
            "reverse_from" => reverse_from, 
            "reverse_to"   => reverse_to,
            "new"          => new_item,
          }
        end    
      else
        from         = r["From"]
        to           = r["To"]
        reverse_from = -(width - from)
        reverse_to   = -(width - to)
        old_item = new_string[reverse_from .. reverse_to]
        new_item = r["Suggestions"][0]["Text"]
        new_string[reverse_from .. reverse_to] = new_item rescue ""
        results["data"] << {
          "old"          => old_item, 
          "from"         => from, 
          "to"           => to, 
          "reverse_from" => reverse_from, 
          "reverse_to"   => reverse_to,
          "new"          => new_item,
        }
      end
      results["corrected"] = new_string
    end
  end
  results
end

#get_json(location, limit = 10) ⇒ Object

Raises:

  • (ArgumentError)


90
91
92
93
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
# File 'lib/rginger/parser.rb', line 90

def get_json(location, limit = 10)
  raise ArgumentError, 'too many HTTP redirects' if limit == 0
  uri = URI.parse(location)

  begin
    response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
      http.open_timeout = 5
      http.read_timeout = 10
      http.get(uri.request_uri)
    end
  
    case response
    when Net::HTTPSuccess
      json = response.body
      JSON.parse(json)
    when Net::HTTPRedirection
      location = response['location']
      warn "redirected to #{location}"
      # Recursive method call
      get_json(location, limit - 1)
    else
      # Error
      throw [uri.to_s, response.value].join(" : ")
    end
  rescue => e
    # Error
    throw [uri.to_s, e.class, e].join(" : ")
  end
end

#rephrase(input) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rginger/parser.rb', line 70

def rephrase(input)

  text = input.dup
  results = {"original" => input, "alternatives" => []}
  return results if !text || text == ""

  width = text.length
  escaped = URI.escape(text)
  url = "#{REPHRASE_ENDPOINT}?s=#{escaped}"

  json_data = get_json(url)

  unless json_data.empty? || json_data["Sentences"].empty?
    results["alternatives"] = json_data["Sentences"].map{|s| s["Sentence"]}
  end    
  results
end