Class: CooCoo::DataSources::Xournal::TrainingDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/coo-coo/data_sources/xournal/training_document.rb,
lib/coo-coo/data_sources/xournal/training_document/sets.rb,
lib/coo-coo/data_sources/xournal/training_document/example.rb,
lib/coo-coo/data_sources/xournal/training_document/constants.rb,
lib/coo-coo/data_sources/xournal/training_document/document_maker.rb,
lib/coo-coo/data_sources/xournal/training_document/document_reader.rb

Overview

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.

Defined Under Namespace

Classes: DocumentMaker, DocumentReader, Example

Constant Summary collapse

VERSION =
'1'
GRID_COLOR =

FIXME Xournal places alpha up front

'#00E0FFFF'
PARSED_GRID_COLOR =
ChunkyPNG::Color.parse(GRID_COLOR)
META_LABEL =
"Training Document"
META_LABEL_REGEX =
/^#{META_LABEL}( +\d+)?: *(\d+)( +\d+)( +\d+)?/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(examples = nil) ⇒ TrainingDocument

Returns a new instance of TrainingDocument.

Parameters:



18
19
20
# 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

Instance Attribute Details

#examplesObject (readonly)

Returns the value of attribute examples.



15
16
17
# File 'lib/coo-coo/data_sources/xournal/training_document.rb', line 15

def examples
  @examples
end

Class Method Details

.ascii_trainer(doc = self.new) ⇒ TrainingDocument

Create a CooCoo::DataSources::Xournal::TrainingDocument for ASCII characters.

Parameters:

  • doc (Document) (defaults to: self.new)

    Optional Document used to extract examples.

Returns:



8
9
10
11
12
13
14
15
# 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) ⇒ TrainingDocument

Parameters:

  • limit (Integer) (defaults to: 2000)

    Optional limit on how many characters to include.

  • doc (Document) (defaults to: self.new)

    Optional Document used to extract examples.

Returns:



45
46
47
# 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) ⇒ TrainingDocument

Parameters:

  • doc (Document) (defaults to: self.new)

    Optional Document used to extract examples.

Returns:



52
53
54
55
# 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) ⇒ TrainingDocument

Parameters:

Returns:



72
73
74
# 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) ⇒ TrainingDocument

Parameters:

  • io_or_path (IO, String)

Returns:



65
66
67
# 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) ⇒ TrainingDocument

Create a CooCoo::DataSources::Xournal::TrainingDocument for Japanese Hiragana, Katakana, and punctuation.

Parameters:

  • doc (Document) (defaults to: self.new)

    Optional Document used to extract examples.

Returns:



33
34
35
36
37
38
39
# 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) ⇒ TrainingDocument

Parameters:

  • doc (Document) (defaults to: self.new)

    Optional Document used to extract examples.

Returns:



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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

.unicode_trainer(starting_offset, number, doc = self.new) ⇒ TrainingDocument

Create a CooCoo::DataSources::Xournal::TrainingDocument for an arbitrary Unicode block.

Parameters:

  • starting_offset (Integer)

    Which Unicode character to start the examples

  • number (Integer)

    The number of characters to place in the document.

  • doc (Document) (defaults to: self.new)

    Optional Document used to extract examples.

Returns:



22
23
24
25
26
27
28
# 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

Instance Method Details

#add_example(label, strokes = nil) ⇒ Object

Add an example to the set.

Parameters:

  • label (String)

    The label of the example.

  • strokes (Array<Stroke>) (defaults to: nil)

    Strokes associated with this label.

Returns:

  • self



36
37
38
39
40
# 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) ⇒ Enumerator

Iterates each Example.

Returns:



44
45
46
47
48
49
50
# 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

#labelsArray<String>

Returns of every example’s label.

Returns:

  • (Array<String>)

    of every example’s label



28
29
30
# File 'lib/coo-coo/data_sources/xournal/training_document.rb', line 28

def labels
  @examples.keys
end

#sizeInteger

Returns Number of examples.

Returns:

  • (Integer)

    Number of examples



23
24
25
# 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) ⇒ Document

Convert the Example set into a Document.

Parameters:

  • columns (Integer)

    Number of examples across the page.

  • rows (Integer)

    Number of examples down the page.

  • page_width (Float) (defaults to: 612)

    Width of the page in points.

  • page_height (Float) (defaults to: 792)

    Height of the page in points.

Returns:



58
59
60
# 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