297 lines
7.7 KiB
Text
297 lines
7.7 KiB
Text
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Datasketch\n",
|
|
"https://pypi.org/project/datasketch/"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 30,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Requirement already satisfied: datasketch in /anaconda3/lib/python3.7/site-packages (1.4.3)\n",
|
|
"Requirement already satisfied: numpy>=1.11 in /anaconda3/lib/python3.7/site-packages (from datasketch) (1.16.1)\n",
|
|
"Requirement already satisfied: redis>=2.10.0 in /anaconda3/lib/python3.7/site-packages (from datasketch) (3.2.1)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"!pip install datasketch"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 43,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from datasketch import MinHash, MinHashLSH\n",
|
|
"\n",
|
|
"from nltk import ngrams\n",
|
|
"from nltk import word_tokenize"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 44,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"text_array = [\"A bird in hand is worth two in the bush.\",\n",
|
|
" \"Good things come to those who wait.\",\n",
|
|
" \"There are other fish in the sea.\",\n",
|
|
" \"The ball is in your court.\"]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 45,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[['A', 'bird', 'in', 'hand', 'is', 'worth', 'two', 'in', 'the', 'bush', '.'],\n",
|
|
" ['Good', 'things', 'come', 'to', 'those', 'who', 'wait', '.'],\n",
|
|
" ['There', 'are', 'other', 'fish', 'in', 'the', 'sea', '.'],\n",
|
|
" ['The', 'ball', 'is', 'in', 'your', 'court', '.']]"
|
|
]
|
|
},
|
|
"execution_count": 45,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"word_token_array = [word_tokenize(text) for text in text_array]\n",
|
|
"\n",
|
|
"word_token_array"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 46,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0 ('A', 'bird', 'in')\n",
|
|
"0 ('bird', 'in', 'hand')\n",
|
|
"0 ('in', 'hand', 'is')\n",
|
|
"0 ('hand', 'is', 'worth')\n",
|
|
"0 ('is', 'worth', 'two')\n",
|
|
"0 ('worth', 'two', 'in')\n",
|
|
"0 ('two', 'in', 'the')\n",
|
|
"0 ('in', 'the', 'bush')\n",
|
|
"0 ('the', 'bush', '.')\n",
|
|
"1 ('Good', 'things', 'come')\n",
|
|
"1 ('things', 'come', 'to')\n",
|
|
"1 ('come', 'to', 'those')\n",
|
|
"1 ('to', 'those', 'who')\n",
|
|
"1 ('those', 'who', 'wait')\n",
|
|
"1 ('who', 'wait', '.')\n",
|
|
"2 ('There', 'are', 'other')\n",
|
|
"2 ('are', 'other', 'fish')\n",
|
|
"2 ('other', 'fish', 'in')\n",
|
|
"2 ('fish', 'in', 'the')\n",
|
|
"2 ('in', 'the', 'sea')\n",
|
|
"2 ('the', 'sea', '.')\n",
|
|
"3 ('The', 'ball', 'is')\n",
|
|
"3 ('ball', 'is', 'in')\n",
|
|
"3 ('is', 'in', 'your')\n",
|
|
"3 ('in', 'your', 'court')\n",
|
|
"3 ('your', 'court', '.')\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for index, word_tokens in enumerate(word_token_array):\n",
|
|
" for n_gram in ngrams(word_tokens, 3):\n",
|
|
" print(index, n_gram)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 47,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"min_hash_lsh = MinHashLSH(threshold=0.5, num_perm=128)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 48,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"min_hashes = {}\n",
|
|
"\n",
|
|
"for index, text in enumerate(text_array):\n",
|
|
" min_hash = MinHash(num_perm=128)\n",
|
|
"\n",
|
|
" for n_gram in ngrams(text, 3):\n",
|
|
" min_hash.update(\"\".join(n_gram).encode('utf-8'))\n",
|
|
" \n",
|
|
" min_hash_lsh.insert(index, min_hash)\n",
|
|
" min_hashes[index] = min_hash"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 49,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{0: <datasketch.minhash.MinHash at 0x11bc3a9b0>,\n",
|
|
" 1: <datasketch.minhash.MinHash at 0x11bc3ab00>,\n",
|
|
" 2: <datasketch.minhash.MinHash at 0x11bc392e8>,\n",
|
|
" 3: <datasketch.minhash.MinHash at 0x11bc39320>}"
|
|
]
|
|
},
|
|
"execution_count": 49,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"min_hashes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 50,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 0 : [0]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 1 : [1]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 2 : [2]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 3 : [3]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for i in min_hashes.keys():\n",
|
|
" result = min_hash_lsh.query(min_hashes[i])\n",
|
|
" print(\"Candidate pairs with Jaccard similarity > 0.5 for input\", i, \":\", result)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 55,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"text_array = [\"A bird in hand is worth two in the bush.\",\n",
|
|
" \"A bird in hands is worth three in the bushes.\",\n",
|
|
" \"Good things come to those who wait.\",\n",
|
|
" \"Good tpings cxme to those who wait long.\",\n",
|
|
" \"There are other fish in the sea.\",\n",
|
|
" \"The ball is in your court.\"\n",
|
|
" ]"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 56,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"min_hash_lsh = MinHashLSH(threshold=0.5, num_perm=128)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 57,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"min_hashes = {}\n",
|
|
"\n",
|
|
"for index, text in enumerate(text_array):\n",
|
|
" min_hash = MinHash(num_perm=128)\n",
|
|
"\n",
|
|
" for n_gram in ngrams(text, 3):\n",
|
|
" min_hash.update(\"\".join(n_gram).encode('utf-8'))\n",
|
|
" \n",
|
|
" min_hash_lsh.insert(index, min_hash)\n",
|
|
" min_hashes[index] = min_hash"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 58,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 0 : [0, 1]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 1 : [0, 1]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 2 : [2, 3]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 3 : [2, 3]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 4 : [4]\n",
|
|
"Candidate pairs with Jaccard similarity > 0.5 for input 5 : [5]\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"for i in min_hashes.keys():\n",
|
|
" result = min_hash_lsh.query(min_hashes[i])\n",
|
|
" print(\"Candidate pairs with Jaccard similarity > 0.5 for input\", i, \":\", result)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"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.1"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|