Something like Datalog in Ruby. Aries a placeholder name, can't think of something clever right now.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

107 行
1.8KB

  1. require "json"
  2. require "csv"
  3. sentences = ['abcdefg',
  4. 'abcdhfg',
  5. 'abqwerty',
  6. 'abqwerty']
  7. def maker charlist, index, data, trie
  8. if index == (charlist.length)
  9. return trie
  10. end
  11. exists = trie.filter {|n| n.first == charlist[index] }.first
  12. node = (! exists) ? [
  13. charlist[index], # symbol,
  14. [], # items
  15. ] : exists
  16. node[1] = maker charlist, index + 1, data, node[1]
  17. if data && index == (charlist.length - 1)
  18. if node[2]
  19. node[2] << data
  20. else
  21. node[2] = [data]
  22. end
  23. end
  24. if ! exists
  25. trie << node
  26. end
  27. return trie
  28. end
  29. def fact value, data, trie
  30. chars = value.to_s.chars
  31. maker chars, 0, data, trie
  32. end
  33. initial_trie = []
  34. sentences.each_with_index do |sentence, idx|
  35. intial_trie = fact sentence, idx, initial_trie
  36. end
  37. pp initial_trie
  38. initial_trie = []
  39. File.readlines("/home/luka/Databases/aries/dissolver/db.csv").each_with_index do |row, idx|
  40. if 1 > idx
  41. next
  42. end
  43. entity, attribute, value, timestmap, added = CSV.parse_line row
  44. added = added == 'true'
  45. if 1 < value.to_s.length && value.to_s.length < 30
  46. initial_trie = fact "id_#{entity}", idx, initial_trie
  47. initial_trie = fact "kv_#{attribute}_#{value}", entity, initial_trie
  48. end
  49. end
  50. initial_trie
  51. def find trie, phrase, index = 0
  52. if index == phrase.length - 1
  53. return trie.first.last
  54. end
  55. node = trie.filter { |n| n.first == phrase[index] }.first
  56. if ! node
  57. return nil
  58. end
  59. return find(node[1], phrase, index + 1)
  60. end
  61. criteria = { 'parent' => 'c282cb2a6395cc1d',
  62. 'back' => nil }
  63. # x = find initial_trie, 'context_primary' # 'ede6dd2ccf3a0bfe'
  64. results = {}
  65. criteria.each do |k, v|
  66. puts k
  67. if ! v.is_a?(NilClass)
  68. sequence = "#{k}_#{v}"
  69. results[k] = find initial_trie, sequence
  70. end
  71. end
  72. results