Add & update files

This commit is contained in:
Ryszard Staruch 2024-11-13 13:27:23 +01:00
parent 590282122c
commit d5c64b2a13
3 changed files with 347 additions and 3 deletions

View File

@ -19,7 +19,7 @@
"source": [ "source": [
"### Zadanie 1 \n", "### Zadanie 1 \n",
"\n", "\n",
"Zaimplementuj algorytm Portera dla języka angielskiego (dokładne kroki są opisane tutaj https://vijinimallawaarachchi.com/2017/05/09/porter-stemming-algorithm/). Funkcja musi przyjmować słowo i zwracać jego rdzeń (stem). Przetestuj program na podanych przkładowych słowach oraz na minimum własnoręcznie wybranych słowach. Funkcja musi uwzględniać wielkość liter w podanym słowie.\n", "Zaimplementuj algorytm Portera dla języka angielskiego (dokładne kroki są opisane tutaj https://vijinimallawaarachchi.com/2017/05/09/porter-stemming-algorithm/). Funkcja musi przyjmować słowo i zwracać jego rdzeń (stem). Przetestuj program na podanych przykładowych słowach oraz na minimum 3 własnoręcznie wybranych słowach. Funkcja musi uwzględniać wielkość liter w podanym słowie.\n",
"\n", "\n",
"W celu efektywnego rozwiązania zadania wykonaj następujące kroki:\n", "W celu efektywnego rozwiązania zadania wykonaj następujące kroki:\n",
"1. Zdefiniuj pojedyncze zadania znajdujące się w algorytmie Portera.\n", "1. Zdefiniuj pojedyncze zadania znajdujące się w algorytmie Portera.\n",
@ -60,7 +60,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [

View File

@ -204,7 +204,239 @@
"execution_count": null, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [] "source": [
"# jeżeli ten if określa 3 warianty na tym samym poziomie, to nie stosujemy zagnieżdżenia warunków\n",
"if [ \"$positive_count\" -gt \"$negative_count\" ]; then\n",
" echo \"wydzwiek pozytywny\"\n",
"else\n",
" if [ \"$negative_count\" -gt \"$positive_count\" ]; then\n",
" echo \"wydzwiek: negatywny\"\n",
" else\n",
" echo \"wydzwiek: neutralny\"\n",
" fi\n",
"fi\n",
"\n",
"\n",
"# ten else nigdy się nie wywoła - nie powinno go być\n",
"if [ $positive_count -gt $negative_count ]\n",
" then echo \"Positive\"\n",
"elif [ $positive_count -lt $negative_count ]\n",
" then echo \"Negative\"\n",
"elif [ $positive_count -eq $negative_count ]\n",
" then echo \"Neutral\"\n",
"else\n",
" echo \"Error\" # to nie istnieje\n",
"fi\n",
"\n",
"\n",
"# positive - taki błąd mocno rzuca się w oczy (mimo że program działa)\n",
"POZITIVE=\"positive-words.txt\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Notebook 05"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# algorytm wzięty z pseudokodu z prezentacji profesora Jassema\n",
"def maxmatch_text_split(text, vocabulary):\n",
" if text == \"\":\n",
" return []\n",
" for i in range(len(text)-1, -1, -1):\n",
" firstword = text[0:i+1] # nie piszemy [0:x] tylko [:x]\n",
" reminder = text[i+1:]\n",
" if firstword in vocabulary:\n",
" return [firstword] + maxmatch_text_split(reminder, vocabulary)\n",
" firstword = text[0]\n",
" reminder = text[1]\n",
" return [firstword] + maxmatch_text_split(reminder, vocabulary)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def create_bpe_tokenizer(text, max_vocab_length):\n",
" nfoiwanfoiwa\n",
" \n",
" for x in range(10):\n",
" nfwoiaf\n",
" \n",
" awfnoia\n",
" if noiawniofa:\n",
" iognioe\n",
" else:\n",
" nawoinoigagna\n",
" fniaw..\n",
"\n",
" return 0\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import string\n",
"from collections import Counter\n",
"import re\n",
"\n",
"\n",
"def create_bpe_tokenizer(text, max_vocab_length):\n",
" text = (\"\".join(x for x in text if x not in string.punctuation)).lower()\n",
" vocabulary = list(set([x for x in text]))\n",
" while len(vocabulary)<max_vocab_length:\n",
" text = re.findall(\"|\".join(vocabulary), \"\".join(text))\n",
" list_bigrams = []\n",
" for i in range(0, len(text)-1):\n",
" list_bigrams.append(\"\".join(text[i:i+2]))\n",
" bi_freq = Counter(list_bigrams)\n",
" if all(i == 1 for i in bi_freq.values()):\n",
" break\n",
" sorted_bigram_list = sorted(bi_freq.items(), key = lambda x: list_bigrams.index(x[0]))\n",
" sorted_bigram_dict={}\n",
" for key, value in sorted_bigram_list:\n",
" sorted_bigram_dict[key] = value\n",
" vocabulary.append(max(sorted_bigram_dict, key=sorted_bigram_dict.get))\n",
" vocabulary = sorted(vocabulary, key = len, reverse=True)\n",
" vocabulary = sorted(vocabulary, key = len, reverse=True)\n",
" text = re.findall(\"|\".join(vocabulary), \"\".join(text))\n",
" # print( len(vocabulary), sorted(vocabulary, key = len))\n",
" return vocabulary\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Próba \"uratowania\" powyższego kodu"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def all_frequencies_are_ones(bigram_freqs):\n",
" return all(i == 1 for i in bigram_freqs.values())\n",
"\n",
"\n",
"def create_bpe_tokenizer2(text, max_vocab_length):\n",
" text = (\"\".join(x for x in text if x not in string.punctuation)).lower()\n",
" vocabulary = list(set(text))\n",
"\n",
" while len(vocabulary) < max_vocab_length:\n",
" text = re.findall(\"|\".join(vocabulary), \"\".join(text))\n",
" bigrams = []\n",
"\n",
" for i in range(0, len(text)-1):\n",
" bigrams.append(\"\".join(text[i:i+2]))\n",
"\n",
" bigram_freq = Counter(bigrams)\n",
" if all_frequencies_are_ones(bigram_freq):\n",
" break\n",
"\n",
" most_common_bigram = bigram_freq.most_common(1)[0][0]\n",
" vocabulary.append(most_common_bigram)\n",
" vocabulary = sorted(vocabulary, key = len, reverse=True)\n",
" \n",
" vocabulary = sorted(vocabulary, key = len, reverse=True)\n",
"\n",
" return vocabulary"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Warto zapoznać się z obiektami z paczki collections"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from collections import defaultdict\n",
"\n",
"pairs = {}\n",
"for sequence in vocab:\n",
" symbols = sequence.split()\n",
" for i in range(len(symbols) - 1):\n",
" pair = (symbols[i], symbols[i + 1])\n",
" if pair in pairs:\n",
" pairs[pair] += vocab[sequence]\n",
" else:\n",
" pairs[pair] = vocab[sequence]\n",
"\n",
"# to samo co\n",
"pairs = defaultdict(int)\n",
"for sequence in vocab:\n",
" symbols = sequence.split()\n",
" for i in range(len(symbols) - 1):\n",
" pair = (symbols[i], symbols[i + 1])\n",
" pairs[pair] += vocab[sequence]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Nie uzywamy dlugich slow na iteratory"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def maxmatch_text_split(text, vocabulary):\n",
" words_list = []\n",
" iterator = 0\n",
" \n",
" while iterator < len(text):\n",
" \n",
" for backwards_iterator in range(len(text), iterator, -1):\n",
" # if text[iterator : backwards_iterator] in vocabulary: \n",
" if text[iterator : backwards_iterator].lower() in vocabulary: #.lower() because every token is lower case in vocab\n",
" words_list.append(text[iterator : backwards_iterator]) #.lower() if want to have exact same tokens as in vocab\n",
" break\n",
" elif backwards_iterator == iterator + 1:\n",
" words_list.append(text[iterator : backwards_iterator])\n",
" \n",
" iterator += len(words_list[-1])\n",
" return words_list"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Niedopuszczalne są takie nazwy zmiennych (z błędami!)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dictinary_of_pairs_occurance = {}"
]
} }
], ],
"metadata": { "metadata": {

112
wyklad_debugowanie.ipynb Normal file
View File

@ -0,0 +1,112 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Debugowanie Najważniejsze Informacje\n",
"Debugowanie to proces identyfikowania i usuwania błędów (bugów) w kodzie. Poniżej kluczowe pojęcia i techniki, które pomagają skutecznie debugować.\n",
"\n",
"#### Narzędzia do kontrolowania wykonywania kodu podczas debugowania:\n",
"\n",
"**Breakpoint** punkt w kodzie, w którym debugger zatrzymuje wykonywanie programu.\n",
"\n",
"**Step Over** wykonuje bieżącą linię i przechodzi do następnej.\n",
"\n",
"**Step Into** wchodzi do wywoływanej funkcji, umożliwiając jej analizę.\n",
"\n",
"**Step Out** kończy analizę funkcji i wraca do poziomu wywołującego.\n",
"\n",
"#### Wskazówki\n",
"\n",
"* Podczas debugowania warto skorzystać z **Debug Console**, pozwalającej na wywołanie kodu w Pythonie na podstawie obecnego stanu programu.\n",
"* Debugujmy zawsze na **przykładowych**, **prostych** danych! (zanim przejdziemy do docelowej, na ogół obszerniejszej wersji danych)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"\n",
"def perform_random_operation(a, b):\n",
" choice = random.choice([1, 2, 3])\n",
"\n",
" if choice == 1:\n",
" output = a + b\n",
" elif choice == 2:\n",
" output = a * b\n",
" else:\n",
" output = a / b\n",
" \n",
" return output"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"def perform_random_operation_elementwise(list_a, list_b):\n",
" res = []\n",
"\n",
" for a, b in zip(list_a, list_b):\n",
" value = perform_random_operation(a, b)\n",
" res.append(value)\n",
"\n",
" return res"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 1.4, 7.5, 4.6, 45, 130, 500, 0, 400]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"first = [1, 5, 2, 5, 3, 10, 20, 0, 4]\n",
"second = [1, 7, 15, 23, 15, 13, 25, 24, 100]\n",
"\n",
"# first = [0, 0, 0]\n",
"# second = [0, 1, 2]\n",
"\n",
"perform_random_operation_elementwise(second, first)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "base",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}