-
Notifications
You must be signed in to change notification settings - Fork 1
/
furigana.rb
executable file
·80 lines (72 loc) · 1.76 KB
/
furigana.rb
1
2
3
4
5
6
7
8
9
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
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env ruby
#-*- coding: utf-8 -*-
$KCODE = "UTF-8"
require "rubygems"
require "net/http"
require "nokogiri"
require "cgi"
require "json"
class Furigana
attr_writer :sentence, :grade
def initialize
@app_id = "UiepUEKxg666SAyFxsBpxR_VE9A_qsPVG_yyUJ8R8RIBRhRt8nJ2buvmiEBiuP6sQoOQ6ks.DuPQozY-"
end
def get_furigana
begin
Net::HTTP.start('jlp.yahooapis.jp', 80) { |http|
response = http.post(
"/FuriganaService/V1/furigana",
"appid=#{@app_id}&grade=#{@grade}&sentence=#{CGI.escape(@sentence)}"
)
@response = response
}
rescue => ex
@response = "HTTP Error #{ex}"
end
return @response unless @response.nil?
end
def return_character_pair
xml = get_furigana.body
doc_xml = Nokogiri::XML(xml)
result = []
words = doc_xml.css("WordList Word")
words.each do |word|
if word.css("Furigana").text == ""
phrase = word.css("Surface").first.text
else
phrase = word.css("Surface").first.text, "(", word.css("Furigana").first.text, ")"
end
result << phrase
end
result.join('')
end
def remove_kanakana
ary = return_character_pair.split("\n")
result = []
ary.each do |str|
m_ary = str.scan %r|[\((](.+?)[\))]|
u_ary = m_ary.uniq.flatten
i = 0
while i < u_ary.length
str.sub!(/([\((]#{u_ary[i]}[\))]){2}/, "\\1")
i += 1
end
result << str
end
result.join("\n")
end
end
cgi = CGI.new
str = cgi.params['val'][0]
str = <<EOD unless cgi.params['val'][0]
データを入力してください
EOD
y = Furigana.new
y.grade = 3
y.sentence = str
cgi.out("application/json") {
{
:text_output => y.remove_kanakana,
:ruby_version => RUBY_VERSION
}.to_json
}