Class: DICTIONARY

Inherits:
ENCODER show all
Defined in:
lib/DICTIONARY.rb

Overview

Dictionary

Instance Method Summary collapse

Methods inherited from ENCODER

#filter, #hotDecodeCharacter, #hotDecodeSentance, #hotEncodeCharacter, #hotEncodeSentance, #listFilesInDirectory, #matrixToNArray, #nArrayToMatrix, #readPlaintextFilesInDirectory, #readPlaintextfile, #stringDifferencePercent

Instance Method Details

#arrayToBinary(array) ⇒ Object



78
79
80
81
82
# File 'lib/DICTIONARY.rb', line 78

def arrayToBinary(array)
  binaryString = array.to_a.join("")
  binary = binaryString.to_i(2)
  return binary
end

#binaryToArray(binary, x_dim) ⇒ Object



72
73
74
75
76
77
# File 'lib/DICTIONARY.rb', line 72

def binaryToArray(binary, x_dim)
  binaryFormatted = ((0b1 << (x_dim+1) ^ binary*2)).to_s(2)[1, x_dim]
  binaryArray = DFloat.zeros(1, x_dim)
  binaryArray = binaryFormatted.split("").map(&:to_i)
  return binaryArray
end

#decodeArray(encodedArray, hash = @encodingToString) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/DICTIONARY.rb', line 83

def decodeArray(encodedArray, hash=@encodingToString)
  output = ""
  for i in 0...encodedArray.shape()[0]
    decodedWord = hash[arrayToBinary(encodedArray[i, true])]
    if decodedWord != nil
      output += decodedWord + " "
    else 
      output += "@ "
    end
  end
  return output
end

#decodeArrayByMaximum(encodedArray, hash = @encodingToString) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/DICTIONARY.rb', line 95

def decodeArrayByMaximum(encodedArray, hash=@encodingToString)
  output = ""
  for i in 0..encodedArray.shape()[0]-1
    if encodedArray[i, true].to_a.max != 0
      max_element = encodedArray[i, true].to_a.each_with_index.max[1]
      encodedArrayBW = Int32.zeros(encodedArray[i, true].shape())
      encodedArrayBW[max_element] = 1
      decodedWord = hash[arrayToBinary(encodedArrayBW)]
      if decodedWord != nil
        output += decodedWord + " "
      else 
        output += "@ "
      end
    else 
      output += "$ "
    end
  end
  return output
end

#encodeArray(wordArray, hash = @stringToEncoding) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/DICTIONARY.rb', line 57

def encodeArray(wordArray, hash=@stringToEncoding)
  if wordArray.instance_of? String
    wordArray = wordArray.split()
  end 
  array = Int32.zeros(wordArray.length(), @x_dim)
  for i in 0...wordArray.length()
    word = wordArray[i]
    encoding = hash[word]
    if encoding != nil
      binaryArray = binaryToArray(encoding, @x_dim)
      array[i, true] = binaryArray
    end
  end
  return array
end

#getEncodingHashsObject



20
21
22
# File 'lib/DICTIONARY.rb', line 20

def getEncodingHashs()
  return @stringToEncoding, @encodingToString
end

#getFrequencyHashObject



17
18
19
# File 'lib/DICTIONARY.rb', line 17

def getFrequencyHash()
  return @frequencyHash
end

#hotEncodeVocabulary(hash, maxEntries) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/DICTIONARY.rb', line 39

def hotEncodeVocabulary(hash, maxEntries)
  stringToEncoding = {}
  encodingToString = {}
  binary = 0b1
  hash.each do |key, value|
    stringToEncoding["#{key}"] = binary
    encodingToString[binary] = "#{key}"
    binary = binary << 1
    if (binary) == (0b1 << maxEntries)
      break;
    end
  end
  return stringToEncoding, encodingToString
end

#init(string, x_dim) ⇒ Object



12
13
14
15
16
# File 'lib/DICTIONARY.rb', line 12

def init(string, x_dim)
  @x_dim = x_dim
  @frequencyHash = wordFrequency(string)
  @stringToEncoding, @encodingToString = hotEncodeVocabulary(@frequencyHash, x_dim)
end

#readFileDataArray(fileDataArray, fileIndex, start, input_len, predict_len) ⇒ Object



53
54
55
56
# File 'lib/DICTIONARY.rb', line 53

def readFileDataArray(fileDataArray, fileIndex, start, input_len, predict_len)
  fileWordArray = (fileDataArray[fileIndex]).split
  return fileWordArray[start, input_len], fileWordArray[(start+predict_len), (input_len)]
end

#viewHash(hash) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/DICTIONARY.rb', line 114

def viewHash(hash)
  puts "key -> value"
  entries = 0
  hash.each do |key, value|
    puts key.to_s + " -> " + value.to_s
    entries += 1
  end
  puts "Entires in hash: " + entries.to_s
end

#wordFrequency(words) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/DICTIONARY.rb', line 23

def wordFrequency(words)
  if words.is_a?(String)
    words = words.split
  end
  frequency = {}
  entries = 0
  for i in 0...words.length()
    if frequency[words[i]] == nil
      frequency[words[i]] = 1
      entries += 1
    else
      frequency[words[i]] = frequency[words[i]] + 1
    end
  end
  return frequency.sort_by {|k, v| v}.reverse
end