Class: SatoriLikeDictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/satori_like_dictionary.rb

Overview

Satori like dictionary for Ukagaka SHIORI subsystem

Defined Under Namespace

Modules: Renderer Classes: Call, ChangeScopeLine, Code, Entries, Entry, Jump, Line, TemplateContext, Word

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(events = nil) ⇒ SatoriLikeDictionary

initialize SatoriLikeDictionary

Parameters:

  • events (Events) (defaults to: nil)

    events definition (if nil then use the dictionary itself)



10
11
12
13
# File 'lib/satori_like_dictionary.rb', line 10

def initialize(events=nil)
  @dictionary = OpenStruct.new
  @events = events || @dictionary
end

Instance Attribute Details

#dictionaryObject (readonly)

Returns the value of attribute dictionary.



6
7
8
# File 'lib/satori_like_dictionary.rb', line 6

def dictionary
  @dictionary
end

Instance Method Details

#aitalk(request) ⇒ String|OpenStruct

call the “ai talk” entry

Parameters:

  • request (OpenStruct)

    request hash

Returns:

  • (String|OpenStruct)

    result



92
93
94
# File 'lib/satori_like_dictionary.rb', line 92

def aitalk(request)
  talk("", request)
end

#load(file) ⇒ Object

load a file as satori like dictionary

Parameters:

  • file (String)

    path to file



25
26
27
# File 'lib/satori_like_dictionary.rb', line 25

def load(file)
  parse(File.read(file))
end

#load_all(directory, ext = 'txt') ⇒ Object

load all files in a directory as satori like dictionaries

Parameters:

  • directory (String)

    path to dictionary

  • ext (String) (defaults to: 'txt')

    file extension filter



18
19
20
21
# 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) ⇒ Object

parse and register satori like dictionary source

Parameters:

  • source (String)

    satori like dictionary source



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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) ⇒ String|OpenStruct

call a named entry

Parameters:

  • entry_name (String)

    entry name

  • request (OpenStruct)

    request hash

Returns:

  • (String|OpenStruct)

    result



81
82
83
84
85
86
87
# 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