Skip to content

Commit

Permalink
grakn example: adding the Graql-to-English.ipynb notebook to the graq…
Browse files Browse the repository at this point in the history
…l folder
  • Loading branch information
neomatrix369 committed Apr 13, 2020
1 parent ab24877 commit c360e8d
Showing 1 changed file with 171 additions and 0 deletions.
171 changes: 171 additions & 0 deletions examples/data/databases/graph/grakn/graql/Graql-to-English.ipynb
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
}

0 comments on commit c360e8d

Please sign in to comment.