在做
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 编辑 ]