You've already forked ui-cviko1
cviko 4
This commit is contained in:
671
notebook/cviko4.ipynb
Normal file
671
notebook/cviko4.ipynb
Normal file
@@ -0,0 +1,671 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"my_string = \"bbaccbabba\""
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"bbaccbabba\n",
|
||||
"Rule1: babccbabba\n",
|
||||
"Rule3: babcbcabba\n",
|
||||
"Rule1: abbcbcabba\n",
|
||||
"Rule2: abbcbacbba\n",
|
||||
"Rule3: abbbcacbba\n",
|
||||
"Rule1: abbbcacbab\n",
|
||||
"Rule2: abbbaccbab\n",
|
||||
"Rule3: abbbacbcab\n",
|
||||
"Rule1: abbabcbcab\n",
|
||||
"Rule2: abbabcbacb\n",
|
||||
"Rule3: abbabbcacb\n",
|
||||
"Rule1: ababbbcacb\n",
|
||||
"Rule2: ababbbaccb\n",
|
||||
"Rule3: ababbbacbc\n",
|
||||
"Rule1: aabbbbacbc\n",
|
||||
"Rule3: aabbbbabcc\n",
|
||||
"Rule1: aabbbabbcc\n",
|
||||
"Rule1: aabbabbbcc\n",
|
||||
"Rule1: aababbbbcc\n",
|
||||
"Rule1: aaabbbbbcc\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(my_string)\n",
|
||||
"change = True\n",
|
||||
"while change:\n",
|
||||
" change = False\n",
|
||||
" #Rule1\n",
|
||||
" if my_string.find(\"ba\") != -1:\n",
|
||||
" my_string = my_string.replace(\"ba\", \"ab\", 1)\n",
|
||||
" print(\"Rule1: \", my_string)\n",
|
||||
" change = True\n",
|
||||
" if my_string.find(\"ca\") != -1:\n",
|
||||
" my_string = my_string.replace(\"ca\", \"ac\", 1)\n",
|
||||
" print(\"Rule2: \", my_string)\n",
|
||||
" change = True\n",
|
||||
" if my_string.find(\"cb\") != -1:\n",
|
||||
" my_string = my_string.replace(\"cb\", \"bc\", 1)\n",
|
||||
" print(\"Rule3: \", my_string)\n",
|
||||
" change = True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Collecting pytholog\n",
|
||||
" Downloading pytholog-2.4.1-py3-none-any.whl.metadata (12 kB)\n",
|
||||
"Collecting more-itertools (from pytholog)\n",
|
||||
" Downloading more_itertools-10.6.0-py3-none-any.whl.metadata (37 kB)\n",
|
||||
"Downloading pytholog-2.4.1-py3-none-any.whl (16 kB)\n",
|
||||
"Downloading more_itertools-10.6.0-py3-none-any.whl (63 kB)\n",
|
||||
"Installing collected packages: more-itertools, pytholog\n",
|
||||
"Successfully installed more-itertools-10.6.0 pytholog-2.4.1\n",
|
||||
"\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.2\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.0.1\u001b[0m\n",
|
||||
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
|
||||
"Note: you may need to restart the kernel to use updated packages.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"%pip install pytholog"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import pytholog as pl"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"new_base = pl.KnowledgeBase(\"persons\")\n",
|
||||
"new_base([\n",
|
||||
" \"man(john)\",\n",
|
||||
" \"man(jack)\",\n",
|
||||
" \"woman(alice)\",\n",
|
||||
" \"father(jack, john)\",\n",
|
||||
" \"father(jack, alice)\"\n",
|
||||
"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Yes']"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Je Jack muž?\n",
|
||||
"new_base.query(pl.Expr(\"man(jack)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Yes']"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# -||- John\n",
|
||||
"new_base.query(pl.Expr(\"man(john)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['No']"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# -||- Tom\n",
|
||||
"new_base.query(pl.Expr(\"man(tom)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Yes']"
|
||||
]
|
||||
},
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Je Jack otcom Alice?\n",
|
||||
"new_base.query(pl.Expr(\"father(jack, alice)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['No']"
|
||||
]
|
||||
},
|
||||
"execution_count": 20,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# -||- Anna\n",
|
||||
"new_base.query(pl.Expr(\"father(jack, anna)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'jack'}, {'X': 'john'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 22,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto je muž?\n",
|
||||
"new_base.query(pl.Expr(\"man(X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'Who': 'jack'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto je otcom Alice?\n",
|
||||
"new_base.query(pl.Expr(\"father(Who, alice)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'john'}, {'X': 'alice'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Deti Johna\n",
|
||||
"new_base.query(pl.Expr(\"father(jack, X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"new_base = pl.KnowledgeBase(\"persons\")\n",
|
||||
"new_base(\n",
|
||||
" [ \"father(juraj,jozef)\",\n",
|
||||
" \"father(jozef,jana)\",\n",
|
||||
" \"father(jozef,zuzana)\",\n",
|
||||
" \"father(michal,katka)\",\n",
|
||||
" \"mother(dana,jana)\",\n",
|
||||
" \"mother(dana,zuzana)\",\n",
|
||||
" \"mother(dana,katka)\",\n",
|
||||
" \"mother(jana,jano)\",\n",
|
||||
" \"man(juraj)\",\n",
|
||||
" \"man(jozef)\",\n",
|
||||
" \"man(michal)\",\n",
|
||||
" \"man(jano)\",\n",
|
||||
" \"man(miro)\",\n",
|
||||
" \"woman(dana)\",\n",
|
||||
" \"woman(jana)\",\n",
|
||||
" \"woman(zuzana)\",\n",
|
||||
" \"woman(katka)\"\n",
|
||||
" ]\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"['Yes']"
|
||||
]
|
||||
},
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Je Jano muž?\n",
|
||||
"new_base.query(pl.Expr(\"man(jano)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'jozef'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 32,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto je otcom Zuzany?\n",
|
||||
"new_base.query(pl.Expr(\"father(X, zuzana)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'jana'}, {'X': 'zuzana'}, {'X': 'katka'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Deti Dany\n",
|
||||
"new_base.query(pl.Expr(\"mother(dana, X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'dana'}, {'X': 'jana'}, {'X': 'katka'}, {'X': 'zuzana'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 34,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto sú ženy?\n",
|
||||
"new_base.query(pl.Expr(\"woman(X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 47,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"new_base = pl.KnowledgeBase(\"loves\")\n",
|
||||
"new_base([\"likes(peter,jana)\",\n",
|
||||
" \"likes(fero,maria)\",\n",
|
||||
" \"likes(jozo,jana)\",\n",
|
||||
" \"likes(peter,maria)\",\n",
|
||||
" \"likes(jozo,maria)\",\n",
|
||||
" \"likes(peter,pavla)\",\n",
|
||||
" \"likes(jozo,pavla)\",\n",
|
||||
" \"likes(peter,beer)\",\n",
|
||||
" \"likes(fero,beer)\",\n",
|
||||
" \"likes(peter,vine)\",\n",
|
||||
" \"likes(maria,vine)\",\n",
|
||||
" \"likes(pavla,beer)\",\n",
|
||||
" \"likes(jana,beer)\",\n",
|
||||
" \"likes(pavla,peter)\",\n",
|
||||
" \"likes(jana,fero)\",\n",
|
||||
" \"girl(jana)\",\n",
|
||||
" \"girl(pavla)\",\n",
|
||||
" \"girl(maria)\",\n",
|
||||
" \"boy(peter)\",\n",
|
||||
" \"boy(fero)\",\n",
|
||||
" \"boy(jozo)\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'Who': 'jozo'}, {'Who': 'peter'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 48,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Komu sa páči Pavla?\n",
|
||||
"new_base.query(pl.Expr(\"likes(Who, pavla)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'beer'}, {'X': 'peter'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto a čo sa páči Pavle?\n",
|
||||
"new_base.query(pl.Expr(\"likes(pavla, X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 50,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'jana'}, {'X': 'maria'}, {'X': 'pavla'}, {'X': 'beer'}, {'X': 'vine'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 50,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto a čo sa páči Petrovi?\n",
|
||||
"new_base.query(pl.Expr(\"likes(peter, X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'X': 'maria'}, {'X': 'beer'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto a čo sa páči Ferovi?\n",
|
||||
"new_base.query(pl.Expr(\"likes(fero, X)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Pridať pravidlo: Jožo má rád dievčatá, ktoré majú radi pivo.\n",
|
||||
"new_base([\"likes(jozo, X) :- girl(X), likes(X, beer)\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 53,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'D': 'jana'}, {'D': 'maria'}, {'D': 'pavla'}, {'D': 'jana'}, {'D': 'pavla'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 53,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Koho má rad Jožo?\n",
|
||||
"new_base.query(pl.Expr(\"likes(jozo, D)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 57,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"new_base.clear_cache()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 61,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Pridať pravidlo: Fero má rád divčatá, ktoré majú radi jeho.\n",
|
||||
"new_base([\"likes(fero, X) :- girl(X), likes(X, fero)\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 63,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'Dievca': 'maria'}, {'Dievca': 'beer'}, {'Dievca': 'jana'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 63,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto a čo sa páči Ferovi?\n",
|
||||
"new_base.query(pl.Expr(\"likes(fero, Dievca)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 64,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"family = pl.KnowledgeBase(\"family\")\n",
|
||||
"family([\n",
|
||||
" \"parents(susanne,charles,jane)\",\n",
|
||||
" \"parents(susanne,charles,diana)\",\n",
|
||||
" \"parents(susanne,charles,jack)\",\n",
|
||||
" \"parents(jane,tom,peter)\",\n",
|
||||
" \"parents(jane,tom,anne)\",\n",
|
||||
" \"parents(anne,luis,kate)\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 65,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'Who': 'jane'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 65,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto je matka Petra?\n",
|
||||
"family.query(pl.Expr(\"parents(Who, _, peter)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 66,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"[{'Who': 'tom'}]"
|
||||
]
|
||||
},
|
||||
"execution_count": 66,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# Kto je otec Petra?\n",
|
||||
"family.query(pl.Expr(\"parents(_, Who, peter)\"))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "ui-cviko1",
|
||||
"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.13.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Reference in New Issue
Block a user