class CooCoo::DataSources::Xournal::TrainingDocument

The {TrainingDocument} is the source of strokes for the trainer of the Xournal recognizer. Each {TrainingDocument} has a set of labels and associated strokes. Examples are loaded and stored to Xournal documents formatted into a grid with a label and strokes in each cell.

Constants

GRID_COLOR
META_LABEL
META_LABEL_REGEX
PARSED_GRID_COLOR
VERSION

Attributes

examples[R]

Public Class Methods

ascii_trainer(doc = self.new) click to toggle source

Create a {TrainingDocument} for ASCII characters. @param doc [Document] Optional Document used to extract examples. @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document/sets.rb, line 8
def self.ascii_trainer(doc = self.new)
  (32...127).each do |c|
    c = c.chr[0]
    doc.add_example(c, [])
  end

  doc
end
cjk_trainer(limit = 2000, doc = self.new) click to toggle source

Create a {TrainingDocument} for the CJK block. @param limit [Integer] Optional limit on how many characters to include. @param doc [Document] Optional Document used to extract examples. @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document/sets.rb, line 45
def self.cjk_trainer(limit = 2000, doc = self.new)
  unicode_trainer(0x4e00, limit)
end
emoji_trainer(doc = self.new) click to toggle source

Create a {TrainingDocument} for emoji. @param doc [Document] Optional Document used to extract examples. @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document/sets.rb, line 52
def self.emoji_trainer(doc = self.new)
  unicode_trainer(0x1F600, 16 * 5, doc)
  unicode_trainer(0x2700, 16 * 12, doc)
end
from_document(doc) click to toggle source

Load a {TrainingDocument} from a {Document}. @param doc [Document] @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 72
def self.from_document(doc)
  DocumentReader.new.load(doc)
end
from_file(io_or_path) click to toggle source

Load {TrainingDocument} from a Xournal file. @param io_or_path [IO, String] @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 65
def self.from_file(io_or_path)
  DocumentReader.new.load(Xournal.from_file(io_or_path))
end
jp_trainer(doc = self.new) click to toggle source

Create a {TrainingDocument} for Japanese Hiragana, Katakana, and punctuation. @param doc [Document] Optional Document used to extract examples. @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document/sets.rb, line 33
def self.jp_trainer(doc = self.new)
  unicode_trainer(0x3000, 64, doc)
  unicode_trainer(0x3040, 96, doc)
  unicode_trainer(0x30A0, 96, doc)
  unicode_trainer(0xff00, 16 * 15, doc)
  doc
end
math_trainer(doc = self.new) click to toggle source

Create a {TrainingDocument} for math symbols. @param doc [Document] Optional Document used to extract examples. @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document/sets.rb, line 60
def self.math_trainer(doc = self.new)
  unicode_trainer(0x2200, 16 * 16, doc)
  unicode_trainer(0x2A00, 16 * 16, doc)
  unicode_trainer(0x2100, 16 * 5, doc)
  unicode_trainer(0x27C0, 16 * 3, doc)
  unicode_trainer(0x2980, 16 * 8, doc)
  unicode_trainer(0x2300, 16 * 16, doc)
  unicode_trainer(0x25A0, 16 * 6, doc)
  unicode_trainer(0x2B00, 16 * 16, doc)
  unicode_trainer(0x2190, 16 * 7, doc)
  unicode_trainer(0x2900, 16 * 8, doc)
  unicode_trainer(0x1D400, 16 * 16 * 4, doc)
end
new(examples = nil) click to toggle source

@param examples [Array<Example>]

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 18
def initialize(examples = nil)
  @examples = examples || Hash.new { |h, k| h[k] = Example.new(k) }
end
unicode_trainer(starting_offset, number, doc = self.new) click to toggle source

Create a {TrainingDocument} for an arbitrary Unicode block. @param starting_offset [Integer] Which Unicode character to start the examples @param number [Integer] The number of characters to place in the document. @param doc [Document] Optional Document used to extract examples. @return [TrainingDocument]

# File lib/coo-coo/data_sources/xournal/training_document/sets.rb, line 22
def self.unicode_trainer(starting_offset, number, doc = self.new)
  number.times do |i|
    doc.add_example("" << (starting_offset + i), [])
  end

  doc
end

Public Instance Methods

add_example(label, strokes = nil) click to toggle source

Add an example to the set. @param label [String] The label of the example. @param strokes [Array<Stroke>] Strokes associated with this label. @return self

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 36
def add_example(label, strokes = nil)
  ex = @examples[label]
  ex.add_set(strokes) if strokes && !strokes.empty?
  self
end
each_example(&block) click to toggle source

Iterates each {Example}. @return [Enumerator]

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 44
def each_example(&block)
  return to_enum(__method__) unless block_given?

  @examples.each do |label, ex|
    block.call(ex)
  end
end
labels() click to toggle source

@return [Array<String>] of every example's label

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 28
def labels
  @examples.keys
end
size() click to toggle source

@return [Integer] Number of examples

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 23
def size
  @examples.size
end
to_document(columns, rows, cells_per_example = 4, page_width = 612, page_height = 792) click to toggle source

Convert the {Example} set into a {Document}. @param columns [Integer] Number of examples across the page. @param rows [Integer] Number of examples down the page. @param page_width [Float] Width of the page in points. @param page_height [Float] Height of the page in points. @return [Document]

# File lib/coo-coo/data_sources/xournal/training_document.rb, line 58
def to_document(columns, rows, cells_per_example = 4, page_width = 612, page_height = 792)
  DocumentMaker.new(self, columns, rows, cells_per_example, page_width, page_height).make_document
end