|
- require "json"
- require "csv"
-
- sentences = ['abcdefg',
- 'abcdhfg',
- 'abqwerty',
- 'abqwerty']
-
-
- def maker charlist, index, data, trie
- if index == (charlist.length)
- return trie
- end
-
- exists = trie.filter {|n| n.first == charlist[index] }.first
-
- node = (! exists) ? [
- charlist[index], # symbol,
- [], # items
- ] : exists
-
- node[1] = maker charlist, index + 1, data, node[1]
-
- if data && index == (charlist.length - 1)
- if node[2]
- node[2] << data
- else
- node[2] = [data]
- end
- end
-
- if ! exists
- trie << node
- end
-
- return trie
- end
-
-
- def fact value, data, trie
- chars = value.to_s.chars
- maker chars, 0, data, trie
- end
-
-
- initial_trie = []
-
- sentences.each_with_index do |sentence, idx|
- intial_trie = fact sentence, idx, initial_trie
- end
-
- pp initial_trie
-
- initial_trie = []
-
- File.readlines("/home/luka/Databases/aries/dissolver/db.csv").each_with_index do |row, idx|
- if 1 > idx
- next
- end
-
- entity, attribute, value, timestmap, added = CSV.parse_line row
- added = added == 'true'
-
- if 1 < value.to_s.length && value.to_s.length < 30
- initial_trie = fact "id_#{entity}", idx, initial_trie
- initial_trie = fact "kv_#{attribute}_#{value}", entity, initial_trie
- end
- end
-
- initial_trie
-
- def find trie, phrase, index = 0
- if index == phrase.length - 1
- return trie.first.last
- end
-
- node = trie.filter { |n| n.first == phrase[index] }.first
-
- if ! node
- return nil
- end
-
- return find(node[1], phrase, index + 1)
- end
-
- criteria = { 'parent' => 'c282cb2a6395cc1d',
- 'back' => nil }
-
-
-
- # x = find initial_trie, 'context_primary' # 'ede6dd2ccf3a0bfe'
- results = {}
-
- criteria.each do |k, v|
-
- puts k
-
- if ! v.is_a?(NilClass)
- sequence = "#{k}_#{v}"
- results[k] = find initial_trie, sequence
- end
-
- end
-
-
- results
|