#!/usr/bin/ruby -Ke # -*- Encoding: EUC-JP -*- # Nikkei-Puzzle.rb # This script solve "Jukugo Sakusei" puzzles on Nikkei Newspaper (evening paper) # using word dictionary included alt-cannadic. # # Author:: FUJIWARA Teruyoshi # Copyright:: Copyright (c) 2013 FUJIWARA Teruyoshi # License:: Distributes under the same terms as Ruby # Date:: November 8, 2013 # Version:: 1.1 # # == Requirement # none # # == Usage # 1. Get alt-cannadic from its website (see below) and extract the archive. # 2. Modify "Define a quiz..." section below according to a quiz on a newspaper. # 3. Run this script as: ./nikkei-puzzle.rb alt-cannadic-110208/gcanna.ctd # # This script show candidates of the answer. # # == See Also # * alt-cannadic: http://sourceforge.jp/projects/alt-cannadic/ # # Define a quiz using regular expression # arg_known_unknown = ["金", "声", "配", "難"] # 2ji-jukugo which 2nd char is hidden, like "金□" arg_unknown_known = ["恋", "紙", "素", "気"] # 2ji-jukugo which 1st char is hidden, like "□金" # Threshold to filter output. Output is printed only when # strings are found more than threshold = 3 # # make RE from arguments # known_unknown = Array::new unknown_known = Array::new arg_known_unknown.each do |s| known_unknown.push("\(#{s}\)\(.\)") end arg_unknown_known.each do |s| unknown_known.push("\(.\)\(#{s}\)") end # # Filter dictionary and make hash # h = Hash::new while s = gets dict_items = s.split(/\s+/) next if dict_items[2] =~ /[\p{hiragana}ー]/ || dict_items[2] =~ /[\p{katakana}ー]/ next unless dict_items[2] =~ /^..$/ known_unknown.each do |p| if dict_items[2] =~ /#{p}/ h[$2] = Array::new unless h.key?($2) h[$2].unshift($1) end end unknown_known.each do |p| if dict_items[2] =~ /#{p}/ h[$1] = Array::new unless h.key?($1) h[$1].unshift($2) end end end # # output result # h.sort {|(k1, v1), (k2, v2)| v1.length <=> v2.length }.reverse.each do |k,v| v.uniq! print "#{k}/#{v}\n" if v.length >= threshold end