
module Botl

  class BackElement

    attr_accessor :is_variable
    attr_accessor :is_optional
    attr_accessor :words
    attr_accessor :variable_name
    attr_accessor :source

    def self.create_with_word s


      belement = BackElement.new

      belement.source = s

      # puts ">>> #{s}"
      w=nil

      if s.start_with?("[") && s.end_with?("]")
        belement.is_optional = true
        w = s[1...-1]
      elsif s.start_with?("{") && s.end_with?("}")

        belement.is_variable = true
        w = s[1...-1]

        belement.variable_name = w
      else
        w = s
      end


      # w = LocalDictionary.replace(w) unless belement.is_variable



      if w.include? "_or_"
        belement.words = w.split("_or_")
        belement.words = belement.words.collect{|x|x.strip.stem}
      else
        # puts "* " + w.strip + " *"
        belement.words = [w.strip.stem]
        # puts "* " + belement.words.to_s + " *"
      end
      #

      belement
    end

    def to_s
      if self.is_variable
        return "{#{variable_name}}" + (self.words.count > 1 ? " <#{self.words.join(", ")}>" : "")
      else
        x = self.words.join("_or_")
        x = "[#{x}]" if self.is_optional
        return x
      end
    end


    def match back_word

      # puts "* words: #{self.words}"
      xmatch = false
      self.words.each do |x|



        xmatch = true if x == back_word.stemmed

        # puts "* match?: #{x} <=> #{back_word.stemmed} => #{xmatch}"

        return true if xmatch

      end

      xmatch
    end

  end


end
