2012-09-29 98 views
0

我有一個非常令人沮喪的問題。我使用ruby 1.8.7在家用機器上安裝了Ubuntu 12.04,而且我幾乎沒有完成一個項目,它可以在我的家用機器上完美編譯和運行,但是當我將它移植到我的大學實驗室時(它必須爲我編譯以獲得一個等級他們還運行紅寶石1.8.7)我得到以下錯誤:需要bigdecimal loaderror ruby​​

`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])[email protected]_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")[email protected]_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])[email protected]_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])[email protected]_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")[email protected]_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` 
+0

你的'require'語句在哪裏? –

回答

0

原來我使用的實驗室機器上安裝了多個版本的Ruby,但我不知道。我認爲IRB正在使用來自其他版本的信息,因此我可以使用bigdecimal而不使用Ruby命令。使用安裝的其他版本的Ruby後,它工作正常。

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)> 
+0

當我進入irb時,我可以做'bigdecimal'以及我變得真實,然後我也可以操作這個類。 WTF?我會在一些代碼中編輯我的原始文章。 – noname

+0

如果將模型直接複製/粘貼到「irb」中,會發生什麼情況?它識別它還是錯誤? –

相關問題