Last active
February 21, 2020 02:59
-
-
Save mocurin/1beac9a63e59098f9b7b105856933b84 to your computer and use it in GitHub Desktop.
Алгорифмы Маркова
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Алгорифмы Маркова" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Простецкая штука чтобы не считать все самому" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from typing import Tuple, List, Set\n", | |
"\n", | |
"\n", | |
"Alphabet = Set[str]\n", | |
"Rule = Tuple[str, str]\n", | |
"\n", | |
"\n", | |
"class Algorithm:\n", | |
" def __init__(self,\n", | |
" R: List[Rule],\n", | |
" S='.'):\n", | |
" self._rules = R\n", | |
" self._stop = S\n", | |
" \n", | |
" def __call__(self,\n", | |
" word: str,\n", | |
" verbose=False):\n", | |
" while True:\n", | |
" backup = word\n", | |
" for a, b in self._rules:\n", | |
" new_word = word.replace(a, b, 1)\n", | |
" if new_word != word:\n", | |
" word = new_word\n", | |
" if verbose:\n", | |
" self._log(word, (a, b))\n", | |
" break\n", | |
" if backup == word:\n", | |
" break\n", | |
" if self._stop in word:\n", | |
" word = word.replace(self._stop, '', 1)\n", | |
" break\n", | |
" return word\n", | |
" \n", | |
" def _log(self,\n", | |
" word: str,\n", | |
" rule: Rule):\n", | |
" a, b = rule\n", | |
" print(\"Правило:\", a, '->', b, sep=' ')\n", | |
" print(\"Слово:\", word, sep=' ')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Демонстрация:" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Правило: aa -> *\n", | |
"Слово: abb*bbaaaba\n", | |
"Правило: b* -> *\n", | |
"Слово: ab*bbaaaba\n", | |
"Правило: b* -> *\n", | |
"Слово: a*bbaaaba\n", | |
"Правило: *b -> *\n", | |
"Слово: a*baaaba\n", | |
"Правило: *b -> *\n", | |
"Слово: a*aaaba\n", | |
"Правило: a* -> *\n", | |
"Слово: *aaaba\n", | |
"Правило: *a -> *\n", | |
"Слово: *aaba\n", | |
"Правило: *a -> *\n", | |
"Слово: *aba\n", | |
"Правило: *a -> *\n", | |
"Слово: *ba\n", | |
"Правило: *b -> *\n", | |
"Слово: *a\n", | |
"Правило: *a -> *\n", | |
"Слово: *\n", | |
"Правило: * -> .\n", | |
"Слово: .\n", | |
"\n" | |
] | |
} | |
], | |
"source": [ | |
"rules = [('b*', '*'),\n", | |
" ('*b', '*'),\n", | |
" ('a*', '*'),\n", | |
" ('*a', '*'),\n", | |
" ('aa', '*'),\n", | |
" ('*', '.')]\n", | |
"\n", | |
"a = Algorithm(rules)\n", | |
"print(a('abbaabbaaaba', verbose=True))" | |
] | |
} | |
], | |
"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.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment