Memory Representation in Dialogue Systems (Part 2)

This notebook is part 2 of the dynamic memory representation series. See part 1 to get started.

Process the Text

As with part 1, part 2 will perform the same evaluation as part 1, except with bAbI tasks QA2, Two Supporting Facts. In QA1, there were two types of entities: persons and rooms. In QA2, there is one additional entity type: items. Each dialogue provides a sequence of statements that indicate persons going to different rooms as before, and also items that persons may have acquired or released. The key insight is that objects move into rooms with the person that last acquired them, and stay in rooms once released. This requires the system to make the distinction between rooms and items, and also between acquiring and releasing actions.

The first step is to import resources/qa2_two-supporting-facts_train.txt into data. Text processing is exactly the same as before: tokenize and POS tag the sentences.


In [1]:
import pandas as pd
import numpy as np
import nltk
from sklearn.metrics import accuracy_score

In [2]:
# Read the bAbI data as CSV
filename = 'resources/qa3_three-supporting-facts_train.txt'
data = pd.read_csv(filename, delimiter='\t', names=['sentence', 'answer', 'factid'])
data = data.fillna('')

# Tag each sentence as a statement or question
tag_sentence = lambda row: 'S' if row.answer == '' else 'Q'
data['type'] = data.apply(tag_sentence, axis=1)

# Use NLTK to tokenize the sentences into arrays of words
tokenize = lambda row: nltk.word_tokenize(row.sentence)[1:]
data.sentence = data.apply(tokenize, axis=1)

In [3]:
# Create a DataFrame with just the statements
def statements():
    return data[data.type == 'S'] \
        .reset_index(drop=True) \
        .drop('answer', axis=1) \
        .drop('factid', axis=1) \
        .drop('type', axis=1)

# Create a DataFrame with just the questions
def questions():
    return data[data.type == 'Q'] \
        .reset_index(drop=True) \
        .drop('type', axis=1)

In [4]:
# Tag each token as a part of speech
pos_tag = lambda row: nltk.pos_tag(row.sentence)
data['tag'] = data.apply(pos_tag, axis=1)

In [5]:
def extract_statement(tags):
    '''Extracts a (subject, relation, object) triple from each statement based on the POS tags'''
    subject, relation, obj = '', '', ''
    for word,tag in tags:
        if tag == 'NNP':
            subject = word
        elif tag == 'VBD' or word == 'journeyed': # TODO: 'journeyed' is tagged improperly
            relation = word
        elif tag == 'NN':
            obj = word
    return (subject, relation, obj)

In [17]:
def extract_question(tags):
    '''Extracts the entity under discussion from each question based on the POS tags'''
    eud0, eud1 = '', ''
    i = 0
    for word,tag in tags:
        if i == 0 and (tag == 'NNP' or tag == 'NN'):
            eud0 = word
            i += 1
        elif i == 1 and (tag == 'NNP' or tag == 'NN'):
            eud1 = word
    return (eud0, eud1)

In [18]:
def extract(row):
    '''Extracts the appropriate data given a processed DataFrame row'''
    if row.type == 'S':
        return extract_statement(row.tag)
    else: 
        return extract_question(row.tag)

In [19]:
data['extracted'] = data.apply(extract, axis=1)

In [20]:
data


Out[20]:
sentence answer factid type tag extracted
0 [Mary, moved, to, the, bathroom, .] S [(Mary, NNP), (moved, VBD), (to, TO), (the, DT... (Mary, moved, bathroom)
1 [Sandra, journeyed, to, the, bedroom, .] S [(Sandra, NNP), (journeyed, VBD), (to, TO), (t... (Sandra, journeyed, bedroom)
2 [Mary, got, the, football, there, .] S [(Mary, NNP), (got, VBD), (the, DT), (football... (Mary, got, football)
3 [John, went, back, to, the, bedroom, .] S [(John, NNP), (went, VBD), (back, RB), (to, TO... (John, went, bedroom)
4 [Mary, journeyed, to, the, office, .] S [(Mary, NNP), (journeyed, VBD), (to, TO), (the... (Mary, journeyed, office)
5 [John, journeyed, to, the, office, .] S [(John, NNP), (journeyed, NN), (to, TO), (the,... (John, journeyed, office)
6 [John, took, the, milk, .] S [(John, NNP), (took, VBD), (the, DT), (milk, N... (John, took, milk)
7 [Daniel, went, back, to, the, kitchen, .] S [(Daniel, NNP), (went, VBD), (back, RB), (to, ... (Daniel, went, kitchen)
8 [John, moved, to, the, bedroom, .] S [(John, NNP), (moved, VBD), (to, TO), (the, DT... (John, moved, bedroom)
9 [Daniel, went, back, to, the, hallway, .] S [(Daniel, NNP), (went, VBD), (back, RB), (to, ... (Daniel, went, hallway)
10 [Daniel, took, the, apple, .] S [(Daniel, NNP), (took, VBD), (the, DT), (apple... (Daniel, took, apple)
11 [John, left, the, milk, there, .] S [(John, NNP), (left, VBD), (the, DT), (milk, N... (John, left, milk)
12 [John, travelled, to, the, kitchen, .] S [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, kitchen)
13 [Sandra, went, back, to, the, bathroom, .] S [(Sandra, NNP), (went, VBD), (back, RB), (to, ... (Sandra, went, bathroom)
14 [Daniel, journeyed, to, the, bathroom, .] S [(Daniel, NNP), (journeyed, VBD), (to, TO), (t... (Daniel, journeyed, bathroom)
15 [John, journeyed, to, the, bathroom, .] S [(John, NNP), (journeyed, NN), (to, TO), (the,... (John, journeyed, bathroom)
16 [Mary, journeyed, to, the, bathroom, .] S [(Mary, NNP), (journeyed, VBD), (to, TO), (the... (Mary, journeyed, bathroom)
17 [Sandra, went, back, to, the, garden, .] S [(Sandra, NNP), (went, VBD), (back, RB), (to, ... (Sandra, went, garden)
18 [Sandra, went, to, the, office, .] S [(Sandra, NNP), (went, VBD), (to, TO), (the, D... (Sandra, went, office)
19 [Daniel, went, to, the, garden, .] S [(Daniel, NNP), (went, VBD), (to, TO), (the, D... (Daniel, went, garden)
20 [Sandra, went, back, to, the, hallway, .] S [(Sandra, NNP), (went, VBD), (back, RB), (to, ... (Sandra, went, hallway)
21 [Daniel, journeyed, to, the, office, .] S [(Daniel, NNP), (journeyed, VBD), (to, TO), (t... (Daniel, journeyed, office)
22 [Mary, dropped, the, football, .] S [(Mary, NNP), (dropped, VBD), (the, DT), (foot... (Mary, dropped, football)
23 [John, moved, to, the, bedroom, .] S [(John, NNP), (moved, VBD), (to, TO), (the, DT... (John, moved, bedroom)
24 [Where, was, the, football, before, the, bathr... office 23 17 5 Q [(Where, WRB), (was, VBD), (the, DT), (footbal... (football, bathroom)
25 [John, travelled, to, the, office, .] S [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, office)
26 [John, travelled, to, the, bedroom, .] S [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, bedroom)
27 [Where, was, the, football, before, the, bathr... office 23 17 5 Q [(Where, WRB), (was, VBD), (the, DT), (footbal... (football, bathroom)
28 [Daniel, left, the, apple, .] S [(Daniel, NNP), (left, VBD), (the, DT), (apple... (Daniel, left, apple)
29 [Sandra, travelled, to, the, kitchen, .] S [(Sandra, NNP), (travelled, VBD), (to, TO), (t... (Sandra, travelled, kitchen)
... ... ... ... ... ... ...
15766 [John, put, down, the, football, .] S [(John, NNP), (put, VBD), (down, RP), (the, DT... (John, put, football)
15767 [Where, was, the, apple, before, the, garden, ?] hallway 36 30 26 Q [(Where, WRB), (was, VBD), (the, DT), (apple, ... (apple, garden)
15768 [John, put, down, the, milk, .] S [(John, NNP), (put, VBD), (down, RP), (the, DT... (John, put, milk)
15769 [John, went, to, the, bedroom, .] S [(John, NNP), (went, VBD), (to, TO), (the, DT)... (John, went, bedroom)
15770 [Sandra, journeyed, to, the, hallway, .] S [(Sandra, NNP), (journeyed, VBD), (to, TO), (t... (Sandra, journeyed, hallway)
15771 [Daniel, journeyed, to, the, garden, .] S [(Daniel, NNP), (journeyed, VBD), (to, TO), (t... (Daniel, journeyed, garden)
15772 [John, moved, to, the, garden, .] S [(John, NNP), (moved, VBD), (to, TO), (the, DT... (John, moved, garden)
15773 [Daniel, took, the, football, there, .] S [(Daniel, NNP), (took, VBD), (the, DT), (footb... (Daniel, took, football)
15774 [Mary, moved, to, the, office, .] S [(Mary, NNP), (moved, VBD), (to, TO), (the, DT... (Mary, moved, office)
15775 [Mary, moved, to, the, kitchen, .] S [(Mary, NNP), (moved, VBD), (to, TO), (the, DT... (Mary, moved, kitchen)
15776 [Daniel, took, the, apple, there, .] S [(Daniel, NNP), (took, VBD), (the, DT), (apple... (Daniel, took, apple)
15777 [Daniel, dropped, the, football, .] S [(Daniel, NNP), (dropped, VBD), (the, DT), (fo... (Daniel, dropped, football)
15778 [Mary, went, to, the, garden, .] S [(Mary, NNP), (went, VBD), (to, TO), (the, DT)... (Mary, went, garden)
15779 [Mary, picked, up, the, football, .] S [(Mary, NNP), (picked, VBD), (up, RP), (the, D... (Mary, picked, football)
15780 [John, travelled, to, the, bathroom, .] S [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, bathroom)
15781 [Mary, left, the, football, there, .] S [(Mary, NNP), (left, VBD), (the, DT), (footbal... (Mary, left, football)
15782 [Daniel, took, the, milk, .] S [(Daniel, NNP), (took, VBD), (the, DT), (milk,... (Daniel, took, milk)
15783 [Daniel, went, to, the, office, .] S [(Daniel, NNP), (went, VBD), (to, TO), (the, D... (Daniel, went, office)
15784 [Daniel, discarded, the, milk, .] S [(Daniel, NNP), (discarded, VBD), (the, DT), (... (Daniel, discarded, milk)
15785 [Daniel, travelled, to, the, kitchen, .] S [(Daniel, NNP), (travelled, VBD), (to, TO), (t... (Daniel, travelled, kitchen)
15786 [Mary, grabbed, the, football, there, .] S [(Mary, NNP), (grabbed, VBD), (the, DT), (foot... (Mary, grabbed, football)
15787 [Mary, travelled, to, the, kitchen, .] S [(Mary, NNP), (travelled, VBD), (to, TO), (the... (Mary, travelled, kitchen)
15788 [Mary, dropped, the, football, .] S [(Mary, NNP), (dropped, VBD), (the, DT), (foot... (Mary, dropped, football)
15789 [Daniel, got, the, football, .] S [(Daniel, NNP), (got, VBD), (the, DT), (footba... (Daniel, got, football)
15790 [Daniel, left, the, apple, .] S [(Daniel, NNP), (left, VBD), (the, DT), (apple... (Daniel, left, apple)
15791 [Daniel, travelled, to, the, bedroom, .] S [(Daniel, NNP), (travelled, VBD), (to, TO), (t... (Daniel, travelled, bedroom)
15792 [Where, was, the, apple, before, the, kitchen, ?] office 64 59 57 Q [(Where, WRB), (was, VBD), (the, DT), (apple, ... (apple, kitchen)
15793 [Sandra, travelled, to, the, bathroom, .] S [(Sandra, NNP), (travelled, VBD), (to, TO), (t... (Sandra, travelled, bathroom)
15794 [Mary, moved, to, the, office, .] S [(Mary, NNP), (moved, VBD), (to, TO), (the, DT... (Mary, moved, office)
15795 [Where, was, the, apple, before, the, kitchen, ?] office 64 59 57 Q [(Where, WRB), (was, VBD), (the, DT), (apple, ... (apple, kitchen)

15796 rows × 6 columns

Define the Graph


In [23]:
from neo4j.v1 import GraphDatabase, basic_auth

In [40]:
# Create a neo4j session
# NOTE: Make sure that URL/credentials are correct and that Neo4j is running
driver = GraphDatabase.driver('bolt://localhost:7687', auth=basic_auth('neo4j', 'neo4j'))

In [25]:
# WARNING: This will clear the database when run!
def reset_db():
    '''Remove all nodes and relationships from the database'''
    session = driver.session()
    session.run('MATCH (n) DETACH DELETE n')

In [26]:
def create(query, start=0, end=0):
    '''Create a graph based on each triple in the extracted statements'''
    session = driver.session()
    stat = statements()
    end = len(stat) if end <= start else end
    for subject,relation,obj in stat[start:end].extracted:
        session.run(query, subject=subject, relation=relation, obj=obj)

This is the point where QA2 starts to be different from QA1. The query generating the knowledge graph needs to be altered slightly to encode information about the ordering of events relative to objects as well as subjects.

In QA1, a linked list was constructed to keep track of events relative to a character; the NEXT edge type indicated the next event that the person acted upon. This was all that was necessary, since the questions asked directly about the most recent event that corresponded to a particular person.

In QA2, questions ask about the item a room is in, which requires a way to keep track of the last person who interacted with it. As such, it is not enough to know the order in which a person performed actions, but it is also necessary to know the order in which an item was handled. The most recent interaction indicates the person who interacted with that object last, and that can be used to find the room based on their visit history.

Thus, the v4 graph query will create three types of lists.

  1. The first list is the global list of events indicated by the NEXT edge type.
  2. The second list is a person's list of events indicated by the S_NEXT (next subject) edge type.
  3. The third list is an item's list of events indicated by the O_NEXT (next object) edge type. Each list has a HEAD edge that points to the most recent event relative to their respective lists.

In [27]:
v4_query = '''
    /// 1. Create Nodes
    MERGE (global:GLOBAL {name:'global'}) // Find/create the global entity
    MERGE (subject:SUBJECT {name:$subject}) // Find/create the subject and object
    MERGE (object:OBJECT {name:$obj})

    /// 2. Create a new relation between the subject and object
    CREATE (subject)-[:R_BEGIN]->(relation:RELATION {name:$relation})-[:R_END]->(object)

    /// 3. Create head pointers to the newly created relation
    CREATE (global)-[globalHead:HEAD]->(relation)
    CREATE (subject)-[subjectHead:HEAD]->(relation)
    CREATE (object)-[objectHead:HEAD]->(relation)

    WITH global,subject,relation,object,subjectHead,objectHead,globalHead

    /// 4. Link the existing global list with the new head node
    // Find the previous global head of the list (if none exist, this query will terminate here)
    MATCH (global)-[prevGlobalHead:HEAD]->(prevGlobalRelation:RELATION) WHERE prevGlobalRelation <> relation
    CREATE (prevGlobalRelation)-[:NEXT]->(relation) // Complete the link
    DELETE prevGlobalHead // Remove the previous head pointer

    WITH subject,relation,object,subjectHead,objectHead

    /// 5. Link the existing subject list with the new head node
    // Find the previous subject head of the list (if none exist, this query will terminate here)
    MATCH (subject)-[prevSubjectHead:HEAD]->(prevSubjectRelation:RELATION) WHERE prevSubjectRelation <> relation
    CREATE (prevSubjectRelation)-[:S_NEXT]->(relation) // Complete the link
    DELETE prevSubjectHead // Remove the previous head pointer

    WITH subject,relation,object,objectHead

    /// 6. Link the existing object list with the new head node
    // Find the previous subject head of the list (if none exist, this query will terminate here)
    MATCH (object)-[prevObjectHead:HEAD]->(prevObjectRelation:RELATION) WHERE prevObjectRelation <> relation
    CREATE (prevObjectRelation)-[:O_NEXT]->(relation) // Complete the link
    DELETE prevObjectHead // Remove the previous head pointer
'''

In [28]:
# Represent each relation as a node, ordered by multiple linked lists
def build_v4_graph(start=0, end=0):
    reset_db()
    
    session = driver.session()
    
    # Create an index for faster access
    session.run('CREATE INDEX ON :SUBJECT(name)')
    session.run('CREATE INDEX ON :RELATION(name)')
    session.run('CREATE INDEX ON :OBJECT(name)')
    
    create(v4_query, start=start, end=end)

In [29]:
all_actions = sorted(list(set(x[1] for x in data.extracted if x != '' and x[1] != '')))

In [30]:
movement_actions = ['journeyed', 'moved', 'travelled', 'went']
acquire_actions = ['got', 'grabbed', 'picked', 'took']
release_actions = ['discarded', 'dropped', 'left', 'put']

In [31]:
def find_last_person(obj):
    '''Finds the last person in contact with the object'''
    query = '''
        MATCH (:OBJECT {name:$name})-[:HEAD]->(relation:RELATION)<-[:R_BEGIN]-(subject:SUBJECT)
        RETURN relation.name AS relation, subject.name AS subject
    '''
    return session.run(query, name=obj)

In [80]:
def find_object_location(obj):
    query = '''
        // Find the last person in contact with the object
        MATCH (:OBJECT {name:$obj})-[:HEAD]->(relation:RELATION)<-[:R_BEGIN]-(subject:SUBJECT)

        // Acquire
        MATCH (subject)-[:HEAD]->(head_relation:RELATION)
        
        MATCH p=(head_relation)<-[next:S_NEXT *1..20]-(prevRelation:RELATION)
        WHERE prevRelation.name IN $movement
        WITH size(next) as dist, p, relation
        ORDER BY dist
        WITH filter(n IN nodes(p) WHERE n.name IN $movement)[1] AS shortest, relation
        MATCH (shortest)-[:R_END]->(object_acquire:OBJECT)
        
        WITH relation, object_acquire

        // Release
        MATCH p=(relation)<-[next:S_NEXT *1..20]-(prevRelation:RELATION)
        WHERE prevRelation.name IN $movement
        WITH size(next) as dist, p, object_acquire, relation
        ORDER BY dist
        WITH filter(n IN nodes(p) WHERE n.name IN $movement)[1] AS shortest, object_acquire, relation
        MATCH (shortest)-[:R_END]->(object_release:OBJECT)

        RETURN DISTINCT object_acquire.name AS acquire, object_release.name AS release, relation.name AS relation
    '''
    return session.run(query, obj=obj, movement=movement_actions)

In [82]:
statements()[:28]


Out[82]:
sentence tag extracted
0 [Mary, moved, to, the, bathroom, .] [(Mary, NNP), (moved, VBD), (to, TO), (the, DT... (Mary, moved, bathroom)
1 [Sandra, journeyed, to, the, bedroom, .] [(Sandra, NNP), (journeyed, VBD), (to, TO), (t... (Sandra, journeyed, bedroom)
2 [Mary, got, the, football, there, .] [(Mary, NNP), (got, VBD), (the, DT), (football... (Mary, got, football)
3 [John, went, back, to, the, bedroom, .] [(John, NNP), (went, VBD), (back, RB), (to, TO... (John, went, bedroom)
4 [Mary, journeyed, to, the, office, .] [(Mary, NNP), (journeyed, VBD), (to, TO), (the... (Mary, journeyed, office)
5 [John, journeyed, to, the, office, .] [(John, NNP), (journeyed, NN), (to, TO), (the,... (John, journeyed, office)
6 [John, took, the, milk, .] [(John, NNP), (took, VBD), (the, DT), (milk, N... (John, took, milk)
7 [Daniel, went, back, to, the, kitchen, .] [(Daniel, NNP), (went, VBD), (back, RB), (to, ... (Daniel, went, kitchen)
8 [John, moved, to, the, bedroom, .] [(John, NNP), (moved, VBD), (to, TO), (the, DT... (John, moved, bedroom)
9 [Daniel, went, back, to, the, hallway, .] [(Daniel, NNP), (went, VBD), (back, RB), (to, ... (Daniel, went, hallway)
10 [Daniel, took, the, apple, .] [(Daniel, NNP), (took, VBD), (the, DT), (apple... (Daniel, took, apple)
11 [John, left, the, milk, there, .] [(John, NNP), (left, VBD), (the, DT), (milk, N... (John, left, milk)
12 [John, travelled, to, the, kitchen, .] [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, kitchen)
13 [Sandra, went, back, to, the, bathroom, .] [(Sandra, NNP), (went, VBD), (back, RB), (to, ... (Sandra, went, bathroom)
14 [Daniel, journeyed, to, the, bathroom, .] [(Daniel, NNP), (journeyed, VBD), (to, TO), (t... (Daniel, journeyed, bathroom)
15 [John, journeyed, to, the, bathroom, .] [(John, NNP), (journeyed, NN), (to, TO), (the,... (John, journeyed, bathroom)
16 [Mary, journeyed, to, the, bathroom, .] [(Mary, NNP), (journeyed, VBD), (to, TO), (the... (Mary, journeyed, bathroom)
17 [Sandra, went, back, to, the, garden, .] [(Sandra, NNP), (went, VBD), (back, RB), (to, ... (Sandra, went, garden)
18 [Sandra, went, to, the, office, .] [(Sandra, NNP), (went, VBD), (to, TO), (the, D... (Sandra, went, office)
19 [Daniel, went, to, the, garden, .] [(Daniel, NNP), (went, VBD), (to, TO), (the, D... (Daniel, went, garden)
20 [Sandra, went, back, to, the, hallway, .] [(Sandra, NNP), (went, VBD), (back, RB), (to, ... (Sandra, went, hallway)
21 [Daniel, journeyed, to, the, office, .] [(Daniel, NNP), (journeyed, VBD), (to, TO), (t... (Daniel, journeyed, office)
22 [Mary, dropped, the, football, .] [(Mary, NNP), (dropped, VBD), (the, DT), (foot... (Mary, dropped, football)
23 [John, moved, to, the, bedroom, .] [(John, NNP), (moved, VBD), (to, TO), (the, DT... (John, moved, bedroom)
24 [John, travelled, to, the, office, .] [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, office)
25 [John, travelled, to, the, bedroom, .] [(John, NNP), (travelled, VBD), (to, TO), (the... (John, travelled, bedroom)
26 [Daniel, left, the, apple, .] [(Daniel, NNP), (left, VBD), (the, DT), (apple... (Daniel, left, apple)
27 [Sandra, travelled, to, the, kitchen, .] [(Sandra, NNP), (travelled, VBD), (to, TO), (t... (Sandra, travelled, kitchen)

In [83]:
build_v4_graph(start=0, end=28)

In [84]:
session = driver.session()
find_object_location('apple').single()


Out[84]:
<Record acquire='garden' release='garden' relation='left'>

Build the Graph


In [43]:
build_v4_graph()

<img src="screenshots/qa2-multiple-list.png",width=1000>

Calcualte an Accuracy Score


In [69]:
def get_answers(row):
    '''Given an input row merge the statement in the graph, 
    or query the graph if it is a question'''
    if row.type == 'S':
        subject,relation,obj = row.extracted
        session.run(v4_query, subject=subject, relation=relation, obj=obj)
        return ''
    elif row.type == 'Q':
        obj = row.extracted[0]
        # WARNING: do not consume the result (e.g., call .consume() or .single()) 
        # until the entire iteration is done.
        # Failure to do so may cause the queries to be VERY slow!
        return find_object_location(obj)

In [70]:
def traverse(result):
    if result['relation'] in acquire_actions:
        return result['acquire']
    else:
        return result['release']

In [71]:
reset_db()

In [72]:
session = driver.session()
results = data.apply(get_answers, axis=1)
results = [x for x in results if x != '']
predicted = [traverse(result.single()) for result in results]

In [73]:
predicted[:5]


Out[73]:
['office', 'office', 'garden', 'garden', 'hallway']

In [74]:
actual = list(questions().answer)

In [75]:
actual[:5]


Out[75]:
['office', 'office', 'bathroom', 'bathroom', 'hallway']

In [76]:
accuracy_score(actual, predicted)


Out[76]:
0.80000000000000004

In [78]:
def find_incorrect(actual, predicted):
    z = list(zip(actual, predicted))
    return [(i, x[0], x[1]) for i,x in enumerate(z) if x[0] != x[1]]

In [79]:
find_incorrect(actual, predicted)


Out[79]:
[(2, 'bathroom', 'garden'),
 (3, 'bathroom', 'garden'),
 (16, 'garden', 'kitchen'),
 (18, 'bathroom', 'garden'),
 (23, 'kitchen', 'bathroom'),
 (24, 'kitchen', 'bathroom'),
 (28, 'hallway', 'office'),
 (36, 'bedroom', 'kitchen'),
 (37, 'bedroom', 'kitchen'),
 (39, 'office', 'hallway'),
 (47, 'garden', 'hallway'),
 (49, 'garden', 'hallway'),
 (57, 'hallway', 'bathroom'),
 (59, 'kitchen', 'bathroom'),
 (61, 'office', 'hallway'),
 (84, 'office', 'bedroom'),
 (91, 'office', 'bathroom'),
 (92, 'garden', 'bathroom'),
 (93, 'office', 'bathroom'),
 (98, 'garden', 'bedroom'),
 (101, 'bathroom', 'bedroom'),
 (111, 'office', 'kitchen'),
 (113, 'bedroom', 'kitchen'),
 (115, 'bedroom', 'garden'),
 (125, 'bathroom', 'hallway'),
 (126, 'bathroom', 'hallway'),
 (127, 'bathroom', 'hallway'),
 (134, 'hallway', 'bathroom'),
 (136, 'hallway', 'bedroom'),
 (155, 'hallway', 'bedroom'),
 (156, 'garden', 'bedroom'),
 (158, 'garden', 'bathroom'),
 (160, 'bathroom', 'garden'),
 (161, 'hallway', 'garden'),
 (169, 'bathroom', 'kitchen'),
 (181, 'hallway', 'office'),
 (182, 'hallway', 'office'),
 (195, 'hallway', 'bathroom'),
 (196, 'hallway', 'bathroom'),
 (198, 'garden', 'office'),
 (199, 'office', 'garden'),
 (206, 'bedroom', 'bathroom'),
 (207, 'bedroom', 'bathroom'),
 (208, 'bedroom', 'bathroom'),
 (209, 'bedroom', 'garden'),
 (214, 'bathroom', 'hallway'),
 (233, 'hallway', 'garden'),
 (237, 'hallway', 'garden'),
 (238, 'hallway', 'garden'),
 (241, 'garden', 'hallway'),
 (243, 'garden', 'hallway'),
 (244, 'bathroom', 'bedroom'),
 (257, 'garden', 'bedroom'),
 (258, 'garden', 'bedroom'),
 (263, 'garden', 'kitchen'),
 (267, 'bathroom', 'hallway'),
 (268, 'bathroom', 'hallway'),
 (269, 'bathroom', 'hallway'),
 (272, 'hallway', 'bedroom'),
 (273, 'office', 'bedroom'),
 (283, 'office', 'bedroom'),
 (293, 'garden', 'hallway'),
 (294, 'garden', 'hallway'),
 (295, 'garden', 'bedroom'),
 (296, 'garden', 'bedroom'),
 (299, 'bathroom', 'garden'),
 (307, 'bathroom', 'kitchen'),
 (308, 'bathroom', 'kitchen'),
 (309, 'hallway', 'kitchen'),
 (313, 'bathroom', 'garden'),
 (317, 'bathroom', 'garden'),
 (318, 'bathroom', 'garden'),
 (324, 'hallway', 'bathroom'),
 (326, 'bedroom', 'office'),
 (327, 'bedroom', 'office'),
 (331, 'office', 'garden'),
 (332, 'hallway', 'garden'),
 (337, 'office', 'hallway'),
 (339, 'bedroom', 'garden'),
 (340, 'bathroom', 'office'),
 (341, 'bathroom', 'office'),
 (366, 'bathroom', 'hallway'),
 (367, 'bathroom', 'hallway'),
 (378, 'garden', 'office'),
 (388, 'hallway', 'garden'),
 (395, 'garden', 'bathroom'),
 (413, 'hallway', 'bathroom'),
 (414, 'hallway', 'bathroom'),
 (416, 'bedroom', 'garden'),
 (427, 'bathroom', 'hallway'),
 (439, 'bathroom', 'kitchen'),
 (443, 'office', 'bathroom'),
 (457, 'kitchen', 'office'),
 (460, 'garden', 'hallway'),
 (474, 'garden', 'bedroom'),
 (479, 'hallway', 'kitchen'),
 (480, 'bedroom', 'garden'),
 (481, 'bedroom', 'garden'),
 (482, 'bedroom', 'bathroom'),
 (483, 'bathroom', 'office'),
 (489, 'bedroom', 'garden'),
 (500, 'bathroom', 'bedroom'),
 (501, 'bathroom', 'bedroom'),
 (511, 'kitchen', 'office'),
 (518, 'kitchen', 'bathroom'),
 (520, 'office', 'kitchen'),
 (531, 'bathroom', 'bedroom'),
 (532, 'bathroom', 'bedroom'),
 (533, 'office', 'bedroom'),
 (534, 'kitchen', 'bathroom'),
 (537, 'garden', 'hallway'),
 (538, 'garden', 'hallway'),
 (540, 'garden', 'bathroom'),
 (541, 'garden', 'bathroom'),
 (546, 'bathroom', 'hallway'),
 (547, 'bathroom', 'hallway'),
 (548, 'bathroom', 'hallway'),
 (550, 'bathroom', 'bedroom'),
 (552, 'hallway', 'kitchen'),
 (562, 'hallway', 'bathroom'),
 (564, 'kitchen', 'bedroom'),
 (579, 'garden', 'bathroom'),
 (583, 'hallway', 'garden'),
 (595, 'office', 'hallway'),
 (609, 'kitchen', 'bedroom'),
 (612, 'office', 'hallway'),
 (621, 'garden', 'kitchen'),
 (632, 'garden', 'hallway'),
 (642, 'garden', 'office'),
 (646, 'bedroom', 'kitchen'),
 (668, 'hallway', 'bathroom'),
 (669, 'hallway', 'bathroom'),
 (671, 'bedroom', 'garden'),
 (676, 'kitchen', 'bedroom'),
 (683, 'kitchen', 'bedroom'),
 (724, 'hallway', 'bedroom'),
 (732, 'bedroom', 'hallway'),
 (738, 'garden', 'bedroom'),
 (745, 'garden', 'kitchen'),
 (746, 'garden', 'kitchen'),
 (747, 'bathroom', 'bedroom'),
 (754, 'hallway', 'garden'),
 (761, 'kitchen', 'bedroom'),
 (764, 'garden', 'kitchen'),
 (765, 'kitchen', 'garden'),
 (766, 'kitchen', 'garden'),
 (777, 'office', 'garden'),
 (783, 'office', 'bathroom'),
 (784, 'office', 'bathroom'),
 (786, 'office', 'garden'),
 (787, 'office', 'garden'),
 (788, 'office', 'garden'),
 (797, 'garden', 'bathroom'),
 (802, 'bedroom', 'hallway'),
 (803, 'kitchen', 'bathroom'),
 (804, 'garden', 'bathroom'),
 (811, 'bathroom', 'hallway'),
 (812, 'bathroom', 'hallway'),
 (815, 'hallway', 'kitchen'),
 (829, 'bathroom', 'bedroom'),
 (835, 'garden', 'kitchen'),
 (843, 'kitchen', 'bathroom'),
 (847, 'kitchen', 'office'),
 (849, 'bathroom', 'office'),
 (856, 'office', 'bathroom'),
 (857, 'bathroom', 'hallway'),
 (863, 'bedroom', 'bathroom'),
 (864, 'office', 'bathroom'),
 (866, 'kitchen', 'bathroom'),
 (867, 'garden', 'bathroom'),
 (870, 'bathroom', 'garden'),
 (872, 'bathroom', 'garden'),
 (877, 'hallway', 'office'),
 (878, 'garden', 'bathroom'),
 (882, 'bedroom', 'office'),
 (894, 'office', 'hallway'),
 (901, 'bedroom', 'kitchen'),
 (902, 'bedroom', 'kitchen'),
 (905, 'bathroom', 'bedroom'),
 (907, 'kitchen', 'hallway'),
 (908, 'garden', 'hallway'),
 (912, 'bedroom', 'garden'),
 (913, 'hallway', 'garden'),
 (914, 'kitchen', 'garden'),
 (931, 'hallway', 'bedroom'),
 (944, 'bathroom', 'bedroom'),
 (946, 'hallway', 'kitchen'),
 (954, 'kitchen', 'garden'),
 (955, 'bathroom', 'office'),
 (957, 'office', 'garden'),
 (962, 'bathroom', 'garden'),
 (967, 'kitchen', 'office'),
 (968, 'bathroom', 'bedroom'),
 (974, 'bathroom', 'garden'),
 (980, 'kitchen', 'office'),
 (985, 'hallway', 'office'),
 (988, 'hallway', 'garden'),
 (991, 'bathroom', 'hallway'),
 (992, 'bathroom', 'hallway'),
 (996, 'garden', 'hallway')]

In [ ]: