class TaggedContent::TagExtractor

Constants

CREDITABLE

Tags that can earn CPD credit

LEARNING_LOG

Tags that are used in learning logs

SECTIONS

Tags that are applied to content based on the position in the document

SIG_EVENT

Tags that are used in significant events

TAGS

Tags that are part of the DSL and are recognized in text

Attributes

section[RW]
tags[R]

Public Class Methods

all_tags() click to toggle source
# File lib/docfolio/paragraph_modules/tags.rb, line 26
def self.all_tags
  SECTIONS + TAGS
end
new() click to toggle source
# File lib/docfolio/paragraph_modules/tags.rb, line 8
def initialize
  @tags =  []
end
reset() click to toggle source

resets the class variables so that a new file can be parsed is called by LearningDiary (through Paragraph) when preparing to parse a new txt file

# File lib/docfolio/paragraph_modules/tags.rb, line 22
def self.reset
  TagExtractor.section = 0 # :TITLE
end

Public Instance Methods

content(tag, str = '') click to toggle source

Joins all content in @tags of with a tag of type tag @param [Symbol] tag The tag for which content that should be selected. @param [String] str An optional string that can be passed to the function

to which selected content will be appended.
# File lib/docfolio/paragraph_modules/tags.rb, line 59
def content(tag, str = '')
  tag_index = 0
  content_index = 1
  @tags.each { |t| str << t[content_index] + ' ' if t[tag_index] == tag }
  str
end
creditable?() click to toggle source

true if the paragraph contains a tag that can earn credit

# File lib/docfolio/paragraph_modules/tags.rb, line 67
def creditable?
  tags.each { |t| return true if CREDITABLE.include?(t[0]) }
  false
end
extract_content(rest_of_str) click to toggle source
# File lib/docfolio/paragraph_modules/tags.rb, line 30
def extract_content(rest_of_str)
  # if a new date or time has not been found then return
  return if rest_of_str == ''

  @old_tags = @tags
  @tags += extract_tags(rest_of_str)

  if tags_extracted?
    # As soon as tags are extracted, there can only be note internal
    # paragraph sections
    TagExtractor.section = 2 #:NOTE
  else
    # No tags have been extracted from the str, so use the paragraphs
    # current section
    tag_as_section(rest_of_str)
  end
end
impact_creditable?() click to toggle source

true if the paragraph contains a tag that can earn impact credit

# File lib/docfolio/paragraph_modules/tags.rb, line 80
def impact_creditable?
  tags.each { |t| return true if t[0] == :I }
  false
end
significant_event?() click to toggle source

true if the paragraph contains a tag used in significant events i.e. is the learning diary a significant event?

# File lib/docfolio/paragraph_modules/tags.rb, line 74
def significant_event?
  tags.each { |t| return true if SIG_EVENT.include?(t[0]) }
  false
end
tag?(tag) click to toggle source

returns true if any tags are of type tag @param [Array] tag An array of tags

# File lib/docfolio/paragraph_modules/tags.rb, line 50
def tag?(tag)
  @tags.each { |t| return true if t[0] == tag }
  false
end

Private Instance Methods

extract_tag(paragraph_text) click to toggle source

Splits the paragraph into an array of tags of type [:tag, 'content'] @param [String] paragraph_text String text of the paragraph with any

date and time info at the beginning, removed

@return [Array] Tagged array of content with elements of type

[:tag, 'content']
# File lib/docfolio/paragraph_modules/tags.rb, line 170
def extract_tag(paragraph_text)
  a = paragraph_text.split(tag_regex)
  preface_with_note(a) + tags_array(a)
end
extract_tags(paragraph_text) click to toggle source

Extracts a paragraph string to a tagged array with elements of type [:tag, 'content']. Called after the date/time info has been removed. If called before, will result in date info at the start being tagged as a :NOTE @param [String] paragraph_text Paragraph string after date info removed @return [Array] Tagged array

# File lib/docfolio/paragraph_modules/tags.rb, line 108
def extract_tags(paragraph_text)
  tag_regex =~ paragraph_text ? extract_tag(paragraph_text) : []
end
preface_with_note(a) click to toggle source

Paragraphs are broken down into a tagged array, with elements of type [:tag, 'text']. The first element of an array is of type string. If the paragraph begins with text before any tags, then this first element will contain this text, otherwise it will be an empty string. This function tests this string and returns it tagged as :NOTE unless empty in which case it returns an empty array [] @param [Array] a An array of tagged content of the paragraph @return [Array] Another tagged array which is either empty, or just

contains a single tagged content of the text at the beginning if there
was any.
# File lib/docfolio/paragraph_modules/tags.rb, line 142
def preface_with_note(a)
  str = a[0].strip
  str == '' ? [] : [[:NOTE, str]]
end
section_tag() click to toggle source
# File lib/docfolio/paragraph_modules/tags.rb, line 190
def section_tag
  SECTIONS[TagExtractor.section]
end
tag_as_section(str) click to toggle source

Add a single tagged content element of type

:symbol (tag), String (content)

to the @tags instance instance variable

using the current value of the section class instance variable as an index to reference the correct section tag symbol from the SECTIONS array. Does not tags content identified in the TAGS array. @param [String] str the content to tag

# File lib/docfolio/paragraph_modules/tags.rb, line 186
def tag_as_section(str)
  tag_it(section_tag, str)
end
tag_it(tag, p) click to toggle source

Add a single tagged content element of type

:symbol (tag), String (content)

to the @tags instance instance variable

. Move the section class instance variable up one (to current :INTRO) if it is at position 0 (currently :TITLE) @param [String] p the content to tag @param [Symbol] tag the tag to use

# File lib/docfolio/paragraph_modules/tags.rb, line 118
def tag_it(tag, p)
  @tags << [tag, p]
  if  TagExtractor.section == 0 &&
      TagExtractor.section != (SECTIONS.count - 1)
    TagExtractor.section += 1
  end
end
tag_regex() click to toggle source

Creates a regex that can be used to match for tags that are recognized as part of the DSL, currently :LP, :R, :DEN, :NOTE and :I

# File lib/docfolio/paragraph_modules/tags.rb, line 128
def tag_regex
  /\b(#{ TAGS.join '|' }): ?/
end
tags_array(a) click to toggle source

Turns an array of strings into a tagged array. Ignores the string variable at position [0]. This first string is turned into a tagged array element by the function preface_with_note and appended in extract_tag @param [Array] a Array of strings from splitting the paragraph @return [Array] A tagged array with elements of the form

[:tag, 'content']
# File lib/docfolio/paragraph_modules/tags.rb, line 154
def tags_array(a)
  taggs = []
  tag_count = (a.count - 1) / 2
  1.upto(tag_count) do |i|
    tag = a[(i * 2) - 1].to_sym
    content = a[i * 2].strip
    taggs << [tag, content]
  end
  taggs
end
tags_extracted?() click to toggle source

@return [Boolean] True if any tags have been extracted

# File lib/docfolio/paragraph_modules/tags.rb, line 176
def tags_extracted?
  @old_tags.count < @tags.count
end