OSDN Git Service

Corrected namespace of GSL::Vector. This line is not reached, though.
[shogi-server/shogi-server.git] / mk_rate
1 #!/usr/bin/ruby
2 # $Id$
3 #
4 # Author:: Daigo Moriwaki
5 # Homepage:: http://sourceforge.jp/projects/shogi-server/
6 #
7 #--
8 # Copyright (C) 2006-2009 Daigo Moriwaki <daigo at debian dot org>
9 #
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 #++
24 #
25 # == Synopsis
26 #
27 # mk_rate reads game results files generated by the mk_game_results command,
28 # calculates rating scores of each player, and then outputs a yaml file 
29 # (players.yaml) that Shogi-server can recognize.
30 #
31 # == Usage
32 #
33 # ./mk_rate [options] GAME_RESULTS_FILE [...]
34 #
35 # ./mk_rate [options]
36
37 # GAME_RESULTS_FILE::
38 #   a path to a file listing results of games, which is genrated by the
39 #   mk_game_results command.
40 #   In the second style above, the file content can be read from the stdin.
41 #
42 # --base-date::
43 #   a base time point for this calicuration (default now). Ex. '2009-10-31'
44 #
45 # --half-life::
46 #   n [days] (default 60)
47 #   
48 # --half-life-ignore::
49 #   m [days] (default  7)
50 #   after m days, the half-life effect works
51 #
52 # --fixed-rate-player::
53 #   player whose rate is fixed at the rate
54 #
55 # --fixed-rate::
56 #   rate 
57 #
58 # --skip-draw-games::
59 #   skip draw games. [default: draw games are counted in as 0.5 win and 0.5
60 #   lost.]
61 #
62 # --help::
63 #   show this message
64 #
65 # == PREREQUIRE
66 #
67 # Sample Command lines that isntall prerequires will work on Debian.
68 #
69 # * Ruby 1.8.7
70 #
71 #   $ sudo aptitude install ruby1.8
72 #
73 # * Rubygems
74 #
75 #   $ sudo aptitude install rubygems
76 #
77 # * Ruby bindings for the GNU Scientific Library (GSL[http://rb-gsl.rubyforge.org/])
78 #
79 #   $ sudo aptitude install libgsl-ruby1.8
80 #
81 # * RGL: {Ruby Graph Library}[http://rubyforge.org/projects/rgl/]
82 #
83 #   $ sudo gem install rgl
84 #
85 # == Examples
86 #
87 #   $ ./mk_rate game_results.txt > players.yaml
88 #
89 #   $ ./mk_game_results . | ./mk_rate > players.yaml
90 #
91 # If you do not want the file to be update in case of errors, 
92 #
93 #   $ ./mk_rate game_results.txt && ./mk_rate game_results.txt > players.yaml
94 #
95 # == How players are rated
96 #
97 # The conditions that games and players are rated as following:
98 #
99 # * Rated games, which were played by both rated players.
100 # * Rated players, who logged in the server with a name followed by a trip: "name,trip".
101 # * (Rated) players, who played more than $GAMES_LIMIT [15] (rated) games. 
102 #
103
104 require 'yaml'
105 require 'time'
106 require 'getoptlong'
107 require 'gsl'
108 require 'rubygems'
109 require 'rgl/adjacency'
110 require 'rgl/connected_components'
111
112 #################################################
113 # Constants
114 #
115
116 # Count out players who play less games than $GAMES_LIMIT
117 $GAMES_LIMIT = $DEBUG ? 0 : 15
118 WIN_MARK  = "win"
119 LOSS_MARK = "lose"
120 DRAW_MARK = "draw"
121
122 # Holds players
123 $players = Hash.new
124 # Holds the last time when a player gamed
125 $players_time = Hash.new { Time.at(0) }
126
127
128 #################################################
129 # Keeps the value of the lowest key
130 #
131 class Record
132   def initialize
133     @lowest = []
134   end
135
136   def set(key, value)
137     if @lowest.empty? || key < @lowest[0]
138       @lowest = [key, value]
139     end
140   end
141
142   def get
143     if @lowest.empty?
144       nil
145     else
146       @lowest[1]
147     end
148   end
149 end
150
151 #################################################
152 # Calculates rates of every player from a Win Loss GSL::Matrix
153 #
154 class Rating
155   include Math
156
157   # The model of the win possibility is 1/(1 + 10^(-d/400)).
158   # The equation in this class is 1/(1 + e^(-Kd)).
159   # So, K should be calculated like this.
160   K = Math.log(10.0) / 400.0
161   
162   # Convergence limit to stop Newton method.
163   ERROR_LIMIT = 1.0e-3
164   # Stop Newton method after this iterations.
165   COUNT_MAX = 500
166
167   # Average rate among the players
168   AVERAGE_RATE = 1000
169
170   
171   ###############
172   # Class methods
173   #  
174   
175   ##
176   # Calcurates the average of the vector.
177   #
178   def Rating.average(vector, mean=0.0)
179     sum = Array(vector).inject(0.0) {|sum, n| sum + n}
180     vector -= GSL::Vector[*Array.new(vector.size, sum/vector.size - mean)]
181     vector
182   end
183
184   ##################
185   # Instance methods
186   #
187   def initialize(win_loss_matrix)
188     @record = Record.new
189     @n = win_loss_matrix
190     case @n
191     when GSL::Matrix, GSL::Matrix::Int
192       @size = @n.size1
193     when ::Matrix
194       @size = @n.row_size
195     else
196       raise ArgumentError
197     end
198     initial_rate
199   end
200   attr_reader :rate, :n
201
202   def player_vector
203     GSL::Vector[*
204       (0...@size).collect {|k| yield k}
205     ]
206   end
207
208   def each_player
209     (0...@size).each {|k| yield k}
210   end
211
212   ##
213   # The possibility that the player k will beet the player i.
214   #
215   def win_rate(k,i)
216     1.0/(1.0 + exp(@rate[i]-@rate[k]))
217   end
218
219   ##
220   # Most possible equation
221   #
222   def func_vector
223     player_vector do|k| 
224       sum = 0.0
225       each_player do |i|
226         next if i == k
227         sum += @n[k,i] * win_rate(i,k) - @n[i,k] * win_rate(k,i) 
228       end
229       sum * 2.0
230     end
231   end
232
233   ##
234   #           / f0/R0 f0/R1 f0/R2 ... \
235   # dfk/dRj = | f1/R0 f1/R1 f1/R2 ... |
236   #           \ f2/R0 f2/R1 f2/R2 ... /
237   def d_func(k,j)
238     sum = 0.0
239     if k == j
240       each_player do |i|
241         next if i == k
242         sum += win_rate(i,k) * win_rate(k,i) * (@n[k,i] + @n[i,k])
243       end
244       sum *= -2.0
245     else # k != j
246       sum = 2.0 * win_rate(j,k) * win_rate(k,j) * (@n[k,j] + @n[j,k])
247     end
248     sum
249   end
250
251   ##
252   # Jacobi matrix of the func().
253   #   m00 m01
254   #   m10 m11
255   #
256   def j_matrix
257     GSL::Matrix[*
258       (0...@size).collect do |k|
259         (0...@size).collect do |j|
260           d_func(k,j)
261         end
262       end
263     ]
264   end
265
266   ##
267   # The initial value of the rate, which is of very importance for Newton
268   # method.  This is based on my huristics; the higher the win probablity of
269   # a player is, the greater points he takes.
270   #
271   def initial_rate
272     possibility = 
273       player_vector do |k|
274         v = GSL::Vector[0, 0]
275         each_player do |i|
276           next if k == i
277           v += GSL::Vector[@n[k,i], @n[i,k]]
278         end
279         v.nrm2 < 1 ? 0 : v[0] / (v[0] + v[1])
280       end
281     rank = possibility.sort_index
282     @rate = player_vector do |k|
283       K*500 * (rank[k]+1) / @size
284     end
285     average!
286   end
287
288   ##
289   # Resets @rate as the higher the current win probablity of a player is, 
290   # the greater points he takes. 
291   #
292   def initial_rate2
293     @rate = @record.get || @rate
294     rank = @rate.sort_index
295     @rate = player_vector do |k|
296       K*@count*1.5 * (rank[k]+1) / @size
297     end
298     average!
299   end
300
301   # mu is the deaccelrating parameter in Deaccelerated Newton method
302   def deaccelrate(mu, old_rate, a, old_f_nrm2)
303     @rate = old_rate - a * mu
304     if func_vector.nrm2 < (1 - mu / 4.0 ) * old_f_nrm2 then
305       return
306     end
307     if mu < 1e-4
308       @record.set(func_vector.nrm2, @rate)
309       initial_rate2
310       return
311     end
312     $stderr.puts "mu: %f " % [mu] if $DEBUG
313     deaccelrate(mu*0.5, old_rate, a, old_f_nrm2)
314   end
315
316   ##
317   # Main process to calculate ratings.
318   #
319   def rating
320     # Counter to stop the process. 
321     # Calulation in Newton method may fall in an infinite loop
322     @count = 0
323
324     # Main loop
325     begin
326       # Solve the equation: 
327       #   J*a=f
328       #   @rate_(n+1) = @rate_(n) - a
329       #
330       # f.nrm2 should approach to zero.
331       f = func_vector
332       j = j_matrix
333
334       # $stderr.puts "j: %s" % [j.inspect] if $DEBUG
335       $stderr.puts "f: %s -> %f" % [f.to_a.inspect, f.nrm2] if $DEBUG
336
337       # GSL::Linalg::LU.solve or GSL::Linalg::HH.solve would be available instead.
338       #a = GSL::Linalg::HH.solve(j, f)
339       a, = GSL::MultiFit::linear(j, f)
340       a = self.class.average(a)
341       # $stderr.puts "a: %s -> %f" % [a.to_a.inspect, a.nrm2] if $DEBUG
342       
343       # Deaccelerated Newton method
344       # GSL::Vector object should be immutable.
345       old_rate   = @rate
346       old_f      = f
347       old_f_nrm2 = old_f.nrm2
348       deaccelrate(1.0, old_rate, a, old_f_nrm2)
349       @record.set(func_vector.nrm2, @rate)
350
351       $stderr.printf "|error| : %5.2e\n", a.nrm2 if $DEBUG
352
353       @count += 1
354       if @count > COUNT_MAX
355         $stderr.puts "Values seem to oscillate. Stopped the process."
356         $stderr.puts "f: %s -> %f" % [func_vector.to_a.inspect, func_vector.nrm2]
357         break
358       end
359
360     end while (a.nrm2 > ERROR_LIMIT * @rate.nrm2)
361     
362     @rate = @record.get
363     $stderr.puts "resolved f: %s -> %f" %
364       [func_vector.to_a.inspect, func_vector.nrm2] if $DEBUG
365
366     @rate *= 1.0/K
367     finite!
368     self
369   end
370
371   ##
372   # Make the values of @rate finite.
373   #
374   def finite!
375     @rate = @rate.collect do |a|
376       if a.infinite?
377         a.infinite? * AVERAGE_RATE * 100
378       else
379         a
380       end
381     end
382   end
383
384   ##
385   # Flatten the values of @rate.
386   #
387   def average!(mean=0.0)
388     @rate = self.class.average(@rate, mean)
389   end
390
391   ##
392   # Translate by value
393   #
394   def translate!(value)
395     @rate += value
396   end
397
398   ##
399   # Make the values of @rate integer.
400   #
401   def integer!
402     @rate = @rate.collect do |a|
403       if a.finite?
404         a.to_i
405       elsif a.nan?
406         0
407       elsif a.infinite?
408         a.infinite? * AVERAGE_RATE * 100
409       end
410     end
411   end
412 end
413
414 #################################################
415 # Encapsulate a pair of keys and win loss matrix.
416 #   - keys is an array of player IDs; [gps+123, foo+234, ...]
417 #   - matrix holds games # where player i (row index) beats player j (column index).
418 #     The row and column indexes match with the keys.
419 #
420 # This object should be immutable. If an internal state is being modified, a
421 # new object is always returned.
422 #
423 class WinLossMatrix
424
425   ###############
426   # Class methods
427   #  
428
429   def self.mk_matrix(players)
430     keys = players.keys.sort
431     size = keys.size
432     matrix =
433       GSL::Matrix[*
434       ((0...size).collect do |k|
435         p1 = keys[k]
436         p1_hash = players[p1]
437         ((0...size).collect do |j|
438           if k == j
439             0
440           else
441             p2 = keys[j]
442             v = p1_hash[p2] || GSL::Vector[0,0]
443             v[0]
444           end
445         end)
446       end)]
447     return WinLossMatrix.new(keys, matrix)
448   end
449
450   def self.mk_win_loss_matrix(players)
451     obj = mk_matrix(players)
452     return obj.filter
453   end
454
455   ##################
456   # Instance methods
457   #
458
459   # an array of player IDs; [gps+123, foo+234, ...]
460   attr_reader :keys
461
462   # matrix holds games # where player i (row index) beats player j (column index).
463   # The row and column indexes match with the keys.
464   attr_reader :matrix
465
466   def initialize(keys, matrix)
467     @keys   = keys
468     @matrix = matrix
469   end
470
471   ##
472   # Returns the size of the keys/matrix
473   #
474   def size
475     if @keys
476       @keys.size
477     else
478       nil
479     end
480   end
481
482   ##
483   # Removes players in a rows such as [1,3,5], and then returns a new
484   # object.
485   #
486   def delete_rows(rows)
487     rows = rows.sort.reverse
488
489     copied_cols = []
490     (0...size).each do |i|
491       next if rows.include?(i)
492       row = @matrix.row(i).clone
493       rows.each do |j|
494         row.delete_at(j)
495       end
496       copied_cols << row
497     end
498     if copied_cols.size == 0
499       new_matrix = GSL::Matrix.new
500     else
501       new_matrix = GSL::Matrix[*copied_cols]
502     end
503
504     new_keys = @keys.clone
505     rows.each do |j|
506       new_keys.delete_at(j)
507     end
508
509     return WinLossMatrix.new(new_keys, new_matrix)
510   end
511
512   ##
513   # Removes players who do not pass a criteria to be rated, and returns a
514   # new object.
515   # 
516   def filter
517     $stderr.puts @keys.inspect if $DEBUG
518     $stderr.puts @matrix.inspect if $DEBUG
519     delete = []  
520     (0...size).each do |i|
521       row = @matrix.row(i)
522       col = @matrix.col(i)
523       win  = row.sum
524       loss = col.sum
525       if win < 1 || loss < 1 || win + loss < $GAMES_LIMIT
526         delete << i
527       end
528     end
529
530     # The recursion ends if there is nothing to delete
531     return self if delete.empty?
532
533     new_obj = delete_rows(delete)
534     new_obj.filter
535   end
536
537   ##
538   # Cuts self into connecting groups such as each player in a group has at least
539   # one game with other players in the group. Returns them as an array.
540   #
541   def connected_subsets
542     g = RGL::AdjacencyGraph.new
543     (0...size).each do |k|
544       (0...size).each do |i|
545         next if k == i
546         if @matrix[k,i] > 0
547           g.add_edge(k,i)
548         end
549       end
550     end
551
552     subsets = []
553     g.each_connected_component do |c|
554       new_keys = []      
555       c.each do |v|
556         new_keys << keys[v.to_s.to_i]
557       end
558       subsets << new_keys
559     end
560
561     subsets = subsets.sort {|a,b| b.size <=> a.size}
562
563     result = subsets.collect do |keys|
564       matrix =
565         GSL::Matrix[*
566         ((0...keys.size).collect do |k|
567           p1 = @keys.index(keys[k])
568           ((0...keys.size).collect do |j|
569             if k == j
570               0
571             else
572               p2 = @keys.index(keys[j])
573               @matrix[p1,p2]
574             end
575           end)
576         end)]
577       WinLossMatrix.new(keys, matrix)
578     end
579
580     return result
581   end
582
583   def to_s
584     "size : #{@keys.size}" + "\n" +
585     @keys.inspect + "\n" + 
586     @matrix.inspect
587   end
588
589 end
590
591
592 #################################################
593 # Main methods
594 #
595
596 # Half-life effect
597 # After NHAFE_LIFE days value will get half.
598 # 0.693 is constant, where exp(0.693) ~ 0.5
599 def half_life(days)
600   if days < $options["half-life-ignore"]
601     return 1.0
602   else
603     Math::exp(-0.693/$options["half-life"]*(days-$options["half-life-ignore"]))
604   end
605 end
606
607 def _add_win_loss(winner, loser, time)
608   how_long_days = ($options["base-date"] - time)/(3600*24)
609   $players[winner] ||= Hash.new { GSL::Vector[0,0] }
610   $players[loser]  ||= Hash.new { GSL::Vector[0,0] }
611   $players[winner][loser] += GSL::Vector[1.0*half_life(how_long_days),0]
612   $players[loser][winner] += GSL::Vector[0,1.0*half_life(how_long_days)]
613 end
614
615 def _add_draw(player1, player2, time)
616   how_long_days = ($options["base-date"] - time)/(3600*24)
617   $players[player1] ||= Hash.new { GSL::Vector[0,0] }
618   $players[player2] ||= Hash.new { GSL::Vector[0,0] }
619   $players[player1][player2] += GSL::Vector[0.5*half_life(how_long_days),0.5*half_life(how_long_days)]
620   $players[player2][player1] += GSL::Vector[0.5*half_life(how_long_days),0.5*half_life(how_long_days)]
621 end
622
623 def _add_time(player, time)
624   $players_time[player] = time if $players_time[player] < time
625 end
626
627 def add(black_mark, black_name, white_name, white_mark, time)
628   if black_mark == WIN_MARK && white_mark == LOSS_MARK
629     _add_win_loss(black_name, white_name, time)
630   elsif black_mark == LOSS_MARK && white_mark == WIN_MARK
631     _add_win_loss(white_name, black_name, time)
632   elsif black_mark == DRAW_MARK && white_mark == DRAW_MARK
633     if $options["skip-draw-games"]
634       return
635     else
636       _add_draw(black_name, white_name, time)
637     end
638   else
639     raise "Never reached!"
640   end
641   _add_time(black_name, time)
642   _add_time(white_name, time)
643 end
644
645 def identify_id(id)
646   if /@NORATE\+/ =~ id # the player having @NORATE in the name should not be rated
647     return nil
648   end
649   id.gsub(/@.*?\+/,"+")
650 end
651
652 # Parse a game result line
653 #
654 def parse(line)
655   time, state, black_mark, black_id, white_id, white_mark, file = line.split("\t")
656   unless time && state && black_mark && black_id &&
657          white_id && white_mark && file
658     $stderr.puts "Failed to parse the line : #{line}"
659     return
660   end
661
662   return if state == "abnormal"
663   time = Time.parse(time)
664   return if $options["base-date"] < time
665   black_id = identify_id(black_id)
666   white_id = identify_id(white_id)
667
668   if black_id && white_id && (black_id != white_id) &&
669      black_mark && white_mark
670     add(black_mark, black_id, white_id, white_mark, time)
671   end
672 end
673
674 def validate(yaml)
675   yaml["players"].each do |group_key, group|
676     group.each do |player_key, player|
677       rate = player['rate']
678       next unless rate
679       if rate > 10000 || rate < -10000
680         return false
681       end
682     end
683   end
684   return true
685 end
686
687 def usage(io)
688     io.puts <<EOF
689 USAGE: #{$0} [options] GAME_RESULTS_FILE [...]
690        #{$0} [options]
691        
692 GAME_RESULTS_FILE:
693   a path to a file listing results of games, which is genrated by the
694   mk_game_results command.
695   In the second style above, the file content can be read from the stdin.
696
697 OPTOINS:
698   --base-date         a base time point for this calicuration (default now). Ex. '2009-10-31'
699   --half-life         n [days] (default 60)
700   --half-life-ignore  m [days] (default  7)
701                       after m days, half-life effect works
702   --fixed-rate-player player whose rate is fixed at the rate
703   --fixed-rate        rate 
704   --skip-draw-games   skip draw games. [default: draw games are counted in
705                       as 0.5 win and 0.5 lost]
706   --help              show this message
707 EOF
708 end
709
710 def main
711   $options = Hash::new
712   parser = GetoptLong.new(
713     ["--base-date",         GetoptLong::REQUIRED_ARGUMENT],
714     ["--half-life",         GetoptLong::REQUIRED_ARGUMENT],
715     ["--half-life-ignore",  GetoptLong::REQUIRED_ARGUMENT],
716     ["--help", "-h",        GetoptLong::NO_ARGUMENT],
717     ["--fixed-rate-player", GetoptLong::REQUIRED_ARGUMENT],
718     ["--fixed-rate",        GetoptLong::REQUIRED_ARGUMENT],
719     ["--skip-draw-games",   GetoptLong::NO_ARGUMENT])
720   parser.quiet = true
721   begin
722     parser.each_option do |name, arg|
723       name.sub!(/^--/, '')
724       $options[name] = arg.dup
725     end
726     if ( $options["fixed-rate-player"] && !$options["fixed-rate"]) ||
727        (!$options["fixed-rate-player"] &&  $options["fixed-rate"]) ||
728        ( $options["fixed-rate-player"] &&  $options["fixed-rate"].to_i <= 0) 
729       usage($stderr)
730       exit 1
731     end
732   rescue
733     usage($stderr)
734     raise parser.error_message
735   end
736   if $options["help"]
737     usage($stdout) 
738     exit 0
739   end
740   if $options["base-date"]
741     $options["base-date"] = Time::parse $options["base-date"]
742   else
743     $options["base-date"] = Time.now
744   end
745   $options["half-life"] ||= 60
746   $options["half-life"] = $options["half-life"].to_i
747   $options["half-life-ignore"] ||= 7
748   $options["half-life-ignore"] = $options["half-life-ignore"].to_i
749   $options["fixed-rate"] = $options["fixed-rate"].to_i if $options["fixed-rate"]
750
751   if ARGV.empty?
752     while line = $stdin.gets do
753       parse line.strip
754     end
755   else
756     while file = ARGV.shift do
757       File.open(file) do |f|
758         f.each_line do |line|
759           parse line.strip
760         end
761       end 
762     end
763   end
764
765   yaml = {} 
766   yaml["players"] = {}
767   rating_group = 0
768   if $players.size > 0
769     obj = WinLossMatrix::mk_win_loss_matrix($players)
770     obj.connected_subsets.each do |win_loss_matrix|
771       yaml["players"][rating_group] = {}
772
773       rating = Rating.new(win_loss_matrix.matrix)
774       rating.rating
775       rating.average!(Rating::AVERAGE_RATE)
776       rating.integer!
777
778       if $options["fixed-rate-player"]
779         # first, try exact match
780         index = win_loss_matrix.keys.index($options["fixed-rate-player"])
781         # second, try regular match
782         unless index
783           win_loss_matrix.keys.each_with_index do |p, i|
784             if %r!#{$options["fixed-rate-player"]}! =~ p
785               index = i
786             end
787           end
788         end
789         if index
790           the_rate = rating.rate[index]
791           rating.translate!($options["fixed-rate"] - the_rate)
792         end
793       end
794
795       win_loss_matrix.keys.each_with_index do |p, i| # player_id, index#
796         win  = win_loss_matrix.matrix.row(i).sum
797         loss = win_loss_matrix.matrix.col(i).sum
798
799         yaml["players"][rating_group][p] = 
800           { 'name' => p.split("+")[0],
801             'rating_group' => rating_group,
802             'rate' => rating.rate[i],
803             'last_modified' => $players_time[p].dup,
804             'win'  => win,
805             'loss' => loss}
806       end
807       rating_group += 1
808     end
809   end
810   rating_group -= 1
811   non_rated_group = 999 # large enough
812   yaml["players"][non_rated_group] = {}
813   $players.each_key do |id|
814     # skip players who have already been rated
815     found = false
816     (0..rating_group).each do |i|
817        found = true if yaml["players"][i][id]
818        break if found
819     end
820     next if found
821
822     v = GSL::Vector[0, 0]
823     $players[id].each_value {|value| v += value}
824     next if v[0] < 1 && v[1] < 1
825
826     yaml["players"][non_rated_group][id] =
827       { 'name' => id.split("+")[0],
828         'rating_group' => non_rated_group,
829         'rate' => 0,
830         'last_modified' => $players_time[id].dup,
831         'win'  => v[0],
832         'loss' => v[1]}
833   end
834   unless validate(yaml)
835     $stderr.puts "Aborted. It did not result in valid ratings."
836     $stderr.puts yaml.to_yaml if $DEBUG
837     exit 10
838   end
839   puts yaml.to_yaml
840 end
841
842 if __FILE__ == $0
843   main
844 end
845
846 # vim: ts=2 sw=2 sts=0