pydialogue.state package#
Submodules#
pydialogue.state.kn_checkers module#
This module contains the functions that check whether the information exists in the KnowledgeBase.
- pydialogue.state.kn_checkers.basic_checker(kb_state, sent)[source]#
Checks whether a sentence with the same meaning is present in the database.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base that contains the set of truthful sentences.
sent (Sentence) – This sentence is checked against the database.
- Returns:
is_true – True is returned if the sentence is present False is returned if the sentence with the opposite meaning is present. None is returned if the sentence is not present.
- Return type:
bool
- pydialogue.state.kn_checkers.elem_exists_alt_checker(kb_state, ent, elem, pneg)[source]#
Checks whether the entity’s property key or attribute exists and is seen by the agent in the context.
- Parameters:
kb_state (KnowledgeBase) – The kb_state.world is used for getting the most recent version of the entity (in case a copy of the entity is provided)
ent (Entity) – The entity whose element existence is checked.
elem (str or tuple) – The property key or the attribute
pneg (str or None) – pneg indicates whether to check for equality or inequality
- Returns:
result – True if the element exists and pneg is None, or the element does not exist and pneg is not None. Otherwise, False is returned. If the element’s existence is not seen by the agent in the context, None is returned.
- Return type:
bool
- pydialogue.state.kn_checkers.elem_exists_checker(kb_state, sent)[source]#
Checks whether the sentence is in the form described in kn_parsers.elem_exists_parse and if so, extracts the arguments and calls the
check_elem_exists_alt()function. Thecheck_elem_exists_alt()checks whether the entity’s element (property or attribute) exists and whether the existence is visible to the agents.- Parameters:
kb_state (KnowledgeBase) – The knowledge base is used for testing the truthfulness of the sentence.
sent (Sentence) – The sentence to check for truthfulness.
- Returns:
elem_exists – Returns True if the sentence is truthful. Otherwise, False. If the information that the sentence conveys is not previously seen by the agent, None is returned regardless of the truthfulness.
- Return type:
bool or None
- pydialogue.state.kn_checkers.have_checker(kb_state, sent)[source]#
Checks whether an object has items on/under/in it and whether the agent has seen this information.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base is used to check the validity of the sentence.
sent (Sentence) – The sentence has to be in the form described in kn_parsers.have_parse.
- Returns:
is_true – True if the sentence is truthful. Otherwise, False. If the information that the sentence conveys is not seen by the agent, None is returned, regardless of the truthfulness of the sentence.
- Return type:
bool or None
- pydialogue.state.kn_checkers.property_alt_checker(kb_state, ent, pkey, pval, pneg)[source]#
Checks whether the entity’s (property_key, property_value) is the correct one and whether the property value is seen by the agent.
Additionally, this function can be used to check whether the entity’s attribute exists and whether is seen by the agent.
- Parameters:
kb_state (KnowledgeBase) – The kb_state.world is used for getting the most recent version of the entity (in case there are copies of the entity)
ent (Entity) – The entity which property is checked.
pkey (str or tuple or None) – The property key. If pkey is None, then the existence of an attribute is checked.
pval (any) – The property value or the attribute.
pneg (str or None) – pneg indicates whether to check for equality or inequality
- Returns:
result – Returns True if the entity’s pkey is pval and pneg is None or entity’s pkey is not pval and pneg is not None. Otherwise, False is returned. If the information is not visible to the agent in the context, None is returned regardless of the truthfulness.
- Return type:
bool or None
- pydialogue.state.kn_checkers.property_checker(kb_state, sent)[source]#
Checks whether the sentence is in one of the following forms:
<entity> ‘s <property_key> is <property_val>
<entity> is <attribute>
and if so, extracts the arguments and calls the
property_check_alt()function. Theproperty_check_alt()checks whether the sentence is truthful and whether the information it conveys is visible to the agents.- Parameters:
kb_state (KnowledgeBase) – The knowledge base that is used for testing the truthfulness of the sentence
sent (Sentence) – The sentence that is checked.
- Returns:
has_prop – Returns True if the sentence is truthful. Otherwise, False. If the information is not visible to the agent, None is returned regardless of the truthfulness of the sentence.
- Return type:
bool or None
pydialogue.state.kn_helpers module#
This module contains the functions that help implement the functions in the other modules in this folder.
- pydialogue.state.kn_helpers.add_attr_seen(kb_state, ent, attr, neg)[source]#
It adds the attr to the
ent.attr_seenorent.attr_seen_negdepending on whether the agent observes one of the following information:The <entity> is <attr>.
The <entity> is not <attr>.
- pydialogue.state.kn_helpers.add_prop_seen(kb_state, ent, pkey, pval)[source]#
If one of the agents observes that the entity’s property key equals the property value, the (pkey, pval) pair is added to the entity.prop_seen field.
- pydialogue.state.kn_helpers.add_prop_seen_neg(kb_state, ent, pkey, pval)[source]#
If one of the agents observes that the entity’s property does not equal the property value, it gets added to the prop_seen_neg field. For example, if Andy’s nickname is no longer ‘cuddle bunny’, it will be written in this field.
- pydialogue.state.kn_helpers.check_prop(sent)[source]#
Checks whether the sentence is in one of the following formats:
<entity> ‘s <property_key> is (not) <property_val>
<entity> is (not) <attribute>
If it fits the format, it extracts the variables in the brackets. Otherwise, None is returned. The <entity> has to be an instance of env_main.BaseEntity.
- pydialogue.state.kn_helpers.check_unique_entity(objects, entity, elements=None, only_seen=False)[source]#
Check whether the <entity> differs from the list of objects based on a list of elements. An element can be a property like size or material or an attribute like ‘locked’ (‘west’, ‘obstacle’), etc. only_seen indicates whether to use the element values that are observed by the agent.
- pydialogue.state.kn_helpers.create_oppos_sent(sent)[source]#
Creates an opposite sentence by adding an AM-NEG argument with the negation “not.” in the describer. The “not” element is not added in the sent.parts because it might be hard to pinpoint the location where it should be added.
- pydialogue.state.kn_helpers.find_similar_objs(objects, entity, elem, only_seen=False)[source]#
Finds objects that are similar to the <entity> based on a single element. An element can be a property or an attribute. only_seen indicates whether to look for objects whose element values are observed by the agent.
- pydialogue.state.kn_helpers.remove_attr_seen(kb_state, ent, attr, neg)[source]#
It removes the attr from the
ent.attr_seenorent.attr_seen_neg. This can happen if the agent observes an updated version of the entity’s attribute. For example, if the door is no longer open and the agent observes this, the ‘open’ attribute is removed from the attr:ent.attr_seen <pydialogue.environment.entities.Entity.attr_seen>.
- pydialogue.state.kn_helpers.remove_prop_seen(kb_state, ent, pkey)[source]#
If one of the agents observes that the entity’s property key no longer equals its current property value, the property key gets removed from the prop_seen field. For example, if the color of the chair is no longer white and the agent observes this, it gets removed.
- pydialogue.state.kn_helpers.remove_prop_seen_neg(kb_state, ent, pkey, pval)[source]#
If one of the agents observes that the entity’s property key is equal to the property value, the value gets removed from the prop_seen_neg field. For example, let’s say that the agent observes the change of Otto’s nickname to ‘tuxedo’. If ‘tuxedo’ exists in the prop_seen_neg[‘nickname’] field from previous observations, it gets removed.
pydialogue.state.kn_parsers module#
This module contains functions that extract meaningful information from the sentences.
- pydialogue.state.kn_parsers.elem_exists_parse(sent)[source]#
Extracts the entity, element (property or attribute), and a negation (if it exists) if the sentence matches one of the following formats:
<entity> has (not) <property>
<entity> has (not) <attribute>
<entity> has (not) direction <direction>
The (neg) is optional and refers to the negation ‘not’.
- pydialogue.state.kn_parsers.have_parse(sent)[source]#
Extracts the constituent arguments of one of the following sentences:
<entity> has (neg) <entity_1>, … , <entity_n> (location)
<entity> has not items (location)
For example, Andy has the small ball and the big ball. The (neg) is optional and refers to the negation ‘not’.
pydialogue.state.kn_updaters module#
This module contains the functions that update the KnowledgeBase.
- pydialogue.state.kn_updaters.basic_updater(kb_state, sent)[source]#
Add the sentence to the list of factual sentences (in the sent_db) and remove the opposite sentence if it exists. The sentence has to come from a trusted source.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base to be potentially updated.
sent (Sentence) – The sentence that is considered to be added.
- Return type:
None.
- pydialogue.state.kn_updaters.drop_updater(kb_state, sent)[source]#
Marks the location of the dropped item as observed. The location of the player dropping the item is marked as well. The sentence should be in the following form:
<player> drops <entity> <at_location>
- pydialogue.state.kn_updaters.elem_exists_alt_updater(kb_state, ent, elem, pneg)[source]#
Updates one of the following dictionaries:
This happens when the agent observes that the entity has/hasn’t a property or an attribute (element). The information is checked for its truthfulness before the update is made.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base is used to fetch the world.
ent (Entity) – The entity to be updated.
elem (str) – The property or attribute that the entity has/hasn’t. An example can be ‘color’ (property) or ‘locked’ (attribute)
pneg (str) – If pneg is not None, then the entity.elem_not_exists is updated
- Return type:
None.
- pydialogue.state.kn_updaters.elem_exists_updater(kb_state, sent)[source]#
Checks whether the sentence is in the form described in kn_parsers.elem_exists_parse and if so, extracts the arguments and calls the update_elem_exists_alt function.
- pydialogue.state.kn_updaters.get_updater(kb_state, sent)[source]#
Marks the location of the taken item as observed. The location of the player getting the item is marked as well. The sentence should be in the following form:
<player> gets <entity> (from_location)
For example, Andy gets the spoon from the table.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base.
sent (Sentence) – The sentence in the form above.
- Return type:
None.
- pydialogue.state.kn_updaters.go_updater(kb_state, sent)[source]#
Marks the player’s location as observed if the following sentence appears:
<player> goes <direction> <start_point>
Also, it marks the <start_point>’s direction as observed. For the sentence: The bear goes south from the barn to the main path, the last seen location of the bear is updated (bear.prop_seen[‘location’]=main_path), and also it updates that the barn has direction south to the main path (barn.prop_seen[‘south’]=main_path)
- Parameters:
kb_state (KnowledgeBase) – The knowledge base.
sent (Sentence) – The sentence in the form above.
- Return type:
None.
- pydialogue.state.kn_updaters.have_updater(kb_state, sent)[source]#
Marks the location of the entities that belong to a possessor as observed.
The sentence is in the form described in kn_parsers.have_parse. It also marks the location of the entities as exclusive, meaning they do not belong to any other entity in the world. If the possessor does not have entities, the location of all objects in the world is marked that it is not in the possessor’s possession.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base to be updated.
sent (Sentence) – The sentence form is described in kn_parsers.have_parse
- Return type:
None.
- pydialogue.state.kn_updaters.look_updater(kb_state, sent)[source]#
Marks the location of the player and the item that it looks as observed. The sentence should be in the following form:
<player> looks (preposition) <at_entity>
- pydialogue.state.kn_updaters.permit_updater(kb_state, sent)[source]#
Adds one of the following sentences in the database:
Getting players is not permitted.
Dropping the item in, on, or under itself is not permitted.
where element_key is ‘color’ or ‘size’.
- pydialogue.state.kn_updaters.property_update_alt(kb_state, ent, pkey, pval, pneg)[source]#
Updates one of the following dictionaries:
where each of the entities is taken from kb_state.world in case the entity is a copy. entity.prop_seen and entity.attr_seen are updated if the property or the attribute is observed by one of the agents. The _neg version is updated if it’s seen that the entity’s pkey is not pval or the entity does not contain the attribute.
- Parameters:
kb_state (KnowledgeBase) – The knowledge base is used to fetch the world.
ent (Entity) – The entity to be updated.
pkey (str or None) – The property key if a property is updated. For an attribute, this value is None.
pval (any) – The property value if a property is updated. Otherwise, it represents an attribute.
pneg (str or None) – If the pneg is present, the _neg version of the dictionaries is updated.
- Return type:
None.
- pydialogue.state.kn_updaters.property_updater(kb_state, sent)[source]#
Extract the entity, property key, property value, and the negation from the following sentence:
<entity> ‘s’ <property_key> is <property_value>
or
<entity> is <attribute>
and calls the
property_update_alt()function. In case the updater can extract additional information from the sentence, it updates the knowledge base with further information, too. For example, if a door is locked, it means that the door is not open as well.- Parameters:
kb_state (KnowledgeBase) – The knowledge base to be updated.
sent (Sentence) – The sentence in the aforementioned forms.
- Return type:
None.
pydialogue.state.knowledge_base module#
This module contains the class that stores factual information efficiently.
- class pydialogue.state.knowledge_base.KnowledgeBase(meta_context, world, undo_changes=None, last_context_id=None, sent_db=None, updaters=None, checkers=None)[source]#
Bases:
objectThe knowledge base is used to efficiently store information and quickly fetch information that is observed by the agent. The meta context can have a million sentences (some of them which are repeating as well), so querying information from the meta context can be costly. For example, if you want to check whether the color of a small toy is blue from the context, you’ll have to find that information first. Later, you have to check for sentences that do not indicate any change in color. On the contrary, the knowledge base only passes the meta context only once and stores the information in the sent_db, and the prop_seen, prop_seen_neg, attr_seen, attr_seen_neg fields of the Entity. This is done with the help of the updaters. Later, the checkers are used to check whether an agent has observed the queried information.
Write when the world and undo changes are used.
- undo_changes#
A list of changes made by the updaters to the sent_db and the fields mentioned above.
- Type:
list
- last_context_id#
The last point in the meta context is used by the updaters to update the knowledge set.
- Type:
int
- sent_db#
A list of factual sentences. This field is used by the kn_updaters.basic_updater.
- Type:
set
- updaters#
A list of functions that update the knowledge base.
- Type:
list
- checkers#
A list of functions that are used to query the validity of a sentence. A sentence is valid if it’s factual and if it is observed by the agent.
- Type:
list
- check(sent, update_context=True)[source]#
Checks the validity of a single sentence against the knowledge base. A sentence is valid if it’s factual, and it is observed by the agent. If update_context is true, it will update the knowledge base first with the unseen sentences.
- multi_check(sents)[source]#
Checks the sentences against the knowledge base for their validity. A sentence is valid if it’s factual, and it is observed by the agent. If all of them are valid, return True. If at least one of them is not valid, return False. Otherwise, return None (unknown validity)