Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
emoUS-public
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
general
dsml
emoUS-public
Commits
9f54db83
Commit
9f54db83
authored
2 years ago
by
zqwerty
Browse files
Options
Downloads
Patches
Plain Diff
add database.py for camrest
parent
1be8d5dc
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
data/unified_datasets/camrest/database.py
+67
-0
67 additions, 0 deletions
data/unified_datasets/camrest/database.py
with
67 additions
and
0 deletions
data/unified_datasets/camrest/database.py
0 → 100644
+
67
−
0
View file @
9f54db83
import
json
import
os
import
random
from
fuzzywuzzy
import
fuzz
from
itertools
import
chain
from
zipfile
import
ZipFile
from
copy
import
deepcopy
from
convlab.util.unified_datasets_util
import
BaseDatabase
,
download_unified_datasets
class
Database
(
BaseDatabase
):
def
__init__
(
self
):
"""
extract data.zip and load the database.
"""
data_path
=
download_unified_datasets
(
'
camrest
'
,
'
data.zip
'
,
os
.
path
.
dirname
(
os
.
path
.
abspath
(
__file__
)))
archive
=
ZipFile
(
data_path
)
self
.
dbs
=
{}
with
archive
.
open
(
'
data/CamRestDB.json
'
)
as
f
:
self
.
dbs
[
'
restaurant
'
]
=
json
.
loads
(
f
.
read
())
self
.
slot2dbattr
=
{
'
price range
'
:
'
pricerange
'
,
}
def
query
(
self
,
domain
:
str
,
state
:
dict
,
topk
:
int
,
ignore_open
=
False
,
soft_contraints
=
(),
fuzzy_match_ratio
=
60
)
->
list
:
"""
return a list of topk entities (dict containing slot-value pairs) for a given domain based on the dialogue state.
"""
# query the db
assert
domain
==
'
restaurant
'
state
=
list
(
map
(
lambda
ele
:
(
self
.
slot2dbattr
.
get
(
ele
[
0
],
ele
[
0
]),
ele
[
1
])
if
not
(
ele
[
0
]
==
'
area
'
and
ele
[
1
]
==
'
center
'
)
else
(
'
area
'
,
'
centre
'
),
state
))
found
=
[]
for
i
,
record
in
enumerate
(
self
.
dbs
[
domain
]):
constraints_iterator
=
zip
(
state
,
[
False
]
*
len
(
state
))
soft_contraints_iterator
=
zip
(
soft_contraints
,
[
True
]
*
len
(
soft_contraints
))
for
(
key
,
val
),
fuzzy_match
in
chain
(
constraints_iterator
,
soft_contraints_iterator
):
if
val
in
[
""
,
"
dont care
"
,
'
not mentioned
'
,
"
don
'
t care
"
,
"
dontcare
"
,
"
do n
'
t care
"
]:
pass
else
:
try
:
if
key
not
in
record
:
continue
if
record
[
key
].
strip
()
==
'
?
'
:
# '?' matches any constraint
continue
else
:
if
not
fuzzy_match
:
if
val
.
strip
().
lower
()
!=
record
[
key
].
strip
().
lower
():
break
else
:
if
fuzz
.
partial_ratio
(
val
.
strip
().
lower
(),
record
[
key
].
strip
().
lower
())
<
fuzzy_match_ratio
:
break
except
:
continue
else
:
res
=
deepcopy
(
record
)
res
[
'
Ref
'
]
=
'
{0:08d}
'
.
format
(
i
)
found
.
append
(
res
)
if
len
(
found
)
==
topk
:
return
found
return
found
if
__name__
==
'
__main__
'
:
db
=
Database
()
assert
issubclass
(
Database
,
BaseDatabase
)
assert
isinstance
(
db
,
BaseDatabase
)
res
=
db
.
query
(
"
restaurant
"
,
[[
'
price range
'
,
'
expensive
'
]],
topk
=
3
)
print
(
res
,
len
(
res
))
# print(db.query("hotel", [['price range', 'moderate'], ['stars','4'], ['type', 'guesthouse'], ['internet', 'yes'], ['parking', 'no'], ['area', 'east']]))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment