打印

[技术介绍] 一个包, linguistics

一个包, linguistics

在做Euler 17 的时候, 要数英文数字 1 到 1000 里共用了多少字母? 这好象是考我英文嘛! 我在EXCEL里用格子算出来的, 觉得没编程的必要. 在Euler 的论坛上看了看, 多数也是死算出来的, 除了这个.

require "linguistics"
Linguistics::use( :en )
all = (1..1000).inject(0) do |total, num|
total + num.en.numwords.split(/[ -]/).inject(0) { |total, word| total + word.length }
end
p all


这里用了一个包, 可以用gem install linguistics来安装.
它的用法等可以去http://deveiate.org/projects/Linguistics看它的wiki.

以后如果不会写序数啊, 过去式过去分词啊什么的就可以查了. 编ruby的php?name=%B1%E0%BC%AD%C6%F7" onclick="tagshow(event)" class="t_tag">编辑器的时候可能可以用它.

很好, 很强大.

----------------------------------------------------
拷一些例子

复数
"box".en.plural
# => "boxes"
"mouse".en.plural
# => "mice"

不定冠词
"book".en.a
# => "a book"
"article".en.a
# => "an article"

现在分词
"runs".en.present_participle
# => "running"
"eats".en.present_participle
# => "eating"
"spies".en.present_participle
# => "spying"

序数
5.en.ordinal
# => "5th"
2004.en.ordinal
# => "2004th"

数字
2004.en.numwords
# => "two thousand and four"
2385762345876.en.numwords
# => "two trillion, three hundred and eighty-five billion, seven
# hundred and sixty-two million, three hundred and forty-five thousand,
# eight hundred and seventy-six"

"模糊"量词
"cow".en.quantify( 5 )
# => "several cows"
"cow".en.quantify( 1005 )
# => "thousands of cows"
"cow".en.quantify( 20_432_123_000_000 )
# => "tens of trillions of cows"

连词
animals = %w{dog cow ox chicken goose goat cow dog rooster llama
pig goat dog cat cat dog cow goat goose goose ox alpaca}
puts "The farm has: " + animals.en.conjunction
#=>"The farm has: four dogs, three cows, three geese, three goats,
two oxen, two cats, a chicken, a rooster, a llama, a pig,
and an alpaca"

.......

[ 本帖最后由 5swords 于 2008-4-14 10:17 编辑 ]
本帖最近评分记录
  • xavier R币 +10 这个太棒了~ 2008-4-15 19:18

TOP

确实很强大
不过后面需要link_parser配合的那几个例子我运行不起来,不知原因
Digging ruby with Pickaxe,
Running agilely on rails

TOP

It needs some other packages... or...

TOP

安了link_parser,就是不成功
貌似不是Linguistics的问题而是link_parser本身的问题.
Digging ruby with Pickaxe,
Running agilely on rails

TOP

好东西。。。可怜我的17题。

def Euler17()
$n = {1=>"one",2=>"two",3=>"three",4=>"four",5=>"five",6=>"six" \
        ,7=>"seven",8=>"eight",9=>"nine",10=>"ten"}
$k = {11=>"eleven",12=>"twelve",13=>"thirteen",14=>"fourteen",\
      15=>"fifteen",16=>"sixteen",17=>"seventeen",18=>"eighteen",19=>"nineteen"}
$m = {2=>"twenty",3=>"thirty",4=>"forty",5=>"fifty", \
      6=>"sixty",7=>"seventy",8=>"eighty",9=>"ninety"}
  sum = 0
  1.upto(1000) {|i|
    sum += tr(i).to_s.length
  }
  puts sum
end
def tr(x)
  ret = ""
  if x == 1000 then 
    return "onethousand"
  end
  if x >= 100 then
    ret = $n[x/100] + "hundred"
    x = x - x/100*100
  end
  if x > 10 then 
    ret = ret + "and" if ret.length > 0
    if x >= 20 then
      ret=ret+$m[x/10]
      x  = x - x/10*10
      ret=ret+$n[x] if x > 0 
    else
      ret=ret+$k[x]
    end
  else
    if x > 0 then
      ret = ret + "and" if ret.length > 0
      ret=ret+$n[x]
    end
  end
  return ret
end
#test
Euler17()


TOP

湖南长沙隐形无线耳机深耳道MP3无线耳塞使用指南

提示: 作者被禁止或删除 内容自动屏蔽

TOP

2008-11-23 22:14 Crawled by CCBot/1.0 (+http://www.commoncrawl.org/bot.html) @38.103.63.61