0

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

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

我想要的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`
4

2 回答 2

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)> 
于 2012-09-29T08:06:30.390 回答
0

原来我使用的实验室的机器在我不知情的情况下安装了多个版本的 Ruby。我认为 IRB 正在使用来自其他版本的信息,因此我能够使用 bigdecimal 而无需仅通过 Ruby 命令使用它。使用已安装的其他版本的 Ruby 后,它运行良好。

于 2012-10-09T00:50:57.737 回答