`
koen
  • 浏览: 80743 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

我的第一个Ruby程序:调用金山词霸在线API查询单词

阅读更多

使用"金山词霸查词接口beta版"实现了在线查询单词功能,以下是代码:

require 'open-uri'
require 'rexml/document'
require 'cgi'
include REXML

#
# My first ruby program: Search word via Ciba web API.
#
# Refer the following stuff:
#
# 1. http://web.iciba.com/partner/api01.shtml
# 2. http://sincenow.chayv.com/?p=43
# 3. http://simohayha.iteye.com/blog/160234
# 4. http://www.ruby-forum.com/topic/58715
# 5. http://www.ruby-doc.org/core/
#

class Ciba
  def initialize(word)
    @word = word
  end

  def search
    open("http://dict-co.iciba.com/api/dictionary.php?w=" + CGI::escape(@word)) do
      |http| @result = http.read
    end
  end

  def parse
    doc = Document.new(@result)
    doc.elements.each("dict/key") { |e| puts e.text }
    doc.elements.each("dict/ps") { |e| puts "[" + e.text + "]" }
    pos_array = Array.new()
    doc.elements.each("dict/pos") { |e| pos_array.push(e.text) }
    acc_array = Array.new()
    doc.elements.each("dict/acceptation") { |e| acc_array.push(e.text) }
    pos_array.each_index { |i| puts pos_array[i] + " " + acc_array[i] }
    pos_array.clear
    doc.elements.each("dict/sent/orig") { |e| pos_array.push(e.text) }
    acc_array.clear
    doc.elements.each("dict/sent/trans") { |e| acc_array.push(e.text) }
    pos_array.each_index { |i| puts "例句" + (i+1).to_s + ":" + pos_array[i] + " " + acc_array[i] }
  end
end

ciba = Ciba.new("cool")
puts ciba.search
3.times {puts ""}
ciba.parse
 

以上程序的输出为:

 

<?xml version="1.0" encoding="UTF-8"?><dict num="219" id="219" name="219"><key>cool</key><ps>ku:l</ps><pron>http://res.iciba.com/resource/amp3/b/1/b1f4f9a523e36fd969f4573e25af4540.mp3</pron><pos>n.</pos><acceptation>凉爽, 凉爽的空气</acceptation><pos>adj.</pos><acceptation>凉爽, 冷静的, 无所顾虑的, 淡漠的</acceptation><pos>v.</pos><acceptation>使冷, 使镇定</acceptation><sent><orig>Cool.</orig><pron>http://res.iciba.com/resource/phrase_mp3/6/3/63ebe9d367b10545dfd1c2cb7448c3e4.mp3</pron><trans>很好。</trans></sent><sent><orig>That's cool!</orig><pron>http://res.iciba.com/resource/phrase_mp3/8/c/8c6332d149db7bcc1498d0af543cc82b.mp3</pron><trans>好极了!</trans></sent><sent><orig>Keep cool!</orig><pron>http://res.iciba.com/resource/phrase_mp3/b/9/b98249b38337c5088bbc660d8f872d6a.mp3</pron><trans>保持冷静!别慌!</trans></sent><sent><orig>Cool it.</orig><pron>http://res.iciba.com/resource/phrase_mp3/e/6/e68dc4981c2310e883786e7f723fa37d.mp3</pron><trans>冷静下来,别那样兴奋。</trans></sent><sent><orig>a cool autumn day; a cool room; cool summer dresses; cool drinks; a cool breeze.</orig><pron>http://res.iciba.com/resource/phrase_mp3/a/f/af885e92d18e71365cfcbb9642c4ec60.mp3</pron><trans>凉爽的秋天;凉爽的房间;凉爽的夏装;凉爽的饮料;凉爽的微风。</trans></sent></dict>



cool
[ku:l]
n. 凉爽, 凉爽的空气
adj. 凉爽, 冷静的, 无所顾虑的, 淡漠的
v. 使冷, 使镇定
例句1:Cool. 很好。
例句2:That's cool! 好极了!
例句3:Keep cool! 保持冷静!别慌!
例句4:Cool it. 冷静下来,别那样兴奋。
例句5:a cool autumn day; a cool room; cool summer dresses; cool drinks; a cool breeze. 凉爽的秋天;凉爽的房间;凉爽的夏装;凉爽的饮料;凉爽的微风。
 

 

 

 

 

 

 

1
0
分享到:
评论

相关推荐

    rubyapi:Ruby API使搜索或浏览Ruby语言API文档变得轻松快捷。

    Ruby API:轻松查找Ruby文档 Ruby API使搜索或浏览Ruby语言API文档变得轻松快捷。 为什么? 现有的Ruby文档网站很难搜索,也无法在移动设备上阅读。 Ruby API提供了受启发的即时搜索结果,以及易于在任何大小的...

    Ruby API 帮助文档

    Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档Ruby API 帮助文档

    ruby API资料

    ruby 帮助资料 API ruby 帮助资料 API ruby 帮助资料 API ruby 帮助资料 API ruby 帮助资料 API

    rubyzip:官方Rubyzip存储库

    如果任何需要旧版rubyzip的第三方gem出现问题,则可以使用以下解决方法: gem 'rubyzip' , '&gt;= 1.0.0' # will load new rubyzip version gem 'zip-zip' # will load compatibility for old rubyzip API. 要求 Ruby...

    curso_tour_of_heroes_api:Ruby on Rails作为API课程-源码

    英雄之旅API Ruby on Rails 6课程作为创建Heroes CRUD的API。 该存储库是YouTube 的。 Ruby版本2.7.2 Rails版本6.1.x 数据库SQLite3(dev)/ PostgreSQL(产品)运行项目的初始设置# clone the projectgit clone ...

    Ruby.Programming_向Ruby之父学程序设计(第2版)

    《Ruby Programming:向Ruby之父学程序设计(第2版)》是为了让完全没有程序设计经验的读者也能灵活地使用Ruby,因此书中详细地说明了各种知识。从程序所需要的变量、常数、方法、类、控制结构等语法的说明,到类的主要...

    orthanc-ruby:Orthanc DICOM 服务器 REST API 的 Ruby 实现

    (这是我的第一个 API 客户端 gem,非常感谢有经验的帮助或建议) :) 安装 注意:如果您需要一个 Orthanc 实例进行测试,您可以使用项目构建一个随时可用的 Orthanc 服务器虚拟机。 将此行添加到应用程序的 ...

    ruby-openai:OpenAI GPT-3 API的Ruby宝石

    安装将此行添加到您的应用程序的Gemfile中: gem 'ruby-openai' 然后执行: $ bundle install或将其自己安装为: $ gem install ruby-openai用法从获取您的API密钥与dotenv 如果您使用的是 ,则可以将密钥添加到.env...

    Ruby-FastJSONAPI一个用于Ruby对象的快速JSONAPI的序列化器

    Fast JSON API:一个用于Ruby对象的快速JSON:API的序列化器

    Ruby-rufusschedulerRuby作业定时调度程序

    rufus-scheduler - Ruby 作业定时调度程序

    ruby:基于Ruby的PubNub API

    配置PubNub 使用RubyGems将PubNub集成到您的项目中: gem install pubnub 创建一个新的App.rb文件并添加以下代码。 require 'pubnub'pubnub = Pubnub . new ( subscribe_key : :mySubscribeKey , publish_key : :...

    carvoyant-api-ruby:用于Carvoyant API的Ruby API包装器

    CarvoyantApi Carvoyant API gem允许Ruby开发人员以编程方式访问Carvoyant资源 安装 将此行添加到您的应用程序的Gemfile中: gem 'carvoyant-api-ruby' 然后执行: $ bundle ... 创建一个新的拉取请求

    Ruby_向ruby之父学程序设计(第二版)(经典入门)

    第一部分:通过简单的Ruby程序来介绍程序的基本架构。  第二部分:介绍基础语法规则,以及类、模块等面向对象程序设计的思考方法与用词。   第三部分:对一些基础类逐一介绍其功能与用法。 第四部分:介绍一些...

    taxjar-ruby:Ruby的Sales Tax API客户端

    从API调用返回的数据被映射到Ruby对象 执照支持贡献 支持的Ruby版本 Ruby 2.3或更高版本 宝石依赖 安装此gem还会捆绑以下依赖项: http-具有可链接API和完整流支持的快速Ruby HTTP客户端。 可寻址-替换Ruby标准...

    ruby中英文api

    ruby中英文api 适合于初学者。 希望能给大家带来一些帮助

    ruby api html

    ruby 1.8.6 api html格式

    rust-vs-ruby:“Rust 与 Ruby”的示例应用程序

    Rust 与 Ruby:构建 API 这是 Rust 与 Ruby:构建 API 博客文章的存储库。 如何运行示例 git clone https://github.com/Sdogruyol/rust-vs-ruby Ruby Ruby版本:2.2.1 Ruby 服务器:瘦 1.6.3 框架:古巴 3.4.0 ...

    authy-ruby:Ruby库访问authy API

    用于Twilio Authy两因素身份验证(2FA)API的Ruby客户端有关Ruby使用Authy API的文档,请参见。 Authy API支持2FA的多个渠道: 通过短信和语音的一次性密码。 软令牌( 通过Authy应用程序) 通过Authy App进行推送...

    dropbox-api:Dropbox API Ruby客户端

    第一版,涵盖了整个API。 重要的!!! 从版本0.2.0开始, Dropbox::API::File#delete和Dropbox::API::Dir#delete都消失了! 原因是它基于Hashie :: Mash并搞砸了Hash#delete。 它由Dropbox::API::File#destroy...

    ruby on rails api

    ruby on rails api方便查阅

Global site tag (gtag.js) - Google Analytics