Class: Lm::POS

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

Overview

definitions A = factor AB = term AB+C = sum (AB + C)(D + E) = product of sums AB+C,D+E

Instance Method Summary collapse

Constructor Details

#initialize(str) ⇒ POS

Returns a new instance of POS.



12
13
14
# File 'lib/lm/pos.rb', line 12

def initialize(str)
  @str = str
end

Instance Method Details

#apply_factorizeObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lm/pos.rb', line 38

def apply_factorize
  newarray = []
  seenkeys = Set.new

  termhash.each do |key, value|
    next if seenkeys.include?(([key] + value.to_a).sort.join("|").to_s)

    seenkeys << ([key] + value.to_a).sort.join("|")
    newarray << "#{key}+#{value.map { |x| Sum.new(x) }.inject(:multiply)}"
  end

  POS.new(newarray.join(","))
end

#expandObject

return SOP



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lm/pos.rb', line 53

def expand
  # puts "EXPAND"
  arr = sum_array

  loop do
    break if arr.length == 1

    aterms = arr[0].split("+")
    bterms = arr[1].split("+")

    newterms = aterms.product(bterms).map do |x|
      x.join("").split("").uniq.join("")
    end

    newsum = newterms.join("+")

    arr = [newsum] + arr[2..]
    # p arr
  end

  Sum.new(arr.first)
end

#sum_arrayObject



16
17
18
# File 'lib/lm/pos.rb', line 16

def sum_array
  @sum_array ||= @str.split(",")
end

#termhashObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lm/pos.rb', line 20

def termhash
  @termhash ||= begin
    res = {}
    sum_array.each do |sum|
      terms = sum.split("+", 2)
      terms.each do |term|
        res[term] ||= Set.new
        terms.each do |x|
          next if x == term

          res[term] << x
        end
      end
    end
    res
  end
end