read-database.rb 746 B

123456789101112131415161718192021222324252627282930313233343536
  1. # Read database of games and query
  2. require 'chess-data'
  3. $total_games = 0
  4. $total_selected = 0
  5. # read in pgn files provided on command line
  6. ARGV.each do |file|
  7. puts "Reading from #{file}"
  8. # create a database
  9. database = ChessData::Database.new
  10. database.add_games_from file
  11. # display info
  12. puts "Read #{database.size} games"
  13. # extract those games which at some point reach given position definition
  14. selected = database.search do
  15. exactly 1, "R", "r"
  16. exactly 5, "P"
  17. exactly 4, "p"
  18. end
  19. # report and save result
  20. puts "Found #{selected.size} games"
  21. selected.to_file "selected.pgn"
  22. $total_games += database.size
  23. $total_selected += selected.size
  24. end
  25. puts "Selected #{$total_selected} out of #{$total_games}"