def atom_choose_impl(self, focus_atoms, config_base): """Implemented factory method to choosing atoms. Args: focus_atoms: The atoms to blend. config_base: A Node to save custom config. :param focus_atoms: list[Atom] :param config_base: Atom """ # Choose all atoms in AtomSpace if focus_atoms was not given. focus_atoms = self.a.get_atoms_by_type(types.Atom) \ if len(focus_atoms) is 0 \ else focus_atoms atom_type = BlendConfig().get_str( self.a, "choose-atom-type", config_base ) least_count = BlendConfig().get_int( self.a, "choose-least-count", config_base ) # Check if given atom_type is valid or not. try: atom_type = types.__dict__[atom_type] except KeyError: atom_type = types.Node # Check if given least_count is valid or not. if least_count < 0: self.last_status = blending_status.NOT_ENOUGH_ATOMS return # Call the actual choosing algorithm method. self.__get_atoms_all(focus_atoms, atom_type, least_count)
def make_default_config(self): """Initialize a default config for this class.""" super(self.__class__, self).make_default_config() BlendConfig().update(self.a, "connect-check-type", "SimilarityLink") BlendConfig().update(self.a, "connect-strength-diff-limit", "0.3") BlendConfig().update(self.a, "connect-confidence-above-limit", "0.7") BlendConfig().update(self.a, "connect-viable-atoms-count-limit", "100")
def atom_choose_impl(self, focus_atoms, config_base): atom_type = BlendConfig().get_str( self.a, "choose-atom-type", config_base ) least_count = BlendConfig().get_int( self.a, "choose-least-count", config_base ) sti_min = BlendConfig().get_str(self.a, "choose-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "choose-sti-max", config_base) try: atom_type = types.__dict__[atom_type] except KeyError: atom_type = types.Node if least_count < 0: self.last_status = blending_status.NOT_ENOUGH_ATOMS return sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None self.__get_atoms_in_sti_range( focus_atoms, atom_type, least_count, sti_min, sti_max )
def link_connect_impl(self, decided_atoms, merged_atom, config_base): """Implemented factory method to connecting links. Args: decided_atoms: The source atoms to make new atom. merged_atom: The atom to connect new links. config_base: A Node to save custom config. :param decided_atoms: list[Atom] :param merged_atom: Atom :param config_base: Atom """ check_type_str = BlendConfig().get_str(self.a, "connect-check-type", config_base) strength_diff_threshold = BlendConfig().get_str( self.a, "connect-strength-diff-limit", config_base) confidence_above_threshold = BlendConfig().get_str( self.a, "connect-confidence-above-limit", config_base) viable_atoms_count_threshold = BlendConfig().get_int( self.a, "connect-viable-atoms-count-limit", config_base) # Check if given atom_type is valid or not. try: self.check_type = types.__dict__[check_type_str] except KeyError: self.check_type = types.Node # Check if given threshold value is valid or not. self.strength_diff_limit = float(strength_diff_threshold) self.confidence_above_limit = float(confidence_above_threshold) self.viable_atoms_count_threshold = viable_atoms_count_threshold self.__connect_viable_conflict_links(decided_atoms, merged_atom)
def new_blend_make_impl(self, decided_atoms, config_base): # Make the new blend node. atom_prefix = BlendConfig().get_str(self.a, "make-atom-prefix", config_base) atom_separator = BlendConfig().get_str(self.a, "make-atom-separator", config_base) atom_postfix = BlendConfig().get_str(self.a, "make-atom-postfix", config_base) self.__make_atom_from_all(decided_atoms, atom_prefix, atom_separator, atom_postfix)
def link_connect_impl(self, decided_atoms, merged_atom, config_base): """Implemented factory method to connecting links. Args: decided_atoms: The source atoms to make new atom. merged_atom: The atom to connect new links. config_base: A Node to save custom config. :param decided_atoms: list[Atom] :param merged_atom: Atom :param config_base: Atom """ check_type_str = BlendConfig().get_str( self.a, "connect-check-type", config_base ) strength_diff_threshold = BlendConfig().get_str( self.a, "connect-strength-diff-limit", config_base ) confidence_above_threshold = BlendConfig().get_str( self.a, "connect-confidence-above-limit", config_base ) data_n_gram_limit = BlendConfig().get_str( self.a, "connect-data-n-gram-limit", config_base ) evaluate_n_gram_limit = BlendConfig().get_str( self.a, "connect-evaluate-n-gram-limit", config_base ) inter_info_strength_above_limit = BlendConfig().get_str( self.a, "connect-inter-info-strength-above-limit", config_base ) # Check if given atom_type is valid or not. try: self.check_type = types.__dict__[check_type_str] except KeyError: self.check_type = types.Node # Check if given threshold value is valid or not. self.strength_diff_limit = float(strength_diff_threshold) self.confidence_above_limit = float(confidence_above_threshold) self.data_n_gram_limit = \ int(data_n_gram_limit) \ if data_n_gram_limit.isdigit()\ else -1 self.evaluate_n_gram_limit = \ int(evaluate_n_gram_limit) \ if evaluate_n_gram_limit.isdigit() \ else -1 self.inter_info_strength_above_limit = \ float(inter_info_strength_above_limit) self.__connect_best_interaction_information_conflict_links( decided_atoms, merged_atom )
def blending_decide_impl(self, chosen_atoms, config_base): result_atoms_count = BlendConfig().get_int( self.a, "decide-result-atoms-count", config_base ) sti_min = BlendConfig().get_str(self.a, "decide-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "decide-sti-max", config_base) sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None self.__decide_atoms_best_sti( chosen_atoms, result_atoms_count, sti_min, sti_max )
def atom_choose_impl(self, focus_atoms, config_base): """Implemented factory method to choosing atoms. Args: focus_atoms: The atoms to blend. config_base: A Node to save custom config. :param focus_atoms: list[Atom] :param config_base: Atom """ atom_type = BlendConfig().get_str(self.a, "choose-atom-type", config_base) least_count = BlendConfig().get_int(self.a, "choose-least-count", config_base) sti_min = BlendConfig().get_str(self.a, "choose-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "choose-sti-max", config_base) # Check if given atom_type is valid or not. try: atom_type = types.__dict__[atom_type] except KeyError: atom_type = types.Node # Check if given least_count is valid or not. if least_count < 0: self.last_status = blending_status.NOT_ENOUGH_ATOMS return # Check if given range of STI value is valid or not. sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None # Call the actual choosing algorithm method. self.__get_atoms_in_sti_range(focus_atoms, atom_type, least_count, sti_min, sti_max)
def link_connect_impl(self, decided_atoms, merged_atom, config_base): """Implemented factory method to connecting links. Args: decided_atoms: The source atoms to make new atom. merged_atom: The atom to connect new links. config_base: A Node to save custom config. :param decided_atoms: list[Atom] :param merged_atom: Atom :param config_base: Atom """ check_type_str = BlendConfig().get_str(self.a, "connect-check-type", config_base) strength_diff_threshold = BlendConfig().get_str( self.a, "connect-strength-diff-limit", config_base) confidence_above_threshold = BlendConfig().get_str( self.a, "connect-confidence-above-limit", config_base) data_n_gram_limit = BlendConfig().get_str(self.a, "connect-data-n-gram-limit", config_base) evaluate_n_gram_limit = BlendConfig().get_str( self.a, "connect-evaluate-n-gram-limit", config_base) inter_info_strength_above_limit = BlendConfig().get_str( self.a, "connect-inter-info-strength-above-limit", config_base) # Check if given atom_type is valid or not. try: self.check_type = types.__dict__[check_type_str] except KeyError: self.check_type = types.Node # Check if given threshold value is valid or not. self.strength_diff_limit = float(strength_diff_threshold) self.confidence_above_limit = float(confidence_above_threshold) self.data_n_gram_limit = \ int(data_n_gram_limit) \ if data_n_gram_limit.isdigit()\ else -1 self.evaluate_n_gram_limit = \ int(evaluate_n_gram_limit) \ if evaluate_n_gram_limit.isdigit() \ else -1 self.inter_info_strength_above_limit = \ float(inter_info_strength_above_limit) self.__connect_best_interaction_information_conflict_links( decided_atoms, merged_atom)
def new_blend_make_impl(self, decided_atoms, config_base): """Implemented factory method to making new atom. Args: decided_atoms: The source atoms to make new atom. config_base: A Node to save custom config. :param decided_atoms: list[Atom] :param config_base: Atom """ atom_prefix = BlendConfig().get_str(self.a, "make-atom-prefix", config_base) atom_separator = BlendConfig().get_str(self.a, "make-atom-separator", config_base) atom_postfix = BlendConfig().get_str(self.a, "make-atom-postfix", config_base) self.__make_atom_from_all(decided_atoms, atom_prefix, atom_separator, atom_postfix)
def link_connect_impl(self, decided_atoms, merged_atom, config_base): check_type_str = BlendConfig().get_str(self.a, "connect-check-type", config_base) strength_diff_threshold = BlendConfig().get_str( self.a, "connect-strength-diff-limit", config_base) confidence_above_threshold = BlendConfig().get_str( self.a, "connect-confidence-above-limit", config_base) self.check_type = get_type(check_type_str) if check_type_str != get_type_name(self.check_type): self.last_status = blending_status.UNKNOWN_TYPE return self.strength_diff_limit = float(strength_diff_threshold) self.confidence_above_limit = float(confidence_above_threshold) self.__connect_random_conflict_links(decided_atoms, merged_atom)
def get_chooser(self, config_base): self.last_status = blending_status.IN_PROCESS chooser = self.choosers.get(BlendConfig().get_str( self.a, "atoms-chooser", config_base)) if chooser is not None: self.last_status = blending_status.SUCCESS return chooser(self.a) else: self.last_status = blending_status.PARAMETER_ERROR raise UserWarning
def get_decider(self, config_base): self.last_status = blending_status.IN_PROCESS decider = self.deciders.get(BlendConfig().get_str( self.a, "blending-decider", config_base)) if decider is not None: self.last_status = blending_status.SUCCESS return decider(self.a) else: self.last_status = blending_status.PARAMETER_ERROR raise UserWarning
def atom_choose_impl(self, focus_atoms, config_base): focus_atoms = self.a.get_atoms_by_type(types.Atom) \ if len(focus_atoms) is 0 \ else focus_atoms atom_type = BlendConfig().get_str(self.a, "choose-atom-type", config_base) least_count = BlendConfig().get_int(self.a, "choose-least-count", config_base) try: atom_type = types.__dict__[atom_type] except KeyError: atom_type = types.Node if least_count < 0: self.last_status = blending_status.NOT_ENOUGH_ATOMS return self.__get_atoms_all(focus_atoms, atom_type, least_count)
def find_inheritance_nodes(a, decided_atoms): """Find the inheritance nodes from decided atoms list. Args: a: An instance of AtomSpace. decided_atoms: The source atoms to make new atom. :param a: opencog.atomspace.AtomSpace :param decided_atoms: list[Atom] Returns: ret: Nodes inherited to decided_atoms. :rtype : list[Atom] """ free_var = a.add_node(types.VariableNode, "$") inheritance_link = a.add_link( types.InheritanceLink, [free_var, decided_atoms] ) """ GetLink InheritanceLink VariableNode $ ConceptNode <Decided Atom> """ get_link = a.add_link(types.GetLink, [inheritance_link]) """ SetLink ConceptNode <Related node 1> ConceptNode <Related node 2> ... ConceptNode <Related node N> """ inheritance_node_set = PyCogExecute().execute(a, get_link) ret = inheritance_node_set.out # Delete temp links. BlendConfig().exe_factory.clean_up(inheritance_node_set) BlendConfig().exe_factory.clean_up(get_link) BlendConfig().exe_factory.clean_up(inheritance_link) BlendConfig().exe_factory.clean_up(free_var) return ret
def get_maker(self, config_base): self.last_status = blending_status.IN_PROCESS maker = self.makers.get(BlendConfig().get_str(self.a, "new-blend-atom-maker", config_base)) if maker is not None: self.last_status = blending_status.SUCCESS return maker(self.a) else: self.last_status = blending_status.PARAMETER_ERROR raise UserWarning
def blending_decide_impl(self, chosen_atoms, config_base): """Implemented factory method to deciding atoms. Args: chosen_atoms: The atoms to decide. config_base: A Node to save custom config. :param chosen_atoms: list[Atom] :param config_base: Atom """ result_atoms_count = BlendConfig().get_int( self.a, "decide-result-atoms-count", config_base ) sti_min = BlendConfig().get_str(self.a, "decide-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "decide-sti-max", config_base) # Check if given range of STI value is valid or not. sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None self.__decide_atoms_best_sti( chosen_atoms, result_atoms_count, sti_min, sti_max )
def blending_decide_impl(self, chosen_atoms, config_base): """Implemented factory method to deciding atoms. Args: chosen_atoms: The atoms to decide. config_base: A Node to save custom config. :param chosen_atoms: list[Atom] :param config_base: Atom """ result_atoms_count = BlendConfig().get_int( self.a, "decide-result-atoms-count", config_base) self.__decide_atoms_random(chosen_atoms, result_atoms_count)
def blending_decide_impl(self, chosen_atoms, config_base): result_atoms_count = BlendConfig().get_int( self.a, "decide-result-atoms-count", config_base) sti_min = BlendConfig().get_str(self.a, "decide-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "decide-sti-max", config_base) sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None self.__decide_atoms_best_sti(chosen_atoms, result_atoms_count, sti_min, sti_max)
def atom_choose_impl(self, focus_atoms, config_base): """Implemented factory method to choosing atoms. Args: focus_atoms: The atoms to blend. config_base: A Node to save custom config. :param focus_atoms: list[Atom] :param config_base: Atom """ atom_type = BlendConfig().get_str( self.a, "choose-atom-type", config_base ) least_count = BlendConfig().get_int( self.a, "choose-least-count", config_base ) sti_min = BlendConfig().get_str(self.a, "choose-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "choose-sti-max", config_base) # Check if given atom_type is valid or not. try: atom_type = types.__dict__[atom_type] except KeyError: atom_type = types.Node # Check if given least_count is valid or not. if least_count < 0: self.last_status = blending_status.NOT_ENOUGH_ATOMS return # Check if given range of STI value is valid or not. sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None # Call the actual choosing algorithm method. self.__get_atoms_in_sti_range( focus_atoms, atom_type, least_count, sti_min, sti_max )
def test_config_inheritance(self): try: __import__("blending.util.blending_config") except ImportError: import unittest raise unittest.SkipTest( "ImportError exception: " + "Can't load Blend Config. " + "make sure the required dependencies are installed.") else: from blending.util.blending_config import BlendConfig self.a.add_link(types.InheritanceLink, [ self.a.add_node(types.ConceptNode, "my-config"), self.a.add_node(types.ConceptNode, "default-config") ]) self.a.add_link(types.ExecutionLink, [ self.a.add_node(types.SchemaNode, "BLEND:atoms-chooser"), self.a.add_node(types.ConceptNode, "my-config"), self.a.add_node(types.ConceptNode, "ChooseAll") ]) # Test config loads correctly. assert_equal( "ChooseNull", BlendConfig().get_str(self.a, "atoms-chooser", "default-config")) # Test override config loads correctly. assert_equal( "ChooseAll", BlendConfig().get_str(self.a, "atoms-chooser", "my-config")) # Test config loads parent's config correctly # if there not exists config. assert_equal( "DecideNull", BlendConfig().get_str(self.a, "blending-decider", "my-config"))
def test_config_update(self): try: __import__("blending.util.blending_config") except ImportError: import unittest raise unittest.SkipTest( "ImportError exception: " + "Can't load Blend Config. " + "make sure the required dependencies are installed.") else: from blending.util.blending_config import BlendConfig # Test config loads correctly. assert_equal( "ChooseNull", BlendConfig().get_str(self.a, "atoms-chooser", "default-config")) BlendConfig().update(self.a, "atoms-chooser", "ChooseAll", "default-config") # Test config updates correctly. assert_equal( "ChooseAll", BlendConfig().get_str(self.a, "atoms-chooser", "default-config"))
def make_default_config(self): """Initialize a default config for this class.""" super(self.__class__, self).make_default_config() BlendConfig().update(self.a, "connect-check-type", "SimilarityLink") BlendConfig().update(self.a, "connect-strength-diff-limit", "0.3") BlendConfig().update(self.a, "connect-confidence-above-limit", "0.7") BlendConfig().update(self.a, "connect-data-n-gram-limit", "None") BlendConfig().update(self.a, "connect-evaluate-n-gram-limit", "None") BlendConfig().update(self.a, "connect-inter-info-strength-above-limit", "0.5")
def atom_choose_impl(self, focus_atoms, config_base): atom_type = BlendConfig().get_str(self.a, "choose-atom-type", config_base) least_count = BlendConfig().get_int(self.a, "choose-least-count", config_base) sti_min = BlendConfig().get_str(self.a, "choose-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "choose-sti-max", config_base) try: atom_type = types.__dict__[atom_type] except KeyError: atom_type = types.Node if least_count < 0: self.last_status = blending_status.NOT_ENOUGH_ATOMS return sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None self.__get_atoms_in_sti_range(focus_atoms, atom_type, least_count, sti_min, sti_max)
def blending_decide_impl(self, chosen_atoms, config_base): """Implemented factory method to deciding atoms. Args: chosen_atoms: The atoms to decide. config_base: A Node to save custom config. :param chosen_atoms: list[Atom] :param config_base: Atom """ result_atoms_count = BlendConfig().get_int( self.a, "decide-result-atoms-count", config_base) sti_min = BlendConfig().get_str(self.a, "decide-sti-min", config_base) sti_max = BlendConfig().get_str(self.a, "decide-sti-max", config_base) # Check if given range of STI value is valid or not. sti_min = int(sti_min) if sti_min.isdigit() else 1 sti_max = int(sti_max) if sti_max.isdigit() else None self.__decide_atoms_best_sti(chosen_atoms, result_atoms_count, sti_min, sti_max)
def get_chooser(self, config_base): """Provider method for atoms chooser. Args: config_base: A Node to save custom config. :param config_base: Atom Returns: The instance of atoms chooser. :rtype : BaseChooser Raises: UserWarning: Can't find the atoms chooser with given name. """ self.last_status = blending_status.IN_PROCESS chooser = self.choosers.get(BlendConfig().get_str( self.a, "atoms-chooser", config_base)) if chooser is not None: self.last_status = blending_status.SUCCESS return chooser(self.a) else: self.last_status = blending_status.PARAMETER_ERROR raise UserWarning
def __prepare(self, focus_atoms, config_base): """Prepare a blending. Args: focus_atoms: The atoms to blend. config_base: A Node to save custom config. :param focus_atoms: list[Atom] :param config_base: Atom """ self.last_status = blending_status.IN_PROCESS if type(config_base) is list: self.last_status = blending_status.PARAMETER_ERROR raise UserWarning("Config can't be list type.") default_config_base = self.a.add_node(types.ConceptNode, BlendConfig().config_prefix_name) self.config_base = default_config_base \ if config_base is None \ else config_base self.focus_atoms = [] \ if focus_atoms is None \ else focus_atoms
def make_default_config(self): """Initialize a default config for this class.""" BlendConfig().update(self.a, "choose-atom-type", "Node") BlendConfig().update(self.a, "choose-least-count", "2")
def make_default_config(self): super(self.__class__, self).make_default_config() BlendConfig().update(self.a, "connect-check-type", "SimilarityLink") BlendConfig().update(self.a, "connect-strength-diff-limit", "0.3") BlendConfig().update(self.a, "connect-confidence-above-limit", "0.7")
def make_default_config(self): BlendConfig().update(self.a, "make-atom-prefix", "") BlendConfig().update(self.a, "make-atom-separator", "-") BlendConfig().update(self.a, "make-atom-postfix", "")
def make_default_config(self): BlendConfig().update(self.a, "decide-result-atoms-count", "2")
def make_default_config(self): """Initialize a default config for this class.""" super(self.__class__, self).make_default_config() BlendConfig().update(self.a, "choose-sti-min", "1") BlendConfig().update(self.a, "choose-sti-max", "None")
def make_default_config(self): BlendConfig().update(self.a, "atoms-chooser", "ChooseAll") BlendConfig().update(self.a, "blending-decider", "DecideBestSTI") BlendConfig().update(self.a, "new-blend-atom-maker", "MakeSimple") BlendConfig().update(self.a, "link-connector", "ConnectSimple")
def make_default_config(self): BlendConfig().update(self.a, "choose-atom-type", "Node") BlendConfig().update(self.a, "choose-least-count", "2")
def make_default_config(self): """Initialize a default config for this class.""" BlendConfig().update(self.a, "make-atom-prefix", "") BlendConfig().update(self.a, "make-atom-separator", "-") BlendConfig().update(self.a, "make-atom-postfix", "")