ontology_search.rb revision 9761530c96f0c65cf46998dafc145508802701c7
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenrequire 'json'
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen# Beware! This is not tested well.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenclass OntologySearch
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen def make_keyword_list_json(prefix)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen JSON.generate(make_keyword_list(prefix))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen def make_keyword_list(prefix)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen text_list = Set.new
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen unless Ontology.where("name = :prefix", prefix: prefix).empty?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen text_list.add(prefix)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen unless Entity.where("name = :prefix", prefix: prefix).empty?
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen text_list.add(prefix)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen Ontology.select(:name).where("name ILIKE :prefix", prefix: "#{prefix}%").group("name").limit(5).each do |ontology|
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen text_list.add(ontology.name)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen Entity.select(:name).where("name ILIKE :prefix", prefix: "#{prefix}%").group("name").limit(5).each do |symbol|
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen text_list.add(symbol.name)
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen end
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen
f16c114c20bbd7d292d93415d1e56c8dd6abd3e7Timo Sirainen Logic.select(:name).where("name ILIKE :prefix", prefix: "#{prefix}%").limit(5).each do |logic|
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen text_list.add(logic.name)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen text_list.to_a.sort.map { |x| {text: x} }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen def make_bean_list_json(keyword_list)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen JSON.generate(make_bean_list(keyword_list))
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen end
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen def make_bean_list(keyword_list)
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen ontology_hash = Hash.new
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen index = 0
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen keyword_list.each do |keyword|
e06c0b65c16ccce69bbee009ead14d7d3d17a256Timo Sirainen keyword_hash = Hash.new
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen Ontology.where("name = :name", name: "#{keyword}").limit(50).each do |ontology|
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen keyword_hash[ontology.id] ||= ontology
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen end
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen Entity.where("name = :name", name: "#{keyword}").limit(50).each do |symbol|
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen keyword_hash[symbol.ontology.id] ||= symbol.ontology
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen end
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen if logic = Logic.find_by_name(keyword)
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen logic.ontologies.each { |o| keyword_hash[o.id] ||= o }
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen end
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen if index == 0
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen ontology_hash = keyword_hash
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen else
02b79f9c2636da1829eee5b92753602bba8b67edTimo Sirainen hash = Hash.new
659fe5d24825b160cae512538088020d97a60239Timo Sirainen keyword_hash.each_key { |key| hash[key] ||= ontology_hash[key] }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ontology_hash = hash
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen index += 1
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bean_list_factory = OntologyBeanListFactory.new
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ontology_hash.each_value do |ontology|
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainen bean_list_factory.add_small_bean(ontology)
445f9e31c6c3aa6c0a72be8565da8f6e594d24fbTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bean_list_factory.bean_list
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen end
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenend
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen