class SatoriLikeDictionary

Satori like dictionary for Ukagaka SHIORI subsystem

Attributes

dictionary[R]

Public Class Methods

new(events=nil) click to toggle source

initialize SatoriLikeDictionary @param [Events] events events definition (if nil then use the dictionary itself)

# File lib/satori_like_dictionary.rb, line 10
def initialize(events=nil)
  @dictionary = OpenStruct.new
  @events = events || @dictionary
end

Public Instance Methods

aitalk(request) click to toggle source

call the “ai talk” entry @param [OpenStruct] request request hash @return [String|OpenStruct] result

# File lib/satori_like_dictionary.rb, line 92
def aitalk(request)
  talk("", request)
end
load(file) click to toggle source

load a file as satori like dictionary @param [String] file path to file

# File lib/satori_like_dictionary.rb, line 25
def load(file)
  parse(File.read(file))
end
load_all(directory, ext = 'txt') click to toggle source

load all files in a directory as satori like dictionaries @param [String] directory path to dictionary @param [String] ext file extension filter

# File lib/satori_like_dictionary.rb, line 18
def load_all(directory, ext = 'txt')
  files = Dir[File.join(directory, "*.#{ext}")]
  files.each {|file| parse(File.read(file))}
end
parse(source) click to toggle source

parse and register satori like dictionary source @param [String] source satori like dictionary source

# File lib/satori_like_dictionary.rb, line 31
def parse(source)
  scope = :comment
  current_entry = nil
  entry_name = nil
  source.each_line do |line|
    line = line.chomp
    if line.start_with?("#")
      # skip comment line (incompatible with satori original)
    elsif line.start_with?("*")
      scope = :entry
      entry_name = linebody(line).strip
      current_entry = Entry.new
      @dictionary[entry_name] ||= Entries.new
      @dictionary[entry_name] << current_entry
    elsif line.start_with?("@")
      scope = :word_entry
      entry_name = linebody(line).strip
    else
      if scope == :entry
        if line.start_with?("$") # incompatible with satori original ($ruby code (same as %)
          current_entry << Code.new(linebody(line))
        elsif line.start_with?(":")
          current_entry << ChangeScopeLine.new(linebody(line))
        elsif line.start_with?(">")
          current_entry << Jump.new(linebody(line))
        elsif line.start_with?("→") # incompatible with satori original (→キャラ名
          current_entry << Call.new(linebody(line))
        elsif line.start_with?("≫")
          $stderr.puts "警告: ≫は実装されていません。スキップします。"
        else
          current_entry << Line.new(line)
        end
      elsif scope == :word_entry
        if line.start_with?("$", ">", "≫", "→")
          $stderr.puts "警告: @の中で行頭#{line[0]}が使われています。文字列として解釈されます。"
        end
        unless line.strip.empty? # skip empty line
          @dictionary[entry_name] ||= Entries.new
          @dictionary[entry_name] << Word.new(line)
        end
      end
      # skip :comment scope
    end
  end
end
talk(entry_name, request) click to toggle source

call a named entry @param [String] entry_name entry name @param [OpenStruct] request request hash @return [String|OpenStruct] result

# File lib/satori_like_dictionary.rb, line 81
def talk(entry_name, request)
  if entries = @dictionary[entry_name]
    entries.render(@events, request)
  else
    nil
  end
end

Private Instance Methods

linebody(line) click to toggle source

line @param [String] line line @return [String] line

# File lib/satori_like_dictionary.rb, line 286
def linebody(line)
  line[1...line.size]
end