将英语句子转换为ruby键值对的最佳方法是什么?
我想在键值对中转换以下句子,如({total_amount:discount_amount})。
$10 off $30 of food #should return {30: 10} $30 of awesome for $10 #should return {30: 20} $20 Sneakers for $5 #should return {20: 15}
我怎么处理这个? 这将有所帮助如果我可以得到任何提示开始……
我假设每个感兴趣的句子都是这样的:
$XX....for....$YY
或者像这样:
$XX....off....$YY
其中XX
和YY
是非负整数, "for"
和"off"
是告诉你这两个数字要做什么的关键词。 如果是这样,似乎你想要这个:
arr = ["$10 off $30 of food", "$30 of awesome for $10", "$20 Sneakers for $5"]
我们首先在扩展模式下定义正则表达式:
r = / \$ # match dollar sign (\d+) # match one or more digits in capture group 1 .*? # match any number of any character lazily \b # word boundary (so "buzzoff" is not matched) (for|off) # match "for" or "off" in capture group 2 \b # another word boundary .*? # match any number of any character lazily \$ # match dollar sign (\d+) # match one or more digits in capture group 3 /x # extended mode for regex def arr.each_with_object([]) do |s,a| s[r] f,l = $1.to_i, $3.to_i case $2 when "for" then a << [f,fl] when "off" then a << [l,f] end end #=> [[30, 10], [30, 20], [20, 15]]
以下是步骤:
enum = arr.each_with_object([]) #=> #
我们可以使用Enumerator#next将enum
每个元素传递给块并将其分配给块变量:
s, a = enum.next #=> ["$10 off $30 of food", []] s #=> "$10 off $30 of food" a #=> []
我们现在执行块计算:
s[r] #=> "10 off $30"
三个捕获组的值为:
$1 #=> "10" $2 #=> "off" $3 #=> "30"
因此:
f,l = $1.to_i, $3.to_i #=> [10, 30]
所以:
case $2 when "for" then a << [f,fl] when "off" then a << [l,f] end
是:
case "off" when "for" then [] << [10, 10-30] when "off" then [] << [30, 10] end #=> [[30, 10]] a #=> [[30, 10]]
其余计算类似地执行。
def read_discount(file_name) File.foreach(file_name) do |line| /[^\d]*(\d+)[^\d]*(\d+)/ =~ line puts "#{$1}:#{$2}" if $1 end end read_discount("31621358.txt")