Created
December 2, 2011 10:44
-
-
Save syonbori/1422768 to your computer and use it in GitHub Desktop.
use Yahoo! Translation via STDIN and utilize from Emacs for editing LaTeX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
lines = STDIN.readlines | |
content = "" | |
lines.each{|l| content << l.chomp << " "} | |
withdots = %w{ | |
et al | |
} | |
# パーセント | |
content.gsub!(/\\%/, "%") | |
# コメントアウトの無効化 | |
content.gsub!(/%+/, "") | |
# ダッシュ | |
content.gsub!(/--/, "-") | |
# \hoge{...}を無効化 | |
content.gsub!(/\\[a-zA-Z]+\{[^}]+\}/, "") | |
# \item | |
content.gsub!("\\item", "") | |
# {\bf ...}系 | |
content.gsub!(/\{\\[a-zA-Z]+ ([^}]+)\}/, "\\1") | |
# 改行 | |
content.gsub!(".", ".\n") | |
# 行末以外の.を復元 | |
withdots.each do |ptn| | |
content.gsub!("#{ptn}.\n", "#{ptn}.") | |
end | |
puts content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env ruby | |
require 'rubygems' | |
require 'pp' | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
INIT_URL="http://honyaku.yahoo.co.jp/transtext/" | |
BASE_URL="http://honyaku.yahoo.co.jp/TranslationText" | |
def trans(type, str, http_obj = Net::HTTP) | |
ieid = oeid = nil | |
case type | |
when "en2ja" | |
ieid = "en"; oeid = "ja" | |
when "ja2en" | |
ieid = "ja"; oeid = "en" | |
else | |
raise "please specify the type" | |
end | |
# get crumb | |
crumb = nil | |
uri = URI.parse(INIT_URL) | |
http_obj.start(uri.host, uri.port) do |http| | |
request = Net::HTTP::Get.new(uri.path) | |
request["user-agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET" | |
res = http.request(request) | |
if res.body != nil and res.body.empty? == false | |
res.body.each_line do |line| | |
next if line.index('id="TTcrumb"') == nil | |
# <input type="hidden" id="TTcrumb" name="TTcrumb" value="..."/> | |
_ , crumb1 = line.chomp.split('value="') | |
crumb, _ = crumb1.split('"') | |
break | |
end | |
end | |
end | |
# call trans api | |
uri = URI.parse(BASE_URL) | |
http_obj.start(uri.host, uri.port) do |http| | |
request = Net::HTTP::Post.new(uri.path) | |
request["user-agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET" | |
request["referer"] = "http://honyaku.yahoo.co.jp/transtext/" | |
request["x-requested-with"] = "XMLHttpRequest" | |
request["Accept-Language"] = "ja" | |
request["Accept"] = "application/json, text/javascript, */*" | |
body = { | |
:ieid => ieid, | |
:oeid => oeid, | |
:results => 1000, | |
:formality => 0, | |
:output => "json", | |
:p => str, | |
:_crumb => crumb, | |
} | |
request.set_form_data(body) | |
res = http.request(request) | |
return res | |
end | |
end | |
str = STDIN.readlines | |
type = ARGV[0] || "en2ja" | |
ret = trans(type, str) | |
# ret = trans(type, str, Net::HTTP::Proxy("192.0.2.1", 8888)) # use proxy | |
json = JSON.parse(ret.body) | |
if json != nil and | |
json.include?("ResultSet") and | |
json["ResultSet"].include?("ResultText") and | |
json["ResultSet"]["ResultText"].include?("Results") | |
trans_text = "" | |
results = json["ResultSet"]["ResultText"]["Results"] | |
results.each do |result| | |
trans_text << "#{result["key"]}: #{result["TranslateText"]}\n" | |
trans_text << "#{result["key"]}: -> #{result["TranslatedText"]}\n" | |
end | |
puts trans_text | |
else | |
puts "failed... received data: #{ret.body}" | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; region2trans | |
(defun trans-region-en2ja () | |
"Translate english lines into japanese using Yahoo translation" | |
(interactive) | |
(save-excursion | |
(shell-command-on-region (point) (mark) "/path/to/the/dir/transregion_en2ja.sh"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
base="/path/to/the/dir" | |
$base/latex_filter.rb | $base/region2trans.rb en2ja |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment