需要bigdecimal loaderror ruby

时间:2012-09-29 07:53:07

标签: ruby linux compiler-errors irb

我有一个非常令人沮丧的问题。我的家用机器上有ubuntu 12.04,带有ruby 1.8.7,我刚刚完成了一个项目,可以在我的家用机器上完成编译和运行,但是当我把它移植到我的大学实验室时(它必须为我编译才能获得一个等级) ,他们也在运行ruby 1.8.7)我收到以下错误:

`require': no such file to load -- bigdecimal (LoadError)

I类试图要求的是BigDecimal类,我需要更大的浮点精度来进行项目的某些对数计算。 任何人都可以帮我解释为什么这会成为一个问题?我认为BigDecimal类包含在1.8.7的ruby核心库中?我在这里错过了什么吗? 编辑:正在编译的代码。

  `#this is the language model generator
    require "trainer"
    require "bigdecimal"

class Model
    attr_accessor :frequency_tables

def initialize(trainer, test_file)
@trainer = trainer
@frequency_tables = []
@frequency_tables[0] = trainer.corpus
@frequency_tables[1] = trainer.freq
@frequency_tables[2] = trainer.bifreq
@frequency_tables[3] = trainer.trifreq
@frequency_tables[4] = trainer.word_count()
@frequency_tables[5] = trainer.vocab_count()
end

def unigram_count(string)
string.upcase!
return @frequency_tables[1][string]
end
def bigram_count(string)
string.upcase!
return @frequency_tables[2][string]
end
def trigram_count(string)
string.upcase!
return @frequency_tables[3][string]
end

def round(float)
return ((float * 10000).round.to_f) / 10000 
end

def process_unigram(sentence)
probability = BigDecimal.new("1") 
sen_arr = sentence.split(" ")
sen_arr.each { |word|
#  binding.pry
  probability *= (BigDecimal.new(unigram_count(word).to_s()) / BigDecimal.new(@frequency_tables[4].to_s())) 
}
unless probability.eql? 0
    val= ((Math.log(probability)/Math.log(2))) 
    return retval = round(val)  
end
return "undefined" 
end

def process_bigram(sentence)
probability = BigDecimal.new("1")
sen_arr = sentence.split(" ")
for i in (0..(sen_arr.length-1))
  unless i == 0
  denom = BigDecimal.new(unigram_count(sen_arr[i-1]).to_s())/BigDecimal.new(@frequency_tables[4].to_s())
    unless denom.eql? 0
      probability *= (BigDecimal.new(bigram_count(sen_arr[i-1]+" "+sen_arr[i]).to_s()) / BigDecimal.new(@frequency_tables[4].to_s()))/denom  
     else
      probability = 0
     end
  else
  probability *= (BigDecimal.new(bigram_count("PHI "+sen_arr[i]).to_s()) / BigDecimal.new(@frequency_tables[4].to_s())) / (BigDecimal.new(unigram_count("PHI").to_s())/BigDecimal.new(@frequency_tables[4].to_s()))   
  end
end
unless probability.eql? 0
    val= ((Math.log(probability)/Math.log(2))) 
    retval = round(val) 
    return retval
end
return "undefined"
end

def process_trigram(sentence)
probability = BigDecimal.new("1")
sen_arr = sentence.split(" ")
for i in (0..(sen_arr.length-1))
  if i >= 2
  denom = (BigDecimal.new(bigram_count(sen_arr[i-2]+" "+sen_arr[i-1]).to_s()))
      unless denom.eql? 0
      probability *= (BigDecimal.new(trigram_count(sen_arr[i-2]+" "+sen_arr[i-1]+" "+sen_arr[i]).to_s()))/denom   
      else
    probability *= 0
      end
  elsif i == 1
  denom = (BigDecimal.new(bigram_count("PHI "+sen_arr[i-1]).to_s()))
      unless denom.eql? 0
      probability *= (BigDecimal.new(trigram_count("PHI "+sen_arr[i-1]+" "+sen_arr[i]).to_s()))/denom  
      else
      probability = 0
      end
  else
  probability *= (BigDecimal.new(trigram_count("PHI PHI "+sen_arr[i]).to_s())) / (BigDecimal.new(unigram_count("PHI").to_s()))   
  end
end
unless probability.eql? 0 
    val= ((Math.log(probability)/Math.log(2))) 
    retval = round(val) 
    return retval
end
return "undefined"
end

def process_smooth_bigram(sentence)
probability = BigDecimal.new("1")
sen_arr = sentence.split(" ")
for i in (0..(sen_arr.length-1))
  unless i == 0
  bigram_c = bigram_count(sen_arr[i-1]+" "+sen_arr[i])+1
  ugram_c = unigram_count(sen_arr[i-1])+@frequency_tables[5]
  probability *= (BigDecimal.new(bigram_c.to_s()))/(BigDecimal.new(ugram_c.to_s()))   
  else
  probability *= (BigDecimal.new((bigram_count("PHI "+sen_arr[i])+1).to_s())) / (BigDecimal.new((unigram_count("PHI")+@frequency_tables[5]).to_s()))   
  end
end
unless probability.eql? 0
    val= ((Math.log(probability)/Math.log(2))) 
    retval = round(val) 
    return retval
end
return "undefined"
end

def process_smooth_trigram(sentence)
probability = BigDecimal.new("1")
sen_arr = sentence.split(" ")
for i in (0..(sen_arr.length-1))
  if i >= 2
  denom = (BigDecimal.new((bigram_count(sen_arr[i-2]+" "+sen_arr[i-1])+@frequency_tables[5]).to_s()))
  probability *= (BigDecimal.new((trigram_count(sen_arr[i-2]+" "+sen_arr[i-1]+" "+sen_arr[i])+1).to_s()))/denom   
  elsif i == 1
  denom = (BigDecimal.new((bigram_count("PHI "+sen_arr[i-1])+@frequency_tables[5]).to_s()))
  probability *= (BigDecimal.new((trigram_count("PHI "+sen_arr[i-1]+" "+sen_arr[i])+1).to_s()))/denom  
  else
  probability *= (BigDecimal.new((trigram_count("PHI PHI "+sen_arr[i])+1).to_s())) / (BigDecimal.new((unigram_count("PHI")+@frequency_tables[5]).to_s()))   
  end
end
unless probability.eql? 0 
    val= ((Math.log(probability)/Math.log(2))) 
    retval = round(val) 
    return retval
end
return "undefined"
end
end`

2 个答案:

答案 0 :(得分:0)

对我来说很好。可能是语法错误,添加一些代码然后我会更新。

ruby-1.8.7-p334 :001 > require 'bigdecimal'
 => true 

ruby-1.8.7-p334 :002 >  BigDecimal("1.0e+25") - BigDecimal("9999999999999999900000000.0")
 => #<BigDecimal:10035f970,'0.1E9',9(45)> 

答案 1 :(得分:0)

在我不知情的情况下,我正在使用的实验室上的机器安装了多个版本的Ruby。我认为IRB正在使用其他版本的信息,因此我可以使用bigdecimal而不使用它只使用Ruby命令。在使用安装的其他版本的Ruby之后,它运行良好。