forked from neomatrix369/awesome-ai-ml-dl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grakn example: adding the Graql-to-English.ipynb notebook to the graq…
…l folder
- Loading branch information
1 parent
ab24877
commit c360e8d
Showing
1 changed file
with
171 additions
and
0 deletions.
There are no files selected for viewing
171 changes: 171 additions & 0 deletions
171
examples/data/databases/graph/grakn/graql/Graql-to-English.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# supress warnings\n", | ||
"import warnings\n", | ||
"warnings.filterwarnings('ignore')\n", | ||
"\n", | ||
"from fuzzywuzzy import fuzz\n", | ||
"import pandas as pd\n", | ||
"import sys\n", | ||
"from pytictoc import TicToc" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## A Graql query\n", | ||
"\n", | ||
"```\n", | ||
"match\n", | ||
" $customer isa person, has age < 20;\n", | ||
" $company isa company, has name \"Telecom\";\n", | ||
" (customer: $customer, provider: $company) isa contract;\n", | ||
" (caller: $customer, callee: $anyone) isa call, has duration $duration;\n", | ||
"get $duration; \n", | ||
"mean $duration;\n", | ||
"```" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"simple_graql_query = \"\"\"\n", | ||
"match\n", | ||
" $customer isa person, has age < 20;\n", | ||
" $company isa company, has name \"Telecom\";\n", | ||
" (customer: $customer, provider: $company) isa contract;\n", | ||
" (caller: $customer, callee: $anyone) isa call, has duration $duration;\n", | ||
"get $duration; \n", | ||
"mean $duration;\n", | ||
"\"\"\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 96, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"simple_graql_query = simple_graql_query.strip()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 97, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"simple_graql_query_as_lines = simple_graql_query.splitlines()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 98, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"['match',\n", | ||
" ' $customer isa person, has age < 20;',\n", | ||
" ' $company isa company, has name \"Telecom\";',\n", | ||
" ' (customer: $customer, provider: $company) isa contract;',\n", | ||
" ' (caller: $customer, callee: $anyone) isa call, has duration $duration;',\n", | ||
" 'get $duration; ',\n", | ||
" 'mean $duration;']" | ||
] | ||
}, | ||
"execution_count": 98, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"simple_graql_query_as_lines" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 99, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"english_from_graql_query = []\n", | ||
"for each_line in simple_graql_query_as_lines:\n", | ||
" if each_line.strip() == \"match\":\n", | ||
" english_from_graql_query.append(\"find records that match all of the below criteria\")\n", | ||
" else:\n", | ||
" english_from_graql_query.append(\n", | ||
" each_line\n", | ||
" .replace(\"$\", \"\")\n", | ||
" .replace(\";\", \"\")\n", | ||
" .replace(\"<\", \"less than\")\n", | ||
" .replace(\"has age less than 20\", \"under the age of 20\")\n", | ||
" .replace(\"has age less than\", \"less than\") \n", | ||
" .replace(\"isa\", \"is an entity of type\")\n", | ||
" .replace(\"has name\", \"by the name\")\n", | ||
" .replace(\"by the name\", \"called\")\n", | ||
" .replace('(customer: customer, provider: company) is an entity of type contract', 'a contract binds a customer with the company Telecom')\n", | ||
" .replace('(caller: customer, callee: anyone) is an entity of type call, has duration duration', 'a person calls another person for a duration of time')\n", | ||
" .replace(\"get duration\", \"get the duration of all the calls made\")\n", | ||
" .replace(\"mean duration\", \"find the average of the duration of all the calls made\") \n", | ||
" )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 100, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"find records that match all of the below criteria\n", | ||
" customer is an entity of type person, under the age of 20\n", | ||
" company is an entity of type company, called \"Telecom\"\n", | ||
" a contract binds a customer with the company Telecom\n", | ||
" a person calls another person for a duration of time\n", | ||
"get the duration of all the calls made \n", | ||
"find the average of the duration of all the calls made\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"for each_line in english_from_graql_query:\n", | ||
" print(each_line)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.7.2" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |