Input: List available evaluators + devices in the current local environment Returns : - An array containing the names of those devices [CODESPLIT] def list_local_devices local_name = "job:localhost" TensorStream :: Evaluator . evaluators . collect { | k , v | v [ :class ] . query_supported_devices . collect do | device_str | [ local_name , "ts:#{k}:#{device_str.name}" ] . join ( "/" ) end } . flatten end


Input: Creates a variable A variable maintains state across sessions [CODESPLIT] def variable ( value , name : nil , initializer : nil , graph : nil , dtype : nil , trainable : true ) op = Graph . get_default_graph . add_op ( :assign , nil , value ) common_options = { initializer : initializer || op , name : name , graph : graph , dtype : dtype , trainable : trainable , } tensor = if value . is_a? ( String ) i_var ( dtype || :string , 0 , [ ] , get_variable_scope , common_options ) elsif value . is_a? ( Integer ) i_var ( dtype || :int32 , 0 , [ ] , get_variable_scope , common_options ) elsif value . is_a? ( Float ) i_var ( dtype || :float32 , 0 , [ ] , get_variable_scope , common_options ) else i_var ( dtype || :float32 , 0 , nil , get_variable_scope , common_options ) end op . set_input ( 0 , tensor . op ) Graph . get_default_graph . add_node ( op ) tensor end


Input: Defines a variable context manager [CODESPLIT] def variable_scope ( scope = nil , default_name = nil , reuse : nil , initializer : nil ) Thread . current [ :tensor_stream_variable_scope ] ||= [ VariableScope . new ] # uniquenifier if scope . nil? && default_name same_names = get_variable_scope . used_names . select { | s | s . start_with? ( default_name ) } new_name = default_name index = 1 while same_names . include? ( new_name ) new_name = "#{default_name}_#{index}" index += 1 end scope = new_name end variable_scope = VariableScope . new ( name : scope , reuse : reuse , initializer : initializer ) get_variable_scope . register_name ( scope || "" ) Thread . current [ :tensor_stream_variable_scope ] << variable_scope scope_name = __v_scope_name if block_given? begin TensorStream . get_default_graph . name_scope ( scope ) do yield ( scope_name ) end ensure Thread . current [ :tensor_stream_variable_scope ] . pop end else variable_scope end end


Input: Creates a session context where operations can be executed [CODESPLIT] def session ( evaluator = nil , thread_pool_class : Concurrent :: ImmediateExecutor , log_device_placement : false , profile_enabled : false ) session = TensorStream :: Session . new ( evaluator , thread_pool_class : thread_pool_class , log_device_placement : log_device_placement , profile_enabled : profile_enabled ) yield session if block_given? session end


Input: Inserts a placeholder for a tensor that will be always fed . [CODESPLIT] def placeholder ( dtype , shape : nil , name : nil ) TensorStream :: Placeholder . new ( dtype , nil , shape , name : name ) end


Input: Check to make sure passed array is dense [CODESPLIT] def check_if_dense ( value , expected_shape = nil ) return unless value . is_a? ( Array ) return if value . empty? expected_shape ||= shape_eval ( value ) s = expected_shape . shift raise TensorStream :: ValueError , "Argument must be a dense tensor: #{value}, expected size #{s} got #{value.size}" if value . size != s return if expected_shape . empty? value . each do | item | check_if_dense ( item , expected_shape . dup ) end end


Input: Auto cast ruby constant data types to the same tensor types of other operands [CODESPLIT] def apply_data_type_coercion ( * args ) coerced_type = check_data_types ( args ) args . map { | a | a . is_a? ( Tensor ) ? a : convert_to_tensor ( a , dtype : coerced_type ) } end


Input: database connection [CODESPLIT] def connection @connection ||= begin url = options [ :url ] || ENV [ "PGSLICE_URL" ] abort "Set PGSLICE_URL or use the --url option" unless url uri = URI . parse ( url ) params = CGI . parse ( uri . query . to_s ) # remove schema @schema = Array ( params . delete ( "schema" ) || "public" ) [ 0 ] uri . query = URI . encode_www_form ( params ) ENV [ "PGCONNECT_TIMEOUT" ] ||= "1" PG :: Connection . new ( uri . to_s ) end rescue PG :: ConnectionBad => e abort e . message rescue URI :: InvalidURIError abort "Invalid url" end


Input: helpers [CODESPLIT] def sql_date ( time , cast , add_cast = true ) if cast == "timestamptz" fmt = "%Y-%m-%d %H:%M:%S UTC" else fmt = "%Y-%m-%d" end str = "'#{time.strftime(fmt)}'" add_cast ? "#{str}::#{cast}" : str end


Input: http : // stackoverflow . com / a / 20537829 [CODESPLIT] def primary_key query = <<-SQL SQL execute ( query , [ schema , name ] ) . map { | r | r [ "attname" ] } end


Input: legacy [CODESPLIT] def fetch_settings ( trigger_name ) needs_comment = false trigger_comment = fetch_trigger ( trigger_name ) comment = trigger_comment || fetch_comment if comment field , period , cast , version = comment [ "comment" ] . split ( "," ) . map { | v | v . split ( ":" ) . last } rescue [ ] version = version . to_i if version end unless period needs_comment = true function_def = execute ( "SELECT pg_get_functiondef(oid) FROM pg_proc WHERE proname = $1" , [ trigger_name ] ) [ 0 ] return [ ] unless function_def function_def = function_def [ "pg_get_functiondef" ] sql_format = SQL_FORMAT . find { | _ , f | function_def . include? ( "'#{f}'" ) } return [ ] unless sql_format period = sql_format [ 0 ] field = / \( \. \w / . match ( function_def ) [ 1 ] end # backwards compatibility with 0.2.3 and earlier (pre-timestamptz support) unless cast cast = "date" # update comment to explicitly define cast needs_comment = true end version ||= trigger_comment ? 1 : 2 declarative = version > 1 [ period , field , cast , needs_comment , declarative , version ] end


Input: @!method initialize ( args ) Construct a new client for the Assistant service . [CODESPLIT] def message ( workspace_id : , input : nil , intents : nil , entities : nil , alternate_intents : nil , context : nil , output : nil , nodes_visited_details : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "message" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "nodes_visited_details" => nodes_visited_details } data = { "input" => input , "intents" => intents , "entities" => entities , "alternate_intents" => alternate_intents , "context" => context , "output" => output } method_url = "/v1/workspaces/%s/message" % [ ERB :: Util . url_encode ( workspace_id ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Workspaces [CODESPLIT] def create_workspace ( name : nil , description : nil , language : nil , metadata : nil , learning_opt_out : nil , system_settings : nil , intents : nil , entities : nil , dialog_nodes : nil , counterexamples : nil ) headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "create_workspace" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "name" => name , "description" => description , "language" => language , "metadata" => metadata , "learning_opt_out" => learning_opt_out , "system_settings" => system_settings , "intents" => intents , "entities" => entities , "dialog_nodes" => dialog_nodes , "counterexamples" => counterexamples } method_url = "/v1/workspaces" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method list_workspaces ( page_limit : nil include_count : nil sort : nil cursor : nil include_audit : nil ) List workspaces . List the workspaces associated with a Watson Assistant service instance . [CODESPLIT] def list_workspaces ( page_limit : nil , include_count : nil , sort : nil , cursor : nil , include_audit : nil ) headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "list_workspaces" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "page_limit" => page_limit , "include_count" => include_count , "sort" => sort , "cursor" => cursor , "include_audit" => include_audit } method_url = "/v1/workspaces" response = request ( method : "GET" , url : method_url , headers : headers , params : params , accept_json : true ) response end


Input: @!method update_workspace ( workspace_id : name : nil description : nil language : nil metadata : nil learning_opt_out : nil system_settings : nil intents : nil entities : nil dialog_nodes : nil counterexamples : nil append : nil ) Update workspace . Update an existing workspace with new or modified data . You must provide component objects defining the content of the updated workspace . [CODESPLIT] def update_workspace ( workspace_id : , name : nil , description : nil , language : nil , metadata : nil , learning_opt_out : nil , system_settings : nil , intents : nil , entities : nil , dialog_nodes : nil , counterexamples : nil , append : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "update_workspace" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "append" => append } data = { "name" => name , "description" => description , "language" => language , "metadata" => metadata , "learning_opt_out" => learning_opt_out , "system_settings" => system_settings , "intents" => intents , "entities" => entities , "dialog_nodes" => dialog_nodes , "counterexamples" => counterexamples } method_url = "/v1/workspaces/%s" % [ ERB :: Util . url_encode ( workspace_id ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method update_intent ( workspace_id : intent : new_intent : nil new_description : nil new_examples : nil ) Update intent . Update an existing intent with new or modified data . You must provide component objects defining the content of the updated intent . [CODESPLIT] def update_intent ( workspace_id : , intent : , new_intent : nil , new_description : nil , new_examples : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "intent must be provided" ) if intent . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "update_intent" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "intent" => new_intent , "description" => new_description , "examples" => new_examples } method_url = "/v1/workspaces/%s/intents/%s" % [ ERB :: Util . url_encode ( workspace_id ) , ERB :: Util . url_encode ( intent ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Entities [CODESPLIT] def create_entity ( workspace_id : , entity : , description : nil , metadata : nil , fuzzy_match : nil , values : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "entity must be provided" ) if entity . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "create_entity" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "entity" => entity , "description" => description , "metadata" => metadata , "fuzzy_match" => fuzzy_match , "values" => values } method_url = "/v1/workspaces/%s/entities" % [ ERB :: Util . url_encode ( workspace_id ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method update_entity ( workspace_id : entity : new_entity : nil new_description : nil new_metadata : nil new_fuzzy_match : nil new_values : nil ) Update entity . Update an existing entity with new or modified data . You must provide component objects defining the content of the updated entity . [CODESPLIT] def update_entity ( workspace_id : , entity : , new_entity : nil , new_description : nil , new_metadata : nil , new_fuzzy_match : nil , new_values : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "entity must be provided" ) if entity . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "update_entity" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "entity" => new_entity , "description" => new_description , "metadata" => new_metadata , "fuzzy_match" => new_fuzzy_match , "values" => new_values } method_url = "/v1/workspaces/%s/entities/%s" % [ ERB :: Util . url_encode ( workspace_id ) , ERB :: Util . url_encode ( entity ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Values [CODESPLIT] def create_value ( workspace_id : , entity : , value : , metadata : nil , value_type : nil , synonyms : nil , patterns : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "entity must be provided" ) if entity . nil? raise ArgumentError . new ( "value must be provided" ) if value . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "create_value" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "value" => value , "metadata" => metadata , "type" => value_type , "synonyms" => synonyms , "patterns" => patterns } method_url = "/v1/workspaces/%s/entities/%s/values" % [ ERB :: Util . url_encode ( workspace_id ) , ERB :: Util . url_encode ( entity ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method update_value ( workspace_id : entity : value : new_value : nil new_metadata : nil new_value_type : nil new_synonyms : nil new_patterns : nil ) Update entity value . Update an existing entity value with new or modified data . You must provide component objects defining the content of the updated entity value . [CODESPLIT] def update_value ( workspace_id : , entity : , value : , new_value : nil , new_metadata : nil , new_value_type : nil , new_synonyms : nil , new_patterns : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "entity must be provided" ) if entity . nil? raise ArgumentError . new ( "value must be provided" ) if value . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "update_value" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "value" => new_value , "metadata" => new_metadata , "type" => new_value_type , "synonyms" => new_synonyms , "patterns" => new_patterns } method_url = "/v1/workspaces/%s/entities/%s/values/%s" % [ ERB :: Util . url_encode ( workspace_id ) , ERB :: Util . url_encode ( entity ) , ERB :: Util . url_encode ( value ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Dialog nodes [CODESPLIT] def create_dialog_node ( workspace_id : , dialog_node : , description : nil , conditions : nil , parent : nil , previous_sibling : nil , output : nil , context : nil , metadata : nil , next_step : nil , title : nil , node_type : nil , event_name : nil , variable : nil , actions : nil , digress_in : nil , digress_out : nil , digress_out_slots : nil , user_label : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "dialog_node must be provided" ) if dialog_node . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "create_dialog_node" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "dialog_node" => dialog_node , "description" => description , "conditions" => conditions , "parent" => parent , "previous_sibling" => previous_sibling , "output" => output , "context" => context , "metadata" => metadata , "next_step" => next_step , "title" => title , "type" => node_type , "event_name" => event_name , "variable" => variable , "actions" => actions , "digress_in" => digress_in , "digress_out" => digress_out , "digress_out_slots" => digress_out_slots , "user_label" => user_label } method_url = "/v1/workspaces/%s/dialog_nodes" % [ ERB :: Util . url_encode ( workspace_id ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method update_dialog_node ( workspace_id : dialog_node : new_dialog_node : nil new_description : nil new_conditions : nil new_parent : nil new_previous_sibling : nil new_output : nil new_context : nil new_metadata : nil new_next_step : nil new_title : nil new_node_type : nil new_event_name : nil new_variable : nil new_actions : nil new_digress_in : nil new_digress_out : nil new_digress_out_slots : nil new_user_label : nil ) Update dialog node . Update an existing dialog node with new or modified data . [CODESPLIT] def update_dialog_node ( workspace_id : , dialog_node : , new_dialog_node : nil , new_description : nil , new_conditions : nil , new_parent : nil , new_previous_sibling : nil , new_output : nil , new_context : nil , new_metadata : nil , new_next_step : nil , new_title : nil , new_node_type : nil , new_event_name : nil , new_variable : nil , new_actions : nil , new_digress_in : nil , new_digress_out : nil , new_digress_out_slots : nil , new_user_label : nil ) raise ArgumentError . new ( "workspace_id must be provided" ) if workspace_id . nil? raise ArgumentError . new ( "dialog_node must be provided" ) if dialog_node . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "update_dialog_node" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "dialog_node" => new_dialog_node , "description" => new_description , "conditions" => new_conditions , "parent" => new_parent , "previous_sibling" => new_previous_sibling , "output" => new_output , "context" => new_context , "metadata" => new_metadata , "next_step" => new_next_step , "title" => new_title , "type" => new_node_type , "event_name" => new_event_name , "variable" => new_variable , "actions" => new_actions , "digress_in" => new_digress_in , "digress_out" => new_digress_out , "digress_out_slots" => new_digress_out_slots , "user_label" => new_user_label } method_url = "/v1/workspaces/%s/dialog_nodes/%s" % [ ERB :: Util . url_encode ( workspace_id ) , ERB :: Util . url_encode ( dialog_node ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Logs [CODESPLIT] def list_all_logs ( filter : , sort : nil , page_limit : nil , cursor : nil ) raise ArgumentError . new ( "filter must be provided" ) if filter . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V1" , "list_all_logs" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "filter" => filter , "sort" => sort , "page_limit" => page_limit , "cursor" => cursor } method_url = "/v1/logs" response = request ( method : "GET" , url : method_url , headers : headers , params : params , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Visual Recognition service . [CODESPLIT] def classify ( images_file : nil , images_filename : nil , images_file_content_type : nil , url : nil , threshold : nil , owners : nil , classifier_ids : nil , accept_language : nil ) headers = { "Accept-Language" => accept_language } sdk_headers = Common . new . get_sdk_headers ( "watson_vision_combined" , "V3" , "classify" ) headers . merge! ( sdk_headers ) params = { "version" => @version } form_data = { } unless images_file . nil? unless images_file . instance_of? ( StringIO ) || images_file . instance_of? ( File ) images_file = images_file . respond_to? ( :to_json ) ? StringIO . new ( images_file . to_json ) : StringIO . new ( images_file ) end images_filename = images_file . path if images_filename . nil? && images_file . respond_to? ( :path ) form_data [ :images_file ] = HTTP :: FormData :: File . new ( images_file , content_type : images_file_content_type . nil? ? "application/octet-stream" : images_file_content_type , filename : images_filename ) end classifier_ids *= "," unless classifier_ids . nil? owners *= "," unless owners . nil? form_data [ :url ] = HTTP :: FormData :: Part . new ( url . to_s , content_type : "text/plain" ) unless url . nil? form_data [ :threshold ] = HTTP :: FormData :: Part . new ( threshold . to_s , content_type : "application/json" ) unless threshold . nil? form_data [ :owners ] = HTTP :: FormData :: Part . new ( owners , content_type : "application/json" ) unless owners . nil? form_data [ :classifier_ids ] = HTTP :: FormData :: Part . new ( classifier_ids , content_type : "application/json" ) unless classifier_ids . nil? method_url = "/v3/classify" response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: Face [CODESPLIT] def detect_faces ( images_file : nil , images_filename : nil , images_file_content_type : nil , url : nil , accept_language : nil ) headers = { "Accept-Language" => accept_language } sdk_headers = Common . new . get_sdk_headers ( "watson_vision_combined" , "V3" , "detect_faces" ) headers . merge! ( sdk_headers ) params = { "version" => @version } form_data = { } unless images_file . nil? unless images_file . instance_of? ( StringIO ) || images_file . instance_of? ( File ) images_file = images_file . respond_to? ( :to_json ) ? StringIO . new ( images_file . to_json ) : StringIO . new ( images_file ) end images_filename = images_file . path if images_filename . nil? && images_file . respond_to? ( :path ) form_data [ :images_file ] = HTTP :: FormData :: File . new ( images_file , content_type : images_file_content_type . nil? ? "application/octet-stream" : images_file_content_type , filename : images_filename ) end form_data [ :url ] = HTTP :: FormData :: Part . new ( url . to_s , content_type : "text/plain" ) unless url . nil? method_url = "/v3/detect_faces" response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: @!method update_classifier ( classifier_id : positive_examples : nil negative_examples : nil negative_examples_filename : nil ) Update a classifier . Update a custom classifier by adding new positive or negative classes or by adding new images to existing classes . You must supply at least one set of positive or negative examples . For details see [ Updating custom classifiers ] ( https : // cloud . ibm . com / docs / services / visual - recognition / customizing . html#updating - custom - classifiers ) . [CODESPLIT] def update_classifier ( classifier_id : , positive_examples : nil , negative_examples : nil , negative_examples_filename : nil ) raise ArgumentError . new ( "classifier_id must be provided" ) if classifier_id . nil? raise ArgumentError . new ( "positive_examples must be a hash" ) unless positive_examples . nil? || positive_examples . is_a? ( Hash ) headers = { } sdk_headers = Common . new . get_sdk_headers ( "watson_vision_combined" , "V3" , "update_classifier" ) headers . merge! ( sdk_headers ) params = { "version" => @version } form_data = { } positive_examples &. each do | key , value | part_name = "%s_positive_examples" % key . to_s unless value . instance_of? ( StringIO ) || value . instance_of? ( File ) value = value . respond_to? ( :to_json ) ? StringIO . new ( value . to_json ) : StringIO . new ( value ) end filename = value . path if value . respond_to? ( :path ) form_data [ part_name . to_sym ] = HTTP :: FormData :: File . new ( value , content_type : "application/octet-stream" , filename : filename ) end unless negative_examples . nil? unless negative_examples . instance_of? ( StringIO ) || negative_examples . instance_of? ( File ) negative_examples = negative_examples . respond_to? ( :to_json ) ? StringIO . new ( negative_examples . to_json ) : StringIO . new ( negative_examples ) end negative_examples_filename = negative_examples . path if negative_examples_filename . nil? && negative_examples . respond_to? ( :path ) form_data [ :negative_examples ] = HTTP :: FormData :: File . new ( negative_examples , content_type : "application/octet-stream" , filename : negative_examples_filename ) end method_url = "/v3/classifiers/%s" % [ ERB :: Util . url_encode ( classifier_id ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: Manage classifiers [CODESPLIT] def create_classifier ( metadata : , training_data : ) raise ArgumentError . new ( "metadata must be provided" ) if metadata . nil? raise ArgumentError . new ( "training_data must be provided" ) if training_data . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "natural_language_classifier" , "V1" , "create_classifier" ) headers . merge! ( sdk_headers ) form_data = { } unless metadata . instance_of? ( StringIO ) || metadata . instance_of? ( File ) metadata = metadata . respond_to? ( :to_json ) ? StringIO . new ( metadata . to_json ) : StringIO . new ( metadata ) end form_data [ :training_metadata ] = HTTP :: FormData :: File . new ( metadata , content_type : "application/json" , filename : metadata . respond_to? ( :path ) ? metadata . path : nil ) unless training_data . instance_of? ( StringIO ) || training_data . instance_of? ( File ) training_data = training_data . respond_to? ( :to_json ) ? StringIO . new ( training_data . to_json ) : StringIO . new ( training_data ) end form_data [ :training_data ] = HTTP :: FormData :: File . new ( training_data , content_type : "text/csv" , filename : training_data . respond_to? ( :path ) ? training_data . path : nil ) method_url = "/v1/classifiers" response = request ( method : "POST" , url : method_url , headers : headers , form : form_data , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Personality Insights service . [CODESPLIT] def profile ( content : , accept : , content_language : nil , accept_language : nil , raw_scores : nil , csv_headers : nil , consumption_preferences : nil , content_type : nil ) raise ArgumentError . new ( "content must be provided" ) if content . nil? raise ArgumentError . new ( "accept must be provided" ) if accept . nil? headers = { "Accept" => accept , "Content-Language" => content_language , "Accept-Language" => accept_language , "Content-Type" => content_type } sdk_headers = Common . new . get_sdk_headers ( "personality_insights" , "V3" , "profile" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "raw_scores" => raw_scores , "csv_headers" => csv_headers , "consumption_preferences" => consumption_preferences } if content_type . start_with? ( "application/json" ) && content . instance_of? ( Hash ) data = content . to_json else data = content end method_url = "/v3/profile" response = request ( method : "POST" , url : method_url , headers : headers , params : params , data : data , accept_json : true ) response end


Input: Synthesis [CODESPLIT] def synthesize ( text : , voice : nil , customization_id : nil , accept : nil ) raise ArgumentError . new ( "text must be provided" ) if text . nil? headers = { "Accept" => accept } sdk_headers = Common . new . get_sdk_headers ( "text_to_speech" , "V1" , "synthesize" ) headers . merge! ( sdk_headers ) params = { "voice" => voice , "customization_id" => customization_id } data = { "text" => text } method_url = "/v1/synthesize" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : false ) response end


Input: Pronunciation [CODESPLIT] def get_pronunciation ( text : , voice : nil , format : nil , customization_id : nil ) raise ArgumentError . new ( "text must be provided" ) if text . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "text_to_speech" , "V1" , "get_pronunciation" ) headers . merge! ( sdk_headers ) params = { "text" => text , "voice" => voice , "format" => format , "customization_id" => customization_id } method_url = "/v1/pronunciation" response = request ( method : "GET" , url : method_url , headers : headers , params : params , accept_json : true ) response end


Input: Custom words [CODESPLIT] def add_word ( customization_id : , word : , translation : , part_of_speech : nil ) raise ArgumentError . new ( "customization_id must be provided" ) if customization_id . nil? raise ArgumentError . new ( "word must be provided" ) if word . nil? raise ArgumentError . new ( "translation must be provided" ) if translation . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "text_to_speech" , "V1" , "add_word" ) headers . merge! ( sdk_headers ) data = { "translation" => translation , "part_of_speech" => part_of_speech } method_url = "/v1/customizations/%s/words/%s" % [ ERB :: Util . url_encode ( customization_id ) , ERB :: Util . url_encode ( word ) ] request ( method : "PUT" , url : method_url , headers : headers , json : data , accept_json : false ) nil end


Input: User data [CODESPLIT] def delete_user_data ( customer_id : ) raise ArgumentError . new ( "customer_id must be provided" ) if customer_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "text_to_speech" , "V1" , "delete_user_data" ) headers . merge! ( sdk_headers ) params = { "customer_id" => customer_id } method_url = "/v1/user_data" request ( method : "DELETE" , url : method_url , headers : headers , params : params , accept_json : false ) nil end


Input: @!method initialize ( args ) Construct a new client for the Compare Comply service . [CODESPLIT] def convert_to_html ( file : , filename : nil , file_content_type : nil , model : nil ) raise ArgumentError . new ( "file must be provided" ) if file . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "compare-comply" , "V1" , "convert_to_html" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "model" => model } form_data = { } unless file . instance_of? ( StringIO ) || file . instance_of? ( File ) file = file . respond_to? ( :to_json ) ? StringIO . new ( file . to_json ) : StringIO . new ( file ) end filename = file . path if filename . nil? && file . respond_to? ( :path ) form_data [ :file ] = HTTP :: FormData :: File . new ( file , content_type : file_content_type . nil? ? "application/octet-stream" : file_content_type , filename : filename ) method_url = "/v1/html_conversion" response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: Comparison [CODESPLIT] def compare_documents ( file_1 : , file_2 : , file_1_content_type : nil , file_2_content_type : nil , file_1_label : nil , file_2_label : nil , model : nil ) raise ArgumentError . new ( "file_1 must be provided" ) if file_1 . nil? raise ArgumentError . new ( "file_2 must be provided" ) if file_2 . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "compare-comply" , "V1" , "compare_documents" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "file_1_label" => file_1_label , "file_2_label" => file_2_label , "model" => model } form_data = { } unless file_1 . instance_of? ( StringIO ) || file_1 . instance_of? ( File ) file_1 = file_1 . respond_to? ( :to_json ) ? StringIO . new ( file_1 . to_json ) : StringIO . new ( file_1 ) end form_data [ :file_1 ] = HTTP :: FormData :: File . new ( file_1 , content_type : file_1_content_type . nil? ? "application/octet-stream" : file_1_content_type , filename : file_1 . respond_to? ( :path ) ? file_1 . path : nil ) unless file_2 . instance_of? ( StringIO ) || file_2 . instance_of? ( File ) file_2 = file_2 . respond_to? ( :to_json ) ? StringIO . new ( file_2 . to_json ) : StringIO . new ( file_2 ) end form_data [ :file_2 ] = HTTP :: FormData :: File . new ( file_2 , content_type : file_2_content_type . nil? ? "application/octet-stream" : file_2_content_type , filename : file_2 . respond_to? ( :path ) ? file_2 . path : nil ) method_url = "/v1/comparison" response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: Feedback [CODESPLIT] def add_feedback ( feedback_data : , user_id : nil , comment : nil ) raise ArgumentError . new ( "feedback_data must be provided" ) if feedback_data . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "compare-comply" , "V1" , "add_feedback" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "feedback_data" => feedback_data , "user_id" => user_id , "comment" => comment } method_url = "/v1/feedback" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Batches [CODESPLIT] def create_batch ( function : , input_credentials_file : , input_bucket_location : , input_bucket_name : , output_credentials_file : , output_bucket_location : , output_bucket_name : , model : nil ) raise ArgumentError . new ( "function must be provided" ) if function . nil? raise ArgumentError . new ( "input_credentials_file must be provided" ) if input_credentials_file . nil? raise ArgumentError . new ( "input_bucket_location must be provided" ) if input_bucket_location . nil? raise ArgumentError . new ( "input_bucket_name must be provided" ) if input_bucket_name . nil? raise ArgumentError . new ( "output_credentials_file must be provided" ) if output_credentials_file . nil? raise ArgumentError . new ( "output_bucket_location must be provided" ) if output_bucket_location . nil? raise ArgumentError . new ( "output_bucket_name must be provided" ) if output_bucket_name . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "compare-comply" , "V1" , "create_batch" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "function" => function , "model" => model } form_data = { } unless input_credentials_file . instance_of? ( StringIO ) || input_credentials_file . instance_of? ( File ) input_credentials_file = input_credentials_file . respond_to? ( :to_json ) ? StringIO . new ( input_credentials_file . to_json ) : StringIO . new ( input_credentials_file ) end form_data [ :input_credentials_file ] = HTTP :: FormData :: File . new ( input_credentials_file , content_type : "application/json" , filename : input_credentials_file . respond_to? ( :path ) ? input_credentials_file . path : nil ) form_data [ :input_bucket_location ] = HTTP :: FormData :: Part . new ( input_bucket_location . to_s , content_type : "text/plain" ) form_data [ :input_bucket_name ] = HTTP :: FormData :: Part . new ( input_bucket_name . to_s , content_type : "text/plain" ) unless output_credentials_file . instance_of? ( StringIO ) || output_credentials_file . instance_of? ( File ) output_credentials_file = output_credentials_file . respond_to? ( :to_json ) ? StringIO . new ( output_credentials_file . to_json ) : StringIO . new ( output_credentials_file ) end form_data [ :output_credentials_file ] = HTTP :: FormData :: File . new ( output_credentials_file , content_type : "application/json" , filename : output_credentials_file . respond_to? ( :path ) ? output_credentials_file . path : nil ) form_data [ :output_bucket_location ] = HTTP :: FormData :: Part . new ( output_bucket_location . to_s , content_type : "text/plain" ) form_data [ :output_bucket_name ] = HTTP :: FormData :: Part . new ( output_bucket_name . to_s , content_type : "text/plain" ) method_url = "/v1/batches" response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Tone Analyzer service . [CODESPLIT] def tone ( tone_input : , sentences : nil , tones : nil , content_language : nil , accept_language : nil , content_type : nil ) raise ArgumentError . new ( "tone_input must be provided" ) if tone_input . nil? headers = { "Content-Language" => content_language , "Accept-Language" => accept_language , "Content-Type" => content_type } sdk_headers = Common . new . get_sdk_headers ( "tone_analyzer" , "V3" , "tone" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "sentences" => sentences , "tones" => tones . to_a } if content_type . start_with? ( "application/json" ) && tone_input . instance_of? ( Hash ) data = tone_input . to_json else data = tone_input end method_url = "/v3/tone" response = request ( method : "POST" , url : method_url , headers : headers , params : params , data : data , accept_json : true ) response end


Input: @!method tone_chat ( utterances : content_language : nil accept_language : nil ) Analyze customer engagement tone . Use the customer engagement endpoint to analyze the tone of customer service and customer support conversations . For each utterance of a conversation the method reports the most prevalent subset of the following seven tones : sad frustrated satisfied excited polite impolite and sympathetic . [CODESPLIT] def tone_chat ( utterances : , content_language : nil , accept_language : nil ) raise ArgumentError . new ( "utterances must be provided" ) if utterances . nil? headers = { "Content-Language" => content_language , "Accept-Language" => accept_language } sdk_headers = Common . new . get_sdk_headers ( "tone_analyzer" , "V3" , "tone_chat" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "utterances" => utterances } method_url = "/v3/tone_chat" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Language Translator service . [CODESPLIT] def translate ( text : , model_id : nil , source : nil , target : nil ) raise ArgumentError . new ( "text must be provided" ) if text . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "language_translator" , "V3" , "translate" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "text" => text , "model_id" => model_id , "source" => source , "target" => target } method_url = "/v3/translate" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Identification [CODESPLIT] def identify ( text : ) raise ArgumentError . new ( "text must be provided" ) if text . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "language_translator" , "V3" , "identify" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = text headers [ "Content-Type" ] = "text/plain" method_url = "/v3/identify" response = request ( method : "POST" , url : method_url , headers : headers , params : params , data : data , accept_json : true ) response end


Input: Models [CODESPLIT] def create_model ( base_model_id : , forced_glossary : nil , parallel_corpus : nil , name : nil ) raise ArgumentError . new ( "base_model_id must be provided" ) if base_model_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "language_translator" , "V3" , "create_model" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "base_model_id" => base_model_id , "name" => name } form_data = { } unless forced_glossary . nil? unless forced_glossary . instance_of? ( StringIO ) || forced_glossary . instance_of? ( File ) forced_glossary = forced_glossary . respond_to? ( :to_json ) ? StringIO . new ( forced_glossary . to_json ) : StringIO . new ( forced_glossary ) end form_data [ :forced_glossary ] = HTTP :: FormData :: File . new ( forced_glossary , content_type : "application/octet-stream" , filename : forced_glossary . respond_to? ( :path ) ? forced_glossary . path : nil ) end unless parallel_corpus . nil? unless parallel_corpus . instance_of? ( StringIO ) || parallel_corpus . instance_of? ( File ) parallel_corpus = parallel_corpus . respond_to? ( :to_json ) ? StringIO . new ( parallel_corpus . to_json ) : StringIO . new ( parallel_corpus ) end form_data [ :parallel_corpus ] = HTTP :: FormData :: File . new ( parallel_corpus , content_type : "application/octet-stream" , filename : parallel_corpus . respond_to? ( :path ) ? parallel_corpus . path : nil ) end method_url = "/v3/models" response = request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Natural Language Understanding service . [CODESPLIT] def analyze ( features : , text : nil , html : nil , url : nil , clean : nil , xpath : nil , fallback_to_raw : nil , return_analyzed_text : nil , language : nil , limit_text_characters : nil ) raise ArgumentError . new ( "features must be provided" ) if features . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "natural-language-understanding" , "V1" , "analyze" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "features" => features , "text" => text , "html" => html , "url" => url , "clean" => clean , "xpath" => xpath , "fallback_to_raw" => fallback_to_raw , "return_analyzed_text" => return_analyzed_text , "language" => language , "limit_text_characters" => limit_text_characters } method_url = "/v1/analyze" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Speech to Text service . [CODESPLIT] def get_model ( model_id : ) raise ArgumentError . new ( "model_id must be provided" ) if model_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "get_model" ) headers . merge! ( sdk_headers ) method_url = "/v1/models/%s" % [ ERB :: Util . url_encode ( model_id ) ] response = request ( method : "GET" , url : method_url , headers : headers , accept_json : true ) response end


Input: @!method recognize_using_websocket ( content_type : recognize_callback : audio : nil chunk_data : false model : nil customization_id : nil acoustic_customization_id : nil customization_weight : nil base_model_version : nil inactivity_timeout : nil interim_results : nil keywords : nil keywords_threshold : nil max_alternatives : nil word_alternatives_threshold : nil word_confidence : nil timestamps : nil profanity_filter : nil smart_formatting : nil speaker_labels : nil ) Sends audio for speech recognition using web sockets . @param content_type [ String ] The type of the input : audio / basic audio / flac audio / l16 audio / mp3 audio / mpeg audio / mulaw audio / ogg audio / ogg ; codecs = opus audio / ogg ; codecs = vorbis audio / wav audio / webm audio / webm ; codecs = opus audio / webm ; codecs = vorbis or multipart / form - data . @param recognize_callback [ RecognizeCallback ] The instance handling events returned from the service . @param audio [ IO ] Audio to transcribe in the format specified by the Content - Type header . @param chunk_data [ Boolean ] If true then the WebSocketClient will expect to receive data in chunks rather than as a single audio file @param model [ String ] The identifier of the model to be used for the recognition request . @param customization_id [ String ] The GUID of a custom language model that is to be used with the request . The base model of the specified custom language model must match the model specified with the model parameter . You must make the request with service credentials created for the instance of the service that owns the custom model . By default no custom language model is used . @param acoustic_customization_id [ String ] The GUID of a custom acoustic model that is to be used with the request . The base model of the specified custom acoustic model must match the model specified with the model parameter . You must make the request with service credentials created for the instance of the service that owns the custom model . By default no custom acoustic model is used . @param language_customization_id [ String ] The GUID of a custom language model that is to be used with the request . The base model of the specified custom language model must match the model specified with the model parameter . You must make the request with service credentials created for the instance of the service that owns the custom model . By default no custom language model is used . @param customization_weight [ Float ] If you specify a customization_id with the request you can use the customization_weight parameter to tell the service how much weight to give to words from the custom language model compared to those from the base model for speech recognition . Specify a value between 0 . 0 and 1 . 0 . Unless a different customization weight was specified for the custom model when it was trained the default value is 0 . 3 . A customization weight that you specify overrides a weight that was specified when the custom model was trained . The default value yields the best performance in general . Assign a higher value if your audio makes frequent use of OOV words from the custom model . Use caution when setting the weight : a higher value can improve the accuracy of phrases from the custom model s domain but it can negatively affect performance on non - domain phrases . @param base_model_version [ String ] The version of the specified base model that is to be used for speech recognition . Multiple versions of a base model can exist when a model is updated for internal improvements . The parameter is intended primarily for use with custom models that have been upgraded for a new base model . The default value depends on whether the parameter is used with or without a custom model . For more information see [ Base model version ] ( https : // console . bluemix . net / docs / services / speech - to - text / input . html#version ) . @param inactivity_timeout [ Integer ] The time in seconds after which if only silence ( no speech ) is detected in submitted audio the connection is closed with a 400 error . Useful for stopping audio submission from a live microphone when a user simply walks away . Use - 1 for infinity . @param interim_results [ Boolean ] Send back non - final previews of each sentence as it is being processed . These results are ignored in text mode . @param keywords [ Array<String > ] Array of keyword strings to spot in the audio . Each keyword string can include one or more tokens . Keywords are spotted only in the final hypothesis not in interim results . If you specify any keywords you must also specify a keywords threshold . Omit the parameter or specify an empty array if you do not need to spot keywords . @param keywords_threshold [ Float ] Confidence value that is the lower bound for spotting a keyword . A word is considered to match a keyword if its confidence is greater than or equal to the threshold . Specify a probability between 0 and 1 inclusive . No keyword spotting is performed if you omit the parameter . If you specify a threshold you must also specify one or more keywords . @param max_alternatives [ Integer ] Maximum number of alternative transcripts to be returned . By default a single transcription is returned . @param word_alternatives_threshold [ Float ] Confidence value that is the lower bound for identifying a hypothesis as a possible word alternative ( also known as \ Confusion Networks \ ) . An alternative word is considered if its confidence is greater than or equal to the threshold . Specify a probability between 0 and 1 inclusive . No alternative words are computed if you omit the parameter . @param word_confidence [ Boolean ] If true confidence measure per word is returned . @param timestamps [ Boolean ] If true time alignment for each word is returned . @param profanity_filter [ Boolean ] If true ( the default ) filters profanity from all output except for keyword results by replacing inappropriate words with a series of asterisks . Set the parameter to false to return results with no censoring . Applies to US English transcription only . @param smart_formatting [ Boolean ] If true converts dates times series of digits and numbers phone numbers currency values and Internet addresses into more readable conventional representations in the final transcript of a recognition request . If false ( the default ) no formatting is performed . Applies to US English transcription only . @param speaker_labels [ Boolean ] Indicates whether labels that identify which words were spoken by which participants in a multi - person exchange are to be included in the response . The default is false ; no speaker labels are returned . Setting speaker_labels to true forces the timestamps parameter to be true regardless of whether you specify false for the parameter . To determine whether a language model supports speaker labels use the GET / v1 / models method and check that the attribute speaker_labels is set to true . You can also refer to [ Speaker labels ] ( https : // console . bluemix . net / docs / services / speech - to - text / output . html#speaker_labels ) . @param grammar_name [ String ] The name of a grammar that is to be used with the recognition request . If you specify a grammar you must also use the language_customization_id parameter to specify the name of the custom language model for which the grammar is defined . The service recognizes only strings that are recognized by the specified grammar ; it does not recognize other custom words from the model s words resource . See [ Grammars ] ( https : // cloud . ibm . com / docs / services / speech - to - text / output . html ) . @param redaction [ Boolean ] If true the service redacts or masks numeric data from final transcripts . The feature redacts any number that has three or more consecutive digits by replacing each digit with an X character . It is intended to redact sensitive numeric data such as credit card numbers . By default the service performs no redaction . [CODESPLIT] def recognize_using_websocket ( content_type : nil , recognize_callback : , audio : nil , chunk_data : false , model : nil , language_customization_id : nil , customization_id : nil , acoustic_customization_id : nil , customization_weight : nil , base_model_version : nil , inactivity_timeout : nil , interim_results : nil , keywords : nil , keywords_threshold : nil , max_alternatives : nil , word_alternatives_threshold : nil , word_confidence : nil , timestamps : nil , profanity_filter : nil , smart_formatting : nil , speaker_labels : nil , grammar_name : nil , redaction : nil ) raise ArgumentError ( "Audio must be provided" ) if audio . nil? && ! chunk_data raise ArgumentError ( "Recognize callback must be provided" ) if recognize_callback . nil? raise TypeError ( "Callback is not a derived class of RecognizeCallback" ) unless recognize_callback . is_a? ( IBMWatson :: RecognizeCallback ) require_relative ( "./websocket/speech_to_text_websocket_listener.rb" ) headers = { } headers = conn . default_options . headers . to_hash unless conn . default_options . headers . to_hash . empty? if ! token_manager . nil? access_token = token_manager . token headers [ "Authorization" ] = "Bearer #{access_token}" elsif ! username . nil? && ! password . nil? headers [ "Authorization" ] = "Basic " + Base64 . strict_encode64 ( "#{username}:#{password}" ) end url = @url . gsub ( "https:" , "wss:" ) params = { "model" => model , "customization_id" => customization_id , "langauge_customization_id" => language_customization_id , "acoustic_customization_id" => acoustic_customization_id , "customization_weight" => customization_weight , "base_model_version" => base_model_version } params . delete_if { | _ , v | v . nil? } url += "/v1/recognize?" + HTTP :: URI . form_encode ( params ) options = { "content_type" => content_type , "inactivity_timeout" => inactivity_timeout , "interim_results" => interim_results , "keywords" => keywords , "keywords_threshold" => keywords_threshold , "max_alternatives" => max_alternatives , "word_alternatives_threshold" => word_alternatives_threshold , "word_confidence" => word_confidence , "timestamps" => timestamps , "profanity_filter" => profanity_filter , "smart_formatting" => smart_formatting , "speaker_labels" => speaker_labels , "grammar_name" => grammar_name , "redaction" => redaction } options . delete_if { | _ , v | v . nil? } WebSocketClient . new ( audio : audio , chunk_data : chunk_data , options : options , recognize_callback : recognize_callback , url : url , headers : headers , disable_ssl_verification : @disable_ssl_verification ) end


Input: : nocov : [CODESPLIT] def recognize_with_websocket ( content_type : , recognize_callback : , audio : nil , chunk_data : false , model : nil , customization_id : nil , acoustic_customization_id : nil , customization_weight : nil , base_model_version : nil , inactivity_timeout : nil , interim_results : nil , keywords : nil , keywords_threshold : nil , max_alternatives : nil , word_alternatives_threshold : nil , word_confidence : nil , timestamps : nil , profanity_filter : nil , smart_formatting : nil , speaker_labels : nil ) Kernel . warn ( "[DEPRECATION] `recognize_with_websocket` is deprecated and will be removed in the next major release. Please use `recognize_using_websocket` instead." ) recognize_using_websocket ( audio : audio , chunk_data : chunk_data , content_type : content_type , model : model , recognize_callback : recognize_callback , customization_id : customization_id , acoustic_customization_id : acoustic_customization_id , customization_weight : customization_weight , base_model_version : base_model_version , inactivity_timeout : inactivity_timeout , interim_results : interim_results , keywords : keywords , keywords_threshold : keywords_threshold , max_alternatives : max_alternatives , word_alternatives_threshold : word_alternatives_threshold , word_confidence : word_confidence , timestamps : timestamps , profanity_filter : profanity_filter , smart_formatting : smart_formatting , speaker_labels : speaker_labels ) end


Input: @!method create_job ( audio : model : nil callback_url : nil events : nil user_token : nil results_ttl : nil language_customization_id : nil acoustic_customization_id : nil base_model_version : nil customization_weight : nil inactivity_timeout : nil keywords : nil keywords_threshold : nil max_alternatives : nil word_alternatives_threshold : nil word_confidence : nil timestamps : nil profanity_filter : nil smart_formatting : nil speaker_labels : nil customization_id : nil grammar_name : nil redaction : nil content_type : nil ) Create a job . Creates a job for a new asynchronous recognition request . The job is owned by the instance of the service whose credentials are used to create it . How you learn the status and results of a job depends on the parameters you include with the job creation request : * By callback notification : Include the callback_url parameter to specify a URL to which the service is to send callback notifications when the status of the job changes . Optionally you can also include the events and user_token parameters to subscribe to specific events and to specify a string that is to be included with each notification for the job . * By polling the service : Omit the callback_url events and user_token parameters . You must then use the ** Check jobs ** or ** Check a job ** methods to check the status of the job using the latter to retrieve the results when the job is complete . [CODESPLIT] def create_job ( audio : , model : nil , callback_url : nil , events : nil , user_token : nil , results_ttl : nil , language_customization_id : nil , acoustic_customization_id : nil , base_model_version : nil , customization_weight : nil , inactivity_timeout : nil , keywords : nil , keywords_threshold : nil , max_alternatives : nil , word_alternatives_threshold : nil , word_confidence : nil , timestamps : nil , profanity_filter : nil , smart_formatting : nil , speaker_labels : nil , customization_id : nil , grammar_name : nil , redaction : nil , content_type : nil ) raise ArgumentError . new ( "audio must be provided" ) if audio . nil? headers = { "Content-Type" => content_type } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "create_job" ) headers . merge! ( sdk_headers ) params = { "model" => model , "callback_url" => callback_url , "events" => events , "user_token" => user_token , "results_ttl" => results_ttl , "language_customization_id" => language_customization_id , "acoustic_customization_id" => acoustic_customization_id , "base_model_version" => base_model_version , "customization_weight" => customization_weight , "inactivity_timeout" => inactivity_timeout , "keywords" => keywords . to_a , "keywords_threshold" => keywords_threshold , "max_alternatives" => max_alternatives , "word_alternatives_threshold" => word_alternatives_threshold , "word_confidence" => word_confidence , "timestamps" => timestamps , "profanity_filter" => profanity_filter , "smart_formatting" => smart_formatting , "speaker_labels" => speaker_labels , "customization_id" => customization_id , "grammar_name" => grammar_name , "redaction" => redaction } data = audio method_url = "/v1/recognitions" response = request ( method : "POST" , url : method_url , headers : headers , params : params , data : data , accept_json : true ) response end


Input: @!method register_callback ( callback_url : user_secret : nil ) Register a callback . Registers a callback URL with the service for use with subsequent asynchronous recognition requests . The service attempts to register or white - list the callback URL if it is not already registered by sending a GET request to the callback URL . The service passes a random alphanumeric challenge string via the challenge_string parameter of the request . The request includes an Accept header that specifies text / plain as the required response type . [CODESPLIT] def register_callback ( callback_url : , user_secret : nil ) raise ArgumentError . new ( "callback_url must be provided" ) if callback_url . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "register_callback" ) headers . merge! ( sdk_headers ) params = { "callback_url" => callback_url , "user_secret" => user_secret } method_url = "/v1/register_callback" response = request ( method : "POST" , url : method_url , headers : headers , params : params , accept_json : true ) response end


Input: Custom corpora [CODESPLIT] def add_corpus ( customization_id : , corpus_name : , corpus_file : , allow_overwrite : nil ) raise ArgumentError . new ( "customization_id must be provided" ) if customization_id . nil? raise ArgumentError . new ( "corpus_name must be provided" ) if corpus_name . nil? raise ArgumentError . new ( "corpus_file must be provided" ) if corpus_file . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "add_corpus" ) headers . merge! ( sdk_headers ) params = { "allow_overwrite" => allow_overwrite } form_data = { } unless corpus_file . instance_of? ( StringIO ) || corpus_file . instance_of? ( File ) corpus_file = corpus_file . respond_to? ( :to_json ) ? StringIO . new ( corpus_file . to_json ) : StringIO . new ( corpus_file ) end form_data [ :corpus_file ] = HTTP :: FormData :: File . new ( corpus_file , content_type : "text/plain" , filename : corpus_file . respond_to? ( :path ) ? corpus_file . path : nil ) method_url = "/v1/customizations/%s/corpora/%s" % [ ERB :: Util . url_encode ( customization_id ) , ERB :: Util . url_encode ( corpus_name ) ] request ( method : "POST" , url : method_url , headers : headers , params : params , form : form_data , accept_json : true ) nil end


Input: Custom words [CODESPLIT] def add_word ( customization_id : , word_name : , word : nil , sounds_like : nil , display_as : nil ) raise ArgumentError . new ( "customization_id must be provided" ) if customization_id . nil? raise ArgumentError . new ( "word_name must be provided" ) if word_name . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "add_word" ) headers . merge! ( sdk_headers ) data = { "word" => word , "sounds_like" => sounds_like , "display_as" => display_as } method_url = "/v1/customizations/%s/words/%s" % [ ERB :: Util . url_encode ( customization_id ) , ERB :: Util . url_encode ( word_name ) ] request ( method : "PUT" , url : method_url , headers : headers , json : data , accept_json : true ) nil end


Input: @!method add_words ( customization_id : words : ) Add custom words . Adds one or more custom words to a custom language model . The service populates the words resource for a custom model with out - of - vocabulary ( OOV ) words from each corpus or grammar that is added to the model . You can use this method to add additional words or to modify existing words in the words resource . The words resource for a model can contain a maximum of 30 thousand custom ( OOV ) words . This includes words that the service extracts from corpora and grammars and words that you add directly . [CODESPLIT] def add_words ( customization_id : , words : ) raise ArgumentError . new ( "customization_id must be provided" ) if customization_id . nil? raise ArgumentError . new ( "words must be provided" ) if words . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "add_words" ) headers . merge! ( sdk_headers ) data = { "words" => words } method_url = "/v1/customizations/%s/words" % [ ERB :: Util . url_encode ( customization_id ) ] request ( method : "POST" , url : method_url , headers : headers , json : data , accept_json : true ) nil end


Input: Custom grammars [CODESPLIT] def add_grammar ( customization_id : , grammar_name : , grammar_file : , content_type : , allow_overwrite : nil ) raise ArgumentError . new ( "customization_id must be provided" ) if customization_id . nil? raise ArgumentError . new ( "grammar_name must be provided" ) if grammar_name . nil? raise ArgumentError . new ( "grammar_file must be provided" ) if grammar_file . nil? raise ArgumentError . new ( "content_type must be provided" ) if content_type . nil? headers = { "Content-Type" => content_type } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "add_grammar" ) headers . merge! ( sdk_headers ) params = { "allow_overwrite" => allow_overwrite } data = grammar_file method_url = "/v1/customizations/%s/grammars/%s" % [ ERB :: Util . url_encode ( customization_id ) , ERB :: Util . url_encode ( grammar_name ) ] request ( method : "POST" , url : method_url , headers : headers , params : params , data : data , accept_json : true ) nil end


Input: Custom audio resources [CODESPLIT] def add_audio ( customization_id : , audio_name : , audio_resource : , contained_content_type : nil , allow_overwrite : nil , content_type : nil ) raise ArgumentError . new ( "customization_id must be provided" ) if customization_id . nil? raise ArgumentError . new ( "audio_name must be provided" ) if audio_name . nil? raise ArgumentError . new ( "audio_resource must be provided" ) if audio_resource . nil? headers = { "Contained-Content-Type" => contained_content_type , "Content-Type" => content_type } sdk_headers = Common . new . get_sdk_headers ( "speech_to_text" , "V1" , "add_audio" ) headers . merge! ( sdk_headers ) params = { "allow_overwrite" => allow_overwrite } data = audio_resource method_url = "/v1/acoustic_customizations/%s/audio/%s" % [ ERB :: Util . url_encode ( customization_id ) , ERB :: Util . url_encode ( audio_name ) ] request ( method : "POST" , url : method_url , headers : headers , params : params , data : data , accept_json : true ) nil end


Input: @!method delete_session ( assistant_id : session_id : ) Delete session . Deletes a session explicitly before it times out . @param assistant_id [ String ] Unique identifier of the assistant . You can find the assistant ID of an assistant on the ** Assistants ** tab of the Watson Assistant tool . For information about creating assistants see the [ documentation ] ( https : // console . bluemix . net / docs / services / assistant / assistant - add . html#assistant - add - task ) . [CODESPLIT] def delete_session ( assistant_id : , session_id : ) raise ArgumentError . new ( "assistant_id must be provided" ) if assistant_id . nil? raise ArgumentError . new ( "session_id must be provided" ) if session_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V2" , "delete_session" ) headers . merge! ( sdk_headers ) params = { "version" => @version } method_url = "/v2/assistants/%s/sessions/%s" % [ ERB :: Util . url_encode ( assistant_id ) , ERB :: Util . url_encode ( session_id ) ] request ( method : "DELETE" , url : method_url , headers : headers , params : params , accept_json : true ) nil end


Input: Message [CODESPLIT] def message ( assistant_id : , session_id : , input : nil , context : nil ) raise ArgumentError . new ( "assistant_id must be provided" ) if assistant_id . nil? raise ArgumentError . new ( "session_id must be provided" ) if session_id . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "conversation" , "V2" , "message" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "input" => input , "context" => context } method_url = "/v2/assistants/%s/sessions/%s/message" % [ ERB :: Util . url_encode ( assistant_id ) , ERB :: Util . url_encode ( session_id ) ] response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method initialize ( args ) Construct a new client for the Discovery service . [CODESPLIT] def create_environment ( name : , description : nil , size : nil ) raise ArgumentError . new ( "name must be provided" ) if name . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "discovery" , "V1" , "create_environment" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "name" => name , "description" => description , "size" => size } method_url = "/v1/environments" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: @!method federated_query_notices ( environment_id : collection_ids : filter : nil query : nil natural_language_query : nil aggregation : nil count : nil return_fields : nil offset : nil sort : nil highlight : nil deduplicate_field : nil similar : nil similar_document_ids : nil similar_fields : nil ) Query multiple collection system notices . Queries for notices ( errors or warnings ) that might have been generated by the system . Notices are generated when ingesting documents and performing relevance training . See the [ Discovery service documentation ] ( https : // cloud . ibm . com / docs / services / discovery?topic = discovery - query - concepts#query - concepts ) for more details on the query language . @param environment_id [ String ] The ID of the environment . @param collection_ids [ Array [ String ]] A comma - separated list of collection IDs to be queried against . @param filter [ String ] A cacheable query that excludes documents that don t mention the query content . Filter searches are better for metadata - type searches and for assessing the concepts in the data set . @param query [ String ] A query search returns all documents in your data set with full enrichments and full text but with the most relevant documents listed first . Use a query search when you want to find the most relevant search results . You cannot use ** natural_language_query ** and ** query ** at the same time . @param natural_language_query [ String ] A natural language query that returns relevant documents by utilizing training data and natural language understanding . You cannot use ** natural_language_query ** and ** query ** at the same time . @param aggregation [ String ] An aggregation search that returns an exact answer by combining query search with filters . Useful for applications to build lists tables and time series . For a full list of possible aggregations see the Query reference . @param count [ Fixnum ] Number of results to return . The maximum for the ** count ** and ** offset ** values together in any one query is ** 10000 ** . @param return_fields [ Array [ String ]] A comma - separated list of the portion of the document hierarchy to return . @param offset [ Fixnum ] The number of query results to skip at the beginning . For example if the total number of results that are returned is 10 and the offset is 8 it returns the last two results . The maximum for the ** count ** and ** offset ** values together in any one query is ** 10000 ** . @param sort [ Array [ String ]] A comma - separated list of fields in the document to sort on . You can optionally specify a sort direction by prefixing the field with - for descending or + for ascending . Ascending is the default sort direction if no prefix is specified . @param highlight [ Boolean ] When true a highlight field is returned for each result which contains the fields which match the query with <em > < / em > tags around the matching query terms . @param deduplicate_field [ String ] When specified duplicate results based on the field specified are removed from the returned results . Duplicate comparison is limited to the current query only ** offset ** is not considered . This parameter is currently Beta functionality . @param similar [ Boolean ] When true results are returned based on their similarity to the document IDs specified in the ** similar . document_ids ** parameter . @param similar_document_ids [ Array [ String ]] A comma - separated list of document IDs to find similar documents . [CODESPLIT] def federated_query_notices ( environment_id : , collection_ids : , filter : nil , query : nil , natural_language_query : nil , aggregation : nil , count : nil , return_fields : nil , offset : nil , sort : nil , highlight : nil , deduplicate_field : nil , similar : nil , similar_document_ids : nil , similar_fields : nil ) raise ArgumentError . new ( "environment_id must be provided" ) if environment_id . nil? raise ArgumentError . new ( "collection_ids must be provided" ) if collection_ids . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "discovery" , "V1" , "federated_query_notices" ) headers . merge! ( sdk_headers ) params = { "version" => @version , "collection_ids" => collection_ids . to_a , "filter" => filter , "query" => query , "natural_language_query" => natural_language_query , "aggregation" => aggregation , "count" => count , "return" => return_fields . to_a , "offset" => offset , "sort" => sort . to_a , "highlight" => highlight , "deduplicate.field" => deduplicate_field , "similar" => similar , "similar.document_ids" => similar_document_ids . to_a , "similar.fields" => similar_fields . to_a } method_url = "/v1/environments/%s/notices" % [ ERB :: Util . url_encode ( environment_id ) ] response = request ( method : "GET" , url : method_url , headers : headers , params : params , accept_json : true ) response end


Input: Events and feedback [CODESPLIT] def create_event ( type : , data : ) raise ArgumentError . new ( "type must be provided" ) if type . nil? raise ArgumentError . new ( "data must be provided" ) if data . nil? headers = { } sdk_headers = Common . new . get_sdk_headers ( "discovery" , "V1" , "create_event" ) headers . merge! ( sdk_headers ) params = { "version" => @version } data = { "type" => type , "data" => data } method_url = "/v1/events" response = request ( method : "POST" , url : method_url , headers : headers , params : params , json : data , accept_json : true ) response end


Input: Pad a string out to n characters with zeros [CODESPLIT] def zero_pad ( n , message ) len = message . bytesize if len == n message elsif len > n raise LengthError , "String too long for zero-padding to #{n} bytes" else message + zeros ( n - len ) end end


Input: Check the length of the passed in string [CODESPLIT] def check_length ( string , length , description ) if string . nil? # code below is runs only in test cases # nil can't be converted to str with #to_str method raise LengthError , "#{description} was nil (Expected #{length.to_int})" , caller end if string . bytesize != length . to_int raise LengthError , "#{description} was #{string.bytesize} bytes (Expected #{length.to_int})" , caller end true end


Input: Check a passed in string converting the argument if necessary [CODESPLIT] def check_string ( string , length , description ) check_string_validation ( string ) string = string . to_s check_length ( string , length , description ) string end


Input: Check a passed in string convertion if necessary [CODESPLIT] def check_hmac_key ( string , _description ) check_string_validation ( string ) string = string . to_str if string . bytesize . zero? raise LengthError , "#{Description} was #{string.bytesize} bytes (Expected more than 0)" , caller end string end


Input: Check a passed string is it valid [CODESPLIT] def check_string_validation ( string ) raise TypeError , "can't convert #{string.class} into String with #to_str" unless string . respond_to? :to_str string = string . to_str raise EncodingError , "strings must use BINARY encoding (got #{string.encoding})" if string . encoding != Encoding :: BINARY end


Input: Compare two 64 byte strings in constant time [CODESPLIT] def verify64 ( one , two ) return false unless two . bytesize == 64 && one . bytesize == 64 c_verify64 ( one , two ) end


Input: Compare two 64 byte strings in constant time [CODESPLIT] def verify64! ( one , two ) check_length ( one , 64 , "First message" ) check_length ( two , 64 , "Second message" ) c_verify64 ( one , two ) end


Input: Compare two 32 byte strings in constant time [CODESPLIT] def verify32 ( one , two ) return false unless two . bytesize == 32 && one . bytesize == 32 c_verify32 ( one , two ) end


Input: Compare two 32 byte strings in constant time [CODESPLIT] def verify32! ( one , two ) check_length ( one , 32 , "First message" ) check_length ( two , 32 , "Second message" ) c_verify32 ( one , two ) end


Input: Compare two 16 byte strings in constant time [CODESPLIT] def verify16 ( one , two ) return false unless two . bytesize == 16 && one . bytesize == 16 c_verify16 ( one , two ) end


Input: Compare two 16 byte strings in constant time [CODESPLIT] def verify16! ( one , two ) check_length ( one , 16 , "First message" ) check_length ( two , 16 , "Second message" ) c_verify16 ( one , two ) end


Input: Compute authenticator for message [CODESPLIT] def auth ( message ) authenticator = Util . zeros ( tag_bytes ) message = message . to_str compute_authenticator ( authenticator , message ) authenticator end


Input: Verifies the given authenticator with the message . [CODESPLIT] def verify ( authenticator , message ) auth = authenticator . to_s Util . check_length ( auth , tag_bytes , "Provided authenticator" ) verify_message ( auth , message ) || raise ( BadAuthenticatorError , "Invalid authenticator provided, message is corrupt" ) end


Input: Encrypts the message with a random nonce [CODESPLIT] def box ( message ) nonce = generate_nonce cipher_text = @box . box ( nonce , message ) nonce + cipher_text end


Input: Decrypts the ciphertext with a random nonce [CODESPLIT] def open ( enciphered_message ) nonce , ciphertext = extract_nonce ( enciphered_message . to_s ) @box . open ( nonce , ciphertext ) end


Input: After a fork the appender thread is not running start it if it is not running . [CODESPLIT] def reopen each do | appender | begin next unless appender . respond_to? ( :reopen ) logger . trace "Reopening appender: #{appender.name}" appender . reopen rescue Exception => exc logger . error "Failed to re-open appender: #{appender.inspect}" , exc end end logger . trace 'All appenders re-opened' end


Input: Backward compatibility [CODESPLIT] def convert_old_appender_args ( appender , level ) options = { } options [ :level ] = level if level if appender . is_a? ( String ) options [ :file_name ] = appender elsif appender . is_a? ( IO ) options [ :io ] = appender elsif appender . is_a? ( Symbol ) || appender . is_a? ( Subscriber ) options [ :appender ] = appender else options [ :logger ] = appender end warn "[DEPRECATED] SemanticLogger.add_appender parameters have changed. Please use: #{options.inspect}" options end


Input: Give each appender its own logger for logging . For example trace messages sent to services or errors when something fails . [CODESPLIT] def logger @logger ||= begin logger = SemanticLogger :: Processor . logger . clone logger . name = self . class . name logger end end


Input: Dynamically supply the log level with every measurement call [CODESPLIT] def measure ( level , message , params = { } , & block ) index = Levels . index ( level ) if level_index <= index measure_internal ( level , index , message , params , block ) elsif block yield ( params ) end end


Input: Log a thread backtrace [CODESPLIT] def backtrace ( thread : Thread . current , level : :warn , message : 'Backtrace:' , payload : nil , metric : nil , metric_amount : nil ) log = Log . new ( name , level ) return false unless meets_log_level? ( log ) backtrace = if thread == Thread . current Utils . extract_backtrace else log . thread_name = thread . name log . tags = ( thread [ :semantic_logger_tags ] || [ ] ) . clone log . named_tags = ( thread [ :semantic_logger_named_tags ] || { } ) . clone thread . backtrace end # TODO: Keep backtrace instead of transforming into a text message at this point # Maybe log_backtrace: true if backtrace message += "\n" message << backtrace . join ( "\n" ) end if log . assign ( message : message , backtrace : backtrace , payload : payload , metric : metric , metric_amount : metric_amount ) && ! filtered? ( log ) self . log ( log ) else false end end


Input: Add the tags or named tags to the list of tags to log for this thread whilst the supplied block is active . [CODESPLIT] def tagged ( * tags , & block ) # Allow named tags to be passed into the logger if tags . size == 1 tag = tags [ 0 ] return yield if tag . nil? || tag == '' return tag . is_a? ( Hash ) ? SemanticLogger . named_tagged ( tag , block ) : SemanticLogger . fast_tag ( tag . to_s , block ) end # Need to flatten and reject empties to support calls from Rails 4 new_tags = tags . flatten . collect ( :to_s ) . reject ( :empty? ) SemanticLogger . tagged ( new_tags , block ) end


Input: Returns the list of tags pushed after flattening them out and removing blanks [CODESPLIT] def push_tags ( * tags ) # Need to flatten and reject empties to support calls from Rails 4 new_tags = tags . flatten . collect ( :to_s ) . reject ( :empty? ) SemanticLogger . push_tags ( new_tags ) end


Input: Whether to log the supplied message based on the current filter if any [CODESPLIT] def filtered? ( log ) return false if @filter . nil? @filter . is_a? ( Regexp ) ? ( @filter =~ log . name ) . nil? : @filter . call ( log ) != true end


Input: Log message at the specified level [CODESPLIT] def log_internal ( level , index , message = nil , payload = nil , exception = nil , & block ) log = Log . new ( name , level , index ) should_log = if payload . nil? && exception . nil? && message . is_a? ( Hash ) # Check if someone just logged a hash payload instead of meaning to call semantic logger if message . key? ( :message ) || message . key? ( :payload ) || message . key? ( :exception ) || message . key? ( :metric ) log . assign ( message ) else log . assign_positional ( nil , message , nil , block ) end else log . assign_positional ( message , payload , exception , block ) end # Log level may change during assign due to :on_exception_level self . log ( log ) if should_log && should_log? ( log ) end


Input: Measure the supplied block and log the message [CODESPLIT] def measure_internal ( level , index , message , params ) exception = nil result = nil # Single parameter is a hash if params . empty? && message . is_a? ( Hash ) params = message message = nil end start = Process . clock_gettime ( Process :: CLOCK_MONOTONIC ) begin if block_given? result = if ( silence_level = params [ :silence ] ) # In case someone accidentally sets `silence: true` instead of `silence: :error` silence_level = :error if silence_level == true silence ( silence_level ) { yield ( params ) } else yield ( params ) end end rescue Exception => exc exception = exc ensure # Must use ensure block otherwise a `return` in the yield above will skip the log entry log = Log . new ( name , level , index ) exception ||= params [ :exception ] message = params [ :message ] if params [ :message ] duration = if block_given? 1_000.0 * ( Process . clock_gettime ( Process :: CLOCK_MONOTONIC ) - start ) else params [ :duration ] || raise ( 'Mandatory block missing when :duration option is not supplied' ) end # Extract options after block completes so that block can modify any of the options payload = params [ :payload ] # May return false due to elastic logging should_log = log . assign ( message : message , payload : payload , min_duration : params [ :min_duration ] || 0.0 , exception : exception , metric : params [ :metric ] , metric_amount : params [ :metric_amount ] , duration : duration , log_exception : params [ :log_exception ] || :partial , on_exception_level : params [ :on_exception_level ] ) # Log level may change during assign due to :on_exception_level self . log ( log ) if should_log && should_log? ( log ) raise exception if exception result end end


Input: For measuring methods and logging their duration . [CODESPLIT] def measure_method ( index : , level : , message : , min_duration : , metric : , log_exception : , on_exception_level : ) # Ignores filter, silence, payload exception = nil start = Process . clock_gettime ( Process :: CLOCK_MONOTONIC ) begin yield rescue Exception => exc exception = exc ensure log = Log . new ( name , level , index ) # May return false due to elastic logging should_log = log . assign ( message : message , min_duration : min_duration , exception : exception , metric : metric , duration : 1_000.0 * ( Process . clock_gettime ( Process :: CLOCK_MONOTONIC ) - start ) , log_exception : log_exception , on_exception_level : on_exception_level ) # Log level may change during assign due to :on_exception_level log ( log ) if should_log && should_log? ( log ) raise exception if exception end end


Input: Returns a Logger instance [CODESPLIT] def log ( log , message = nil , progname = nil , & block ) # Compatibility with ::Logger return add ( log , message , progname , block ) unless log . is_a? ( SemanticLogger :: Log ) Logger . call_subscribers ( log ) Logger . processor . log ( log ) end


Input: Assign named arguments to this log entry supplying defaults where applicable [CODESPLIT] def assign ( message : nil , payload : nil , min_duration : 0.0 , exception : nil , metric : nil , metric_amount : nil , duration : nil , backtrace : nil , log_exception : :full , on_exception_level : nil , dimensions : nil ) # Elastic logging: Log when :duration exceeds :min_duration # Except if there is an exception when it will always be logged if duration self . duration = duration return false if ( duration < min_duration ) && exception . nil? end self . message = message if payload && payload . is_a? ( Hash ) self . payload = payload elsif payload self . message = message . nil? ? payload . to_s : "#{message} -- #{payload}" self . payload = nil end if exception case log_exception when :full self . exception = exception when :partial self . message = "#{message} -- Exception: #{exception.class}: #{exception.message}" when nil , :none # Log the message without the exception that was raised nil else raise ( ArgumentError , "Invalid value:#{log_exception.inspect} for argument :log_exception" ) end # On exception change the log level if on_exception_level self . level = on_exception_level self . level_index = Levels . index ( level ) end end if backtrace self . backtrace = Utils . extract_backtrace ( backtrace ) elsif level_index >= SemanticLogger . backtrace_level_index self . backtrace = Utils . extract_backtrace end if metric self . metric = metric self . metric_amount = metric_amount self . dimensions = dimensions end true end


Input: Assign positional arguments to this log entry supplying defaults where applicable [CODESPLIT] def assign_positional ( message = nil , payload = nil , exception = nil ) # Exception being logged? # Under JRuby a java exception is not a Ruby Exception #   Java::JavaLang::ClassCastException.new.is_a?(Exception) => false if exception . nil? && payload . nil? && message . respond_to? ( :backtrace ) && message . respond_to? ( :message ) exception = message message = nil elsif exception . nil? && payload && payload . respond_to? ( :backtrace ) && payload . respond_to? ( :message ) exception = payload payload = nil elsif payload && ! payload . is_a? ( Hash ) message = message . nil? ? payload : "#{message} -- #{payload}" payload = nil end # Add result of block as message or payload if not nil if block_given? && ( result = yield ) if result . is_a? ( String ) message = message . nil? ? result : "#{message} -- #{result}" assign ( message : message , payload : payload , exception : exception ) elsif message . nil? && result . is_a? ( Hash ) && %i[ message payload exception ] . any? { | k | result . key? k } assign ( result ) elsif payload &. respond_to? ( :merge ) assign ( message : message , payload : payload . merge ( result ) , exception : exception ) else assign ( message : message , payload : result , exception : exception ) end else assign ( message : message , payload : payload , exception : exception ) end end


Input: Call the block for exception and any nested exception [CODESPLIT] def each_exception # With thanks to https://github.com/bugsnag/bugsnag-ruby/blob/6348306e44323eee347896843d16c690cd7c4362/lib/bugsnag/notification.rb#L81 depth = 0 exceptions = [ ] ex = exception while ! ex . nil? && ! exceptions . include? ( ex ) && exceptions . length < MAX_EXCEPTIONS_TO_UNWRAP exceptions << ex yield ( ex , depth ) depth += 1 ex = if ex . respond_to? ( :cause ) && ex . cause ex . cause elsif ex . respond_to? ( :continued_exception ) && ex . continued_exception ex . continued_exception elsif ex . respond_to? ( :original_exception ) && ex . original_exception ex . original_exception end end end


Input: Returns [ String ] the exception backtrace including all of the child / caused by exceptions [CODESPLIT] def backtrace_to_s trace = '' each_exception do | exception , i | if i . zero? trace = ( exception . backtrace || [ ] ) . join ( "\n" ) else trace << "\nCause: #{exception.class.name}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}" end end trace end


Input: Returns [ String ] the duration in human readable form [CODESPLIT] def duration_human return nil unless duration seconds = duration / 1000 if seconds >= 86_400.0 # 1 day "#{(seconds / 86_400).to_i}d #{Time.at(seconds).strftime('%-Hh %-Mm')}" elsif seconds >= 3600.0 # 1 hour Time . at ( seconds ) . strftime ( '%-Hh %-Mm' ) elsif seconds >= 60.0 # 1 minute Time . at ( seconds ) . strftime ( '%-Mm %-Ss' ) elsif seconds >= 1.0 # 1 second "#{format('%.3f', seconds)}s" else duration_to_s end end


Input: Extract the filename and line number from the last entry in the supplied backtrace [CODESPLIT] def extract_file_and_line ( stack , short_name = false ) match = CALLER_REGEXP . match ( stack . first ) [ short_name ? File . basename ( match [ 1 ] ) : match [ 1 ] , match [ 2 ] . to_i ] end


Input: Returns [ String String ] the file_name and line_number from the backtrace supplied in either the backtrace or exception [CODESPLIT] def file_name_and_line ( short_name = false ) stack = backtrace || exception &. backtrace extract_file_and_line ( stack , short_name ) if stack &. size &. positive? end


Input: DEPRECATED : Use SemanticLogger :: Formatters :: Raw [CODESPLIT] def to_h ( host = SemanticLogger . host , application = SemanticLogger . application ) logger = DeprecatedLogger . new ( host , application ) SemanticLogger :: Formatters :: Raw . new . call ( self , logger ) end


Input: Returns a human readable string that contains + corrections + . This formatter is designed to be less verbose to not take too much screen space while being helpful enough to the user . [CODESPLIT] def message_for ( corrections ) return "" if corrections . empty? output = "\n\n    Did you mean? " . dup output << corrections . join ( "\n                  " ) output << "\n " end


Input: : nodoc : This code is based directly on the Text gem implementation Copyright ( c ) 2006 - 2013 Paul Battley Michael Neumann Tim Fletcher . [CODESPLIT] def distance ( str1 , str2 ) n = str1 . length m = str2 . length return m if n . zero? return n if m . zero? d = ( 0 .. m ) . to_a x = nil # to avoid duplicating an enumerable object, create it outside of the loop str2_codepoints = str2 . codepoints str1 . each_codepoint . with_index ( 1 ) do | char1 , i | j = 0 while j < m cost = ( char1 == str2_codepoints [ j ] ) ? 0 : 1 x = min3 ( d [ j + 1 ] + 1 , # insertion i + 1 , # deletion d [ j ] + cost # substitution ) d [ j ] = i i = x j += 1 end d [ m ] = x end x end


Input: detects the minimum value out of three arguments . This method is faster than [ a b c ] . min and puts less GC pressure . See https : // github . com / yuki24 / did_you_mean / pull / 1 for a performance benchmark . [CODESPLIT] def min3 ( a , b , c ) if a < b && a < c a elsif b < c b else c end end


Input: Used by flor when it looks up for a variable and finds nothing . The last step is to ask the ganger if it knows about a tasker under the given ( domain and ) name . [CODESPLIT] def has_tasker? ( exid , name ) #return false if RESERVED_NAMES.include?(name) d = Flor . domain ( exid ) ! ! ( @unit . loader . tasker ( d , 'ganger' ) || @unit . loader . tasker ( d , 'tasker' ) || @unit . loader . tasker ( d , name ) ) end


Input: Called by Flor :: Scheduler . The ganger then has to hand the task ( the message ) to the proper tasker . [CODESPLIT] def task ( executor , message ) domain = message [ 'exid' ] . split ( '-' , 2 ) . first tname = message [ 'tasker' ] tconf = ( ! message [ 'routed' ] && ( @unit . loader . tasker ( domain , 'ganger' , message ) || @unit . loader . tasker ( domain , 'tasker' , message ) ) ) || @unit . loader . tasker ( domain , tname , message ) fail ArgumentError . new ( "tasker #{tname.inspect} not found" ) unless tconf if tconf . is_a? ( Array ) points = [ nil , message [ 'point' ] ] points << 'detask' if points . include? ( 'cancel' ) tconf = tconf . find { | h | points . include? ( h [ 'point' ] ) } end message [ 'tconf' ] = tconf unless tconf [ 'include_tconf' ] == false message [ 'vars' ] = gather_vars ( executor , tconf , message ) m = dup_message ( message ) # # the tasker gets a copy of the message (and it can play with it # to its heart content), meanwhile the message is handed to the # "post" notifiers. @unit . caller . call ( self , tconf , m ) # # might return a re-routing message, # especially if it's a domain tasker end


Input: By default taskers don t see the flor variables in the execution . If include_vars or exclude_vars is present in the configuration of the tasker some or all of the variables are passed . [CODESPLIT] def gather_vars ( executor , tconf , message ) # try to return before a potentially costly call to executor.vars(nid) return nil if ( tconf . keys & %w[ include_vars exclude_vars ] ) . empty? # default behaviour, don't pass variables to taskers iv = expand_filter ( tconf [ 'include_vars' ] ) return nil if iv == false ev = expand_filter ( tconf [ 'exclude_vars' ] ) return { } if ev == true vars = executor . vars ( message [ 'nid' ] ) return vars if iv == true vars = vars . select { | k , v | var_match ( k , iv ) } if iv vars = vars . reject { | k , v | var_match ( k , ev ) } if ev vars end


Input: NB : logger configuration entries start with hok_ [CODESPLIT] def shutdown @hooks . each do | n , o , hook , b | hook . shutdown if hook . respond_to? ( :shutdown ) end end


Input: Dumps all or some of the executions to a JSON string . See Scheduler#load for importing . [CODESPLIT] def dump ( io = nil , opts = nil , & block ) io , opts = nil , io if io . is_a? ( Hash ) opts ||= { } o = lambda { | k | v = opts [ k ] || opts [ "#{k}s" . to_sym ] ; v ? Array ( v ) : nil } # exis = o [ :exid ] doms = o [ :domain ] sdms = o [ :strict_domain ] || o [ :sdomain ] # filter = lambda { | q | q = q . where ( exid : exis ) if exis q = q . where { Sequel . | ( doms . inject ( [ ] ) { | a , d | a . concat ( [ { domain : d } , Sequel . like ( :domain , d + '.%' ) ] ) } ) } if doms q = q . where ( domain : sdms ) if sdms q } exs , tms , tps , pts = storage . db . transaction { [ filter [ executions ] . collect ( :to_h ) , filter [ timers ] . collect ( :to_h ) , filter [ traps ] . collect ( :to_h ) , filter [ pointers ] . collect ( :to_h ) ] } o = io ? io : StringIO . new h = { timestamp : Flor . tstamp , executions : exs , timers : tms , traps : tps , pointers : pts } block . call ( h ) if block JSON . dump ( h , o ) io ? io : o . string end


Input: Read a previous JSON dump and loads it into the storage . Can be useful when testing dumping once and reloading multiple times to test variants . [CODESPLIT] def load ( string_or_io , opts = { } , & block ) s = string_or_io s = s . read if s . respond_to? ( :read ) string_or_io . close if string_or_io . respond_to? ( :close ) && opts [ :close ] h = JSON . load ( s ) mks = DUMP_KEYS - h . keys fail Flor :: FlorError . new ( "missing keys #{mks.inspect}" ) if mks . any? o = lambda { | k | v = opts [ k ] || opts [ "#{k}s" . to_sym ] ; v ? Array ( v ) : nil } # exis = o [ :exid ] doms = o [ :domain ] sdms = o [ :strict_domain ] || o [ :sdomain ] # doms = doms . collect { | d | / \A #{ d } \. #{ Flor :: NAME_REX } \z / } if doms counts = { executions : 0 , timers : 0 , traps : 0 , pointers : 0 , total : 0 } storage . db . transaction do ( DUMP_KEYS - %w[ timestamp ] ) . each do | k | y = k . to_sym cla = storage . send ( k ) cols = cla . columns rows = h [ k ] . inject ( [ ] ) { | a , hh | next a if exis && ! exis . include? ( hh [ 'exid' ] ) next a if doms && ! doms . find { | d | d . match ( hh [ 'domain' ] ) } next a if sdms && ! sdms . include? ( hh [ 'domain' ] ) counts [ y ] += 1 counts [ :total ] += 1 vals = cla . from_h ( hh ) a << cols . collect { | c | vals [ c ] } } cla . import ( cols , rows ) if rows . any? end block . call ( h ) if block end counts end


Input: def procedures ( path ) [CODESPLIT] def library ( domain , name = nil , opts = { } ) path , key = split ( domain , name ) libs = entries ( 'libraries' , path ) if opts [ :subflows ] # used by "graft"/"import" libs += entries ( 'sublibraries' , path ) libs = libs . sort_by { | pa , _ , _ | pa . count ( '.' ) } end libs . each { | pa , ke , va | next unless ke == key return [ [ pa , ke ] . join ( '.' ) , va ] } nil end


Input: point for messages that after consumption are conserved in the execution s closing_messages array [CODESPLIT] def do_run @unit . logger . log_run_start ( self ) counter_next ( 'runs' ) t0 = Time . now ( @unit . conf [ 'exe_max_messages' ] || 77 ) . times do | i | break if @shutdown m = @messages . shift break unless m m = ( @messages << m ) . shift if m [ 'point' ] == 'terminated' && @messages . any? # # handle 'terminated' messages last ms = process ( m ) @consumed << m ims , oms = ms . partition { | mm | mm [ 'exid' ] == @exid } # qui est "in", qui est "out"? counter_add ( 'omsgs' , oms . size ) # keep track of "out" messages, messages to other executions @messages . concat ( ims ) @unit . storage . put_messages ( oms ) end @alive = false @execution . merge! ( closing_messages : @consumed . select { | m | CLOSING_POINTS . include? ( m [ 'point' ] ) } ) @unit . storage . put_execution ( @execution ) @unit . storage . consume ( @consumed ) @unit . storage . put_messages ( @messages ) du = Time . now - t0 t0 = Flor . tstamp ( t0 ) @unit . logger . log_run_end ( self , t0 , du ) @unit . hooker . notify ( self , make_end_message ( t0 , du , @execution [ 'size' ] ) ) @consumed . clear rescue Exception => exc # TODO eventually, have a dump dir fn = [ 'flor' , @unit . conf [ 'env' ] , @unit . identifier , @exid , 'r' + counter ( 'runs' ) . to_s ] . collect ( :to_s ) . join ( '_' ) + '.dump' @unit . logger . error ( "#{self.class}#do_run()" , exc , "(dumping to #{fn})" ) File . open ( fn , 'wb' ) do | f | f . puts ( Flor . to_pretty_s ( { execution : @execution , messages : @messages , consumed : @consumed , traps : @traps . collect ( :to_h ) , exid : @exid , alive : @alive , shutdown : @shutdown , thread : [ @thread . object_id , @thread . to_s ] } ) ) f . puts ( '-' * 80 ) f . puts ( on_do_run_exc ( exc ) ) end #puts on_do_run_exc(exc) # dump notification above end


Input: For domain taskers [CODESPLIT] def route ( name ) if name . is_a? ( String ) [ Flor . dup_and_merge ( @message , 'tasker' => name , 'original_tasker' => @message [ 'tasker' ] , 'routed' => true ) ] else [ Flor . dup_and_merge ( @message , 'routed' => ! ! name ) ] end end


Input: So that #reply may be called with reply reply ( [CODESPLIT] def derive_message ( m ) exid = m [ 'exid' ] nid = m [ 'nid' ] pl = m [ 'payload' ] return m if Flor . is_exid? ( exid ) && Flor . is_nid? ( nid ) && pl . is_a? ( Hash ) m = Flor . to_string_keyed_hash ( m ) h = Flor . dup ( @message ) ks = m . keys if ks == [ 'payload' ] h [ 'payload' ] = m [ 'payload' ] elsif ( ks & %w[ ret set unset ] ) . size > 0 pl = ( h [ 'payload' ] ||= { } ) pl [ 'ret' ] = m [ 'ret' ] if m . has_key? ( 'ret' ) ( m [ 'set' ] || { } ) . each { | k , v | pl [ k ] = v } ( m [ 'unset' ] || [ ] ) . each { | k | pl . delete ( k . to_s ) } else h [ 'payload' ] = m end h end


Input: Avoid the proc / cancel problem upstreams in ConfExecutor by ignoring non - core procedures keeping this around for now [CODESPLIT] def extract_filters ( h ) r = { } r [ :consumed ] = h [ 'consumed' ] r [ :point ] = Flor . h_fetch_a ( h , 'points' , 'point' , nil ) r [ :nid ] = Flor . h_fetch_a ( h , 'nids' , 'nid' , nil ) r [ :heap ] = Flor . h_fetch_a ( h , 'heaps' , 'heap' , nil ) r [ :heat ] = Flor . h_fetch_a ( h , 'heats' , 'heat' , nil ) #opts[:name] = data['names'] r end


Input: TODO eventually merge with Waiter . parse_serie [CODESPLIT] def message_match? ( msg_s , ountil ) return false unless ountil ms = msg_s ; ms = [ ms ] if ms . is_a? ( Hash ) nid , point = ountil . split ( ' ' ) ms . find { | m | m [ 'nid' ] == nid && m [ 'point' ] == point } end