Skip to content

Commit

Permalink
Handle queries returning a numeric value
Browse files Browse the repository at this point in the history
  • Loading branch information
neomatrix369 committed Feb 6, 2020
1 parent 769ccf8 commit c4efe2d
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 21 deletions.
59 changes: 46 additions & 13 deletions examples/data/databases/graph/grakn/graql/english-graql-queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
],
'CALL_DURATION_COMPARISON': ['How does the average call duration among customers aged under 20 compare those aged over 40?'
],
'CALL_DURATION_UNDER_20': ['How does the average call duration among customers aged under 20?'
],
'CALL_DURATION_OVER_40': ['How does the average call duration among customers aged over 40?'
],
}

alternative_queries_in_english = {
Expand Down Expand Up @@ -60,8 +64,12 @@
'Customers who have called each other atleast once',
'Get phone number of people received calls from customer of certain age'
],
'CALL_DURATION_COMPARISON': ['How average call duration among customers compared between ages'
'CALL_DURATION_COMPARISON': ['How average call duration among customers compared between ages?'
, 'how long did the call last'],
'CALL_DURATION_UNDER_20': ['How much time do customers under 20 spend time on a call on an averge?'
],
'CALL_DURATION_OVER_40': ['How much time do customers over 40 spend time on a call on an averge?'
],
}

graql_queries = {
Expand All @@ -77,7 +85,7 @@
$min-date == 2018-09-10T00:00:00; $started-at > $min-date;
get $phone-number;
""",
'These are numbers of the customers who called +86 921 547 9004 since 2018-09-10T00:00:00'
'These are numbers of the customers who called +86 921 547 9004 since 2018-09-10T00:00:00'
],
'UNDER_20_PHONE_CALLS_LONDON': ["""
match
Expand All @@ -92,7 +100,7 @@
$target-call-date > $pattern-call-date;
get $phone-number;
""",
'Here are the phone numbers of the people (London calls)'
'Here are the phone numbers of the people (London calls)'
],
'OVER_50_PHONE_CALLS_CAMBRIDGE': ["""
match
Expand All @@ -107,7 +115,7 @@
$target-call-date > $pattern-call-date;
get $phone-number;
""",
'Here are the phone numbers of the people (Cambridge calls)'
'Here are the phone numbers of the people (Cambridge calls)'
],
'COMMON_CUSTOMERS_MULTIPLE_NUMBERS': ["""
match
Expand All @@ -118,7 +126,7 @@
(caller: $customer-b, callee: $common-contact) isa call;
get $phone-number;
""",
'Here are the numbers of the common customers'],
'Here are the numbers of the common customers'],
'COMMON_CUSTOMERS_SINGLE_NUMBER': ["""
match
$target isa person, has phone-number "+48 894 777 5173";
Expand All @@ -132,25 +140,50 @@
(caller: $customer-a, callee: $customer-b) isa call;
get $phone-number-a, $phone-number-b;
""",
'The numbers of the customers who have called the single number are'],
'The numbers of the customers who have called the single number are'
],
'CALL_DURATION_COMPARISON': [["""
match
$customer isa person, has age < 20;
$company isa company, has name "Telecom";
(customer: $customer, provider: $company) isa contract;
(caller: $customer, callee: $anyone) isa call, has duration $duration;
get $duration; mean $duration;
get $duration;
mean $duration;
""",
'The average call duration between customers have been'
],
["""
'The average call duration between customers have been (in seconds)'
],
["""
match
$customer isa person, has age > 40;
$company isa company, has name "Telecom";
(customer: $customer, provider: $company) isa contract;
(caller: $customer, callee: $anyone) isa call, has duration $duration;
get $duration; mean $duration;
get $duration;
mean $duration;
""",
'The average call duration between customers have been'
'The average call duration between customers have been (in seconds)'
]],
}
'CALL_DURATION_UNDER_20': ["""
match
$customer isa person, has age < 20;
$company isa company, has name "Telecom";
(customer: $customer, provider: $company) isa contract;
(caller: $customer, callee: $anyone) isa call, has duration $duration;
get $duration;
mean $duration;
""",
'The average call duration between customers have been (in seconds)'
],
'CALL_DURATION_OVER_40': ["""
match
$customer isa person, has age > 40;
$company isa company, has name "Telecom";
(customer: $customer, provider: $company) isa contract;
(caller: $customer, callee: $anyone) isa call, has duration $duration;
get $duration;
mean $duration;
""",
'The average call duration between customers have been (in seconds)'
],
}
20 changes: 12 additions & 8 deletions examples/data/databases/graph/grakn/graql/grakn_console_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ def create_grakn_connection():

def print_to_log(title, content):
show_divider()
print(f"{GRAQL_BOT}",title)
print(content)
print(f"{GRAQL_BOT}", title, content)
show_divider()

def execute_user_query(query_code, query_response, transaction):
Expand All @@ -100,12 +99,17 @@ def execute_user_query(query_code, query_response, transaction):
print("")
print(f"{GRAQL_BOT} Let me think, will take a moment, please be patient (talking to Highlander Grakn Server)...")
iterator = transaction.query(graql_query)
answers = iterator.collect_concepts()
if hasattr(answers[0], 'value'):
result = [answer.value() for answer in answers]
else:
print(f"{GRAQL_BOT} 😲 Schema found, 😩 we don't have the expertise to build it at the moment, your best bet it to use Graql Console or Workbase")
return
if type(iterator).__name__ == 'ResponseIterator':
result = list(iterator)
result = result[0].number()
else:
answers = iterator.collect_concepts()
if hasattr(answers[0], 'value'):
result = [answer.value() for answer in answers]
else:
print(f"{GRAQL_BOT} 😲 Schema found, 😩 we don't have the expertise to build it at the moment, your best bet it to use Graql Console or Workbase")
return

results_cache.update({query_code: []})
results_cache[query_code] = result

Expand Down

0 comments on commit c4efe2d

Please sign in to comment.