diff --git a/app/lib/smooch_nlu.rb b/app/lib/smooch_nlu.rb index 01e15fb32f..1b44904807 100644 --- a/app/lib/smooch_nlu.rb +++ b/app/lib/smooch_nlu.rb @@ -46,13 +46,13 @@ def update_keywords(language, keywords, keyword, operation, doc_id, context) keywords << keyword alegre_operation = 'post' alegre_params = common_alegre_params.merge({ text: keyword, models: ALEGRE_MODELS_AND_THRESHOLDS.keys }) + Bot::Alegre.get_sync_raw_params(alegre_params, "text") if alegre_operation && alegre_params elsif operation == 'remove' keywords -= [keyword] alegre_operation = 'delete' alegre_params = common_alegre_params.merge({ quiet: true }) + Bot::Alegre.request_delete_from_raw(alegre_params, "text") if alegre_operation && alegre_params end - # FIXME: Add error handling and better logging - Bot::Alegre.request(alegre_operation, '/text/similarity/', alegre_params) if alegre_operation && alegre_params keywords end @@ -91,7 +91,7 @@ def self.alegre_matches_from_message(message, language, context, alegre_result_k language: language, }.merge(context) } - response = Bot::Alegre.request('post', '/text/similarity/search/', params) + response = Bot::Alegre.get_sync_raw_params(params, "text") # One approach would be to take the option that has the most matches # Unfortunately this approach is influenced by the number of keywords per option diff --git a/app/models/concerns/alegre_v2.rb b/app/models/concerns/alegre_v2.rb index 2dde5fc159..d93be886b7 100644 --- a/app/models/concerns/alegre_v2.rb +++ b/app/models/concerns/alegre_v2.rb @@ -1,7 +1,7 @@ require 'active_support/concern' class AlegreTimeoutError < StandardError; end class TemporaryProjectMedia - attr_accessor :team_id, :id, :url, :type + attr_accessor :team_id, :id, :url, :text, :type def media media_type_map = { "claim" => "Claim", @@ -55,11 +55,18 @@ def sync_path_for_type(type) end def async_path(project_media) - "/similarity/async/#{get_type(project_media)}" + self.async_path_for_type(get_type(project_media)) + end + + def async_path_for_type(type) + "/similarity/async/#{type}" end def delete_path(project_media) - type = get_type(project_media) + self.delete_path_for_type(get_type(project_media)) + end + + def delete_path_for_type(type) "/#{type}/similarity/" end @@ -122,6 +129,10 @@ def request(method, path, params, retries=3) end end + def request_delete_from_raw(params, type) + request("delete", delete_path_for_type(type), params) + end + def request_delete(data, project_media) request("delete", delete_path(project_media), data) end @@ -148,18 +159,22 @@ def get_type(project_media) type end + def content_hash_for_value(value) + Digest::MD5.hexdigest(value) + end + def content_hash(project_media, field) if Bot::Alegre::ALL_TEXT_SIMILARITY_FIELDS.include?(field) - Digest::MD5.hexdigest(project_media.send(field)) + content_hash_for_value(project_media.send(field)) else if project_media.is_link? - return Digest::MD5.hexdigest(project_media.media.url) + return content_hash_for_value(project_media.media.url) elsif project_media.is_a?(TemporaryProjectMedia) return Rails.cache.read("url_sha:#{project_media.url}") elsif !project_media.is_text? return project_media.media.file.filename.split(".").first else - return Digest::MD5.hexdigest(project_media.send(field).to_s) + return content_hash_for_value(project_media.send(field).to_s) end end end @@ -267,6 +282,14 @@ def store_package_text(project_media, field, params) generic_package_text(project_media, field, params) end + def get_sync_raw_params(params, type) + request("post", sync_path_for_type(type), params) + end + + def get_async_raw_params(params, type) + request("post", async_path_for_type(type), params) + end + def get_sync(project_media, field=nil, params={}) request_sync( store_package(project_media, field, params), @@ -537,4 +560,4 @@ def restrict_contexts(project_media, project_media_id_scores) }] end end -end +end \ No newline at end of file