aitech-eks-pub/cw/03a_tfidf_tasks_ODPOWIEDZI....

70 lines
1.6 KiB
Plaintext
Raw Normal View History

2021-03-09 13:46:07 +01:00
{
"cells": [
2021-03-24 12:38:00 +01:00
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def word_to_index(word):\n",
" vec = np.zeros(len(vocabulary))\n",
" if word in vocabulary:\n",
" idx = vocabulary.index(word)\n",
" vec[idx] = 1\n",
" else:\n",
" vec[-1] = 1\n",
" return vec"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def tf(document):\n",
" document_vector = None\n",
" for word in document:\n",
" if document_vector is None:\n",
" document_vector = word_to_index(word)\n",
" else:\n",
" document_vector += word_to_index(word)\n",
" return document_vector"
]
},
2021-03-09 13:46:07 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
2021-03-24 12:38:00 +01:00
"source": [
"def similarity(query, document):\n",
" numerator = np.sum(query * document)\n",
" denominator = np.sqrt(np.sum(query*query)) * np.sqrt(np.sum(document*document)) \n",
" return numerator / denominator"
]
2021-03-09 13:46:07 +01:00
}
],
"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",
2021-03-24 12:38:00 +01:00
"version": "3.8.3"
2021-03-09 13:46:07 +01:00
}
},
"nbformat": 4,
"nbformat_minor": 4
}