Update database.py
Browse files- database.py +13 -4
database.py
CHANGED
@@ -38,7 +38,13 @@ class Database(BaseDatabase):
|
|
38 |
}
|
39 |
|
40 |
def query(self, domain: str, state: dict, topk: int, ignore_open=False, soft_contraints=(), fuzzy_match_ratio=60) -> list:
|
41 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
# query the db
|
43 |
if domain == 'taxi':
|
44 |
return [{'taxi_colors': random.choice(self.dbs[domain]['taxi_colors']),
|
@@ -106,6 +112,9 @@ if __name__ == '__main__':
|
|
106 |
db = Database()
|
107 |
assert issubclass(Database, BaseDatabase)
|
108 |
assert isinstance(db, BaseDatabase)
|
109 |
-
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
38 |
}
|
39 |
|
40 |
def query(self, domain: str, state: dict, topk: int, ignore_open=False, soft_contraints=(), fuzzy_match_ratio=60) -> list:
|
41 |
+
"""
|
42 |
+
return a list of topk entities (dict containing slot-value pairs) for a given domain based on the dialogue state.
|
43 |
+
:param state: support two formats: 1) [[slot,value], [slot,value]...]; 2) {domain: {slot: value, slot: value...}} (the same as belief state)
|
44 |
+
"""
|
45 |
+
if isinstance(state, dict):
|
46 |
+
assert domain in state, print(f"domain {domain} not in state {state}")
|
47 |
+
state = state[domain].items()
|
48 |
# query the db
|
49 |
if domain == 'taxi':
|
50 |
return [{'taxi_colors': random.choice(self.dbs[domain]['taxi_colors']),
|
|
|
112 |
db = Database()
|
113 |
assert issubclass(Database, BaseDatabase)
|
114 |
assert isinstance(db, BaseDatabase)
|
115 |
+
# both calls will be ok
|
116 |
+
res1 = db.query("restaurant", [['price range', 'expensive']], topk=3)
|
117 |
+
res2 = db.query("restaurant", {'restaurant':{'price range': 'expensive'}}, topk=3)
|
118 |
+
assert res1 == res2
|
119 |
+
print(res1, len(res1))
|
120 |
+
# print(db.query("hotel", [['price range', 'moderate'], ['stars','4'], ['type', 'guesthouse'], ['internet', 'yes'], ['parking', 'no'], ['area', 'east']]))
|