gpt4 book ai didi

ruby - 需要 bigdecimal loaderror ruby

转载 作者:太空宇宙 更新时间:2023-11-04 04:21:33 24 4
gpt4 key购买 nike

我有一个非常令人沮丧的问题。我的家用机器上有 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`

最佳答案

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

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

关于ruby - 需要 bigdecimal loaderror ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12650964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com