gpt4 book ai didi

ruby - 使用 Ruby 使用简单的 hashmap 替换数字数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:05:32 26 4
gpt4 key购买 nike

我正在尝试想出一种使用 Ruby 来打乱(或屏蔽)一些数字数据的简单方法,以便从实时数据创建一个虚拟数据集。我想让数据尽可能接近原始格式(即保留所有非数字字符)。数据中的数字对应于个人标识号,(有时)是关系数据库中使用的键。因此,如果数字字符串出现不止一次,我想将它一致地映射到相同的(最好是唯一的)值。一旦数据被加扰,我不需要能够逆转加扰。

我创建了一个 scramble 函数,它接受一个字符串并生成一个简单的散列来将数字映射到新值(该函数只映射数字,其他一切保持原样)。为了增加安全性,每次调用该函数时,都会重新生成 key 。因此,每次调用该函数时,同一个短语将产生两个不同的结果。

module HashModule
def self.scramble(str)
numHash ={}
0.upto(9) do |i|
numHash[i.to_s]=rand(10).to_s
end

output= String.new(str)
output.gsub!(/\d/) do|d|
d.replace numHash[d]
end

puts "Input: " + str
puts "Hash Key: " + numHash.to_s
puts "Output: " + output
end
end

HashModule.scramble("56609-8 NO PCT 001")
HashModule.scramble("56609-8 NO PCT 001")

这会产生以下输出:

Input: 56609-8 NO PCT 001
Hash Key: {"0"=>"9", "1"=>"4", "2"=>"8",
"3"=>"9", "4"=>"4", "5"=>"8",
"6"=>"4", "7"=>"0", "8"=>"2",
"9"=>"1"}
Output: 84491-2 NO PCT 994

Input: 56609-8 NO PCT 001
Hash Key: {"0"=>"2", "1"=>"0", "2"=>"9",
"3"=>"8", "4"=>"4", "5"=>"5",
"6"=>"7", "7"=>"4", "8"=>"2",
"9"=>"0"}
Output: 57720-2 NO PCT 220

给定数据集:

PTO NO PC
R5632893423 IP
R566788882-001
NO PCT AMB PTO
NO AMB/CALL IP
A566788882
1655543AACHM IP
56664320000000
00566333-1

我首先将所有数字提取到一个数组中。然后我使用我创建的加扰函数来创建替换 HashMap ,例如

 {"5632893423"=>"5467106076", "566788882"=>"888299995", 
"001"=>"225", "1655543"=>"2466605",
"56664320000000"=>"70007629999999",
"00566333"=>"00699999", "1"=>"3"}

[顺便说一句,在我的例子中,我还没有找到一种方法来坚持哈希值都是唯一的,这与被映射的字符串对应于关系数据库中的唯一 ID 的事件有关,如上所述.]

我在我的原始字符串上使用 gsub 并将散列键替换为加扰值。我的代码有效,但我很想知道如何让它更简洁。我通过每次调用函数时重新生成 key 来实现,我创建了额外的工作。 (否则,我可以只创建一个 key 来替换所有数字)。

有没有人对我如何以其他方式完成此任务有建议? (我是 Ruby 的新手,所以改进我的代码的建议也很受欢迎)。

input = <<EOS
PTO NO PC
R5632893423 IP
R566788882-001
NO PCT AMB PTO
NO AMB/CALL IP
A566788882
1655543AACHM IP
56664320000000
00566333-1
EOS

module HashModule
def self.scramble(str)
numHash ={}
0.upto(9) do |i|
numHash[i.to_s]=rand(10).to_s
end

output= String.new(str)
output.gsub!(/\d/) do|d|
d.replace numHash[d]
end
return output
end
end

# Extract unique non-null numbers from the input file
numbers = input.split(/[^\d]/).uniq.reject{ |e| e.empty? }

# Create a hash that maps each number to a scrambled value
# Using the function defined above

mapper ={}
numbers.map(&:to_s).each {|x| mapper[x]=HashModule.scramble(x)}

# Create a regexp to find all numbers in input file
re = Regexp.new(mapper.keys.map { |x| Regexp.escape(x) }.join('|'))

# Replace numbers with scrambled values
puts input.gsub(re, mapper)

以上代码产生以下输出:

PTO NO PC
R7834913043 IP
R799922223-772
NO PCT AMB PTO
NO AMB/CALL IP
A799922223
6955509AACHM IP
13330271111111
66166777-6

最佳答案

也许是这样的:

module HashModule
ScrambleKey = Hash[(0..9).map(&:to_s).zip((0..9).to_a.shuffle)]
def self.scramble(str); str.gsub(/\d/){ScrambleKey[$&]} end
end

puts HashModule.scramble(input)

给出:

PTO NO PC
R6907580170 IP
R699455557-223
NO PCT AMB PTO
NO AMB/CALL IP
A699455557
3966610AACHM IP
69991072222222
22699000-3

关于ruby - 使用 Ruby 使用简单的 hashmap 替换数字数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14808299/

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