├── All sorting techniques.ipynb
├── Bitcoin mining Cryptography Blockchain.ipynb
├── Black hole number 4 - Cosmic number solution.ipynb
├── Connect mysql to python.ipynb
├── Data extraction Python libraries.ipynb
├── Distance calculation between two Geographic location locations.ipynb
├── Django Noes.ipynb
├── Download Hisorical data of stocks.ipynb
├── Easy OCR convert image to text.ipynb
├── Face Recognition Machine Learning.ipynb
├── Image Processing .ipynb
├── Levenshtein Distance Algorithm .ipynb
├── Mutable immutable concept .ipynb
├── NLP -Spacy Custom Entity Ruler.ipynb
├── Quick Python.ipynb
├── Solutions.ipynb
├── Speech Recognition Libraries.ipynb
├── Technical Indicator's of Stock market.ipynb
├── Zerodha Live Trading .ipynb
├── pandu.ipynb
├── readme.md
├── simple api on flask.ipynb
└── titanic_data.csv
/All sorting techniques.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "iObvH3HyOcpU"
7 | },
8 | "source": [
9 | "# Bubble Sort "
10 | ]
11 | },
12 | {
13 | "cell_type": "code",
14 | "execution_count": 5,
15 | "metadata": {
16 | "id": "QNnvPHkKOcpf",
17 | "outputId": "ac3126ce-1354-436d-e30c-015864414482"
18 | },
19 | "outputs": [
20 | {
21 | "data": {
22 | "text/plain": [
23 | "[2, 2, 2, 3, 4, 4, 4, 4, 4, 5, 5, 6, 6, 6, 6, 6, 7, 8, 8]"
24 | ]
25 | },
26 | "execution_count": 5,
27 | "metadata": {},
28 | "output_type": "execute_result"
29 | }
30 | ],
31 | "source": [
32 | "def bubble_sort(arr):\n",
33 | " def swap(i, j):\n",
34 | " arr[i], arr[j] = arr[j], arr[i]\n",
35 | "\n",
36 | " n = len(arr)\n",
37 | " swapped = True\n",
38 | " \n",
39 | " x = -1\n",
40 | " while swapped:\n",
41 | " swapped = False\n",
42 | " x = x + 1\n",
43 | " for i in range(1, n-x):\n",
44 | " if arr[i - 1] > arr[i]:\n",
45 | " swap(i - 1, i)\n",
46 | " swapped = True\n",
47 | " \n",
48 | " return arr\n",
49 | "ar=[6,8,4,6,2,4,7,6,8,5,2,4,6,2,5,4,6,3,4]\n",
50 | "z=bubble_sort(ar)\n",
51 | "z"
52 | ]
53 | },
54 | {
55 | "cell_type": "markdown",
56 | "metadata": {
57 | "id": "eQmmIuluOcpi"
58 | },
59 | "source": [
60 | "# selection sort\n"
61 | ]
62 | },
63 | {
64 | "cell_type": "code",
65 | "execution_count": null,
66 | "metadata": {
67 | "id": "Qr7oC7bWOcpi",
68 | "outputId": "4c2f5dad-d8bc-4ae6-d9f1-9ba84ea8ab87"
69 | },
70 | "outputs": [
71 | {
72 | "data": {
73 | "text/plain": [
74 | "[2, 2, 2, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8]"
75 | ]
76 | },
77 | "execution_count": 25,
78 | "metadata": {
79 | "tags": []
80 | },
81 | "output_type": "execute_result"
82 | }
83 | ],
84 | "source": [
85 | "def selection_sort(arr): \n",
86 | " for i in range(len(arr)):\n",
87 | " minimum = i\n",
88 | " for j in range(i + 1, len(arr)):\n",
89 | " if arr[j] < arr[minimum]:\n",
90 | " minimum = j\n",
91 | "\n",
92 | " arr[minimum], arr[i] = arr[i], arr[minimum]\n",
93 | " \n",
94 | " return arr\n",
95 | "ar=[6,8,4,6,2,4,7,6,8,5,2,4,6,2,5]\n",
96 | "z=selection_sort(ar)\n",
97 | "z"
98 | ]
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "metadata": {
103 | "id": "C8O4QWGrOcpj"
104 | },
105 | "source": [
106 | "# insertion sort"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": null,
112 | "metadata": {
113 | "id": "XQ0Vp_i3Ocpk",
114 | "outputId": "54d84451-3dd8-48a7-e94a-9d65271e1f50"
115 | },
116 | "outputs": [
117 | {
118 | "data": {
119 | "text/plain": [
120 | "[2, 2, 2, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8]"
121 | ]
122 | },
123 | "execution_count": 19,
124 | "metadata": {
125 | "tags": []
126 | },
127 | "output_type": "execute_result"
128 | }
129 | ],
130 | "source": [
131 | "def insersion_sort(arr, simulation=False):\n",
132 | " \n",
133 | " for i in range(len(arr)):\n",
134 | " cursor = arr[i]\n",
135 | " pos = i\n",
136 | " \n",
137 | " while pos > 0 and arr[pos - 1] > cursor:\n",
138 | " # Swap the number down the list\n",
139 | " arr[pos] = arr[pos - 1]\n",
140 | " pos = pos - 1\n",
141 | " # Break and do the final swap\n",
142 | " arr[pos] = cursor\n",
143 | "\n",
144 | " return arr\n",
145 | "ar=[6,8,4,6,2,4,7,6,8,5,2,4,6,2,5]\n",
146 | "z=insersion_sort(ar)\n",
147 | "z"
148 | ]
149 | },
150 | {
151 | "cell_type": "markdown",
152 | "metadata": {
153 | "id": "1FenLqBgOcpk"
154 | },
155 | "source": [
156 | "# Merge sort"
157 | ]
158 | },
159 | {
160 | "cell_type": "code",
161 | "execution_count": null,
162 | "metadata": {
163 | "id": "AV_wieX4Ocpl",
164 | "outputId": "3bb99140-4717-4442-fcea-881436af4384"
165 | },
166 | "outputs": [
167 | {
168 | "data": {
169 | "text/plain": [
170 | "[2, 2, 2, 4, 4, 4, 5, 5, 6, 6, 6, 6, 7, 8, 8]"
171 | ]
172 | },
173 | "execution_count": 20,
174 | "metadata": {
175 | "tags": []
176 | },
177 | "output_type": "execute_result"
178 | }
179 | ],
180 | "source": [
181 | "def merge_sort(arr):\n",
182 | " # The last array split\n",
183 | " if len(arr) <= 1:\n",
184 | " return arr\n",
185 | " mid = len(arr) // 2\n",
186 | " # Perform merge_sort recursively on both halves\n",
187 | " left, right = merge_sort(arr[:mid]), merge_sort(arr[mid:])\n",
188 | "\n",
189 | " # Merge each side together\n",
190 | " return merge(left, right, arr.copy())\n",
191 | "\n",
192 | "\n",
193 | "def merge(left, right, merged):\n",
194 | "\n",
195 | " left_cursor, right_cursor = 0, 0\n",
196 | " while left_cursor < len(left) and right_cursor < len(right):\n",
197 | " \n",
198 | " # Sort each one and place into the result\n",
199 | " if left[left_cursor] <= right[right_cursor]:\n",
200 | " merged[left_cursor+right_cursor]=left[left_cursor]\n",
201 | " left_cursor += 1\n",
202 | " else:\n",
203 | " merged[left_cursor + right_cursor] = right[right_cursor]\n",
204 | " right_cursor += 1\n",
205 | " \n",
206 | " for left_cursor in range(left_cursor, len(left)):\n",
207 | " merged[left_cursor + right_cursor] = left[left_cursor]\n",
208 | " \n",
209 | " for right_cursor in range(right_cursor, len(right)):\n",
210 | " merged[left_cursor + right_cursor] = right[right_cursor]\n",
211 | "\n",
212 | " return merged\n",
213 | "ar=[6,8,4,6,2,4,7,6,8,5,2,4,6,2,5]\n",
214 | "z=merge_sort(ar)\n",
215 | "z"
216 | ]
217 | }
218 | ],
219 | "metadata": {
220 | "colab": {
221 | "name": "All sorting techniques.ipynb",
222 | "provenance": []
223 | },
224 | "kernelspec": {
225 | "display_name": "Python 3",
226 | "language": "python",
227 | "name": "python3"
228 | },
229 | "language_info": {
230 | "codemirror_mode": {
231 | "name": "ipython",
232 | "version": 3
233 | },
234 | "file_extension": ".py",
235 | "mimetype": "text/x-python",
236 | "name": "python",
237 | "nbconvert_exporter": "python",
238 | "pygments_lexer": "ipython3",
239 | "version": "3.6.7"
240 | }
241 | },
242 | "nbformat": 4,
243 | "nbformat_minor": 1
244 | }
245 |
--------------------------------------------------------------------------------
/Black hole number 4 - Cosmic number solution.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "22de7931",
6 | "metadata": {},
7 | "source": [
8 | "## Theory - Cosmic number / Black hole number 4 "
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "e2107077",
14 | "metadata": {},
15 | "source": [
16 | "4 being called a “Cosmic number” or something but never knew its also called a Black hole Number.\n",
17 | "\n",
18 | "Name any number. Doesn’t matter if its negative, positive, Rational, irrational, complex or anything.\n",
19 | "\n",
20 | "* For our example : Lets take 3.\n",
21 | "\n",
22 | " * Now count the number of letters in its name.\n",
23 | "\n",
24 | " * So THREE has 5 letters.\n",
25 | "\n",
26 | " * Now count the no. of letters in 5.\n",
27 | "\n",
28 | " * So FIVE has 4 letters.\n",
29 | "\n",
30 | " * Keep going.Repeat the same step again. Count the no. of letters in 4 .\n",
31 | "\n",
32 | " * Now you would only find yourself looping at 4.\n",
33 | "\n",
34 | " * Fourever.\n",
35 | "\n",
36 | " * You cannot escape this loop, in the same sense that you cannot espace a BH. So the name.\n",
37 | "\n",
38 | " * You can do this with any number and you will always, eventually, wind up at four.\n",
39 | "\n",
40 | " * We get stuck at four because its the only no. in the English language, spelled with the same no. of letters as the quantity it represents.\n",
41 | "\n"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "id": "b40191d4",
47 | "metadata": {},
48 | "source": [
49 | "#### Example"
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "id": "0e0342b9",
55 | "metadata": {},
56 | "source": [
57 | "* Start with any whole number, and write out its numeral in English.\n",
58 | " * Start with the number 5 -> FIVE\n",
59 | " * Count the number of characters in its spelling (count spaces and hyphens).\n",
60 | " * 4 -> FOUR\n",
61 | " * Repeat and you get 4.\n",
62 | " * Repeat and you get 4.\n",
63 | " * Once you reach 4, you never get out, so you have reached the black hole.\n",
64 | "\n",
65 | "* Start with the number 163 -> ONE-HUNDRED SIXTY-THREE\n",
66 | " * Count the number of characters in its spelling (count spaces and hyphens).\n",
67 | " * 23 -> 12 -> 6 -> 3 -> 5 -> 4\n",
68 | " * Repeat and you get 4.\n",
69 | " * Repeat and you get 4.\n",
70 | " * Once you reach 4, you never get out, so you have reached the black hole."
71 | ]
72 | },
73 | {
74 | "cell_type": "markdown",
75 | "id": "0e200270",
76 | "metadata": {},
77 | "source": [
78 | "## Proof"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": 1,
84 | "id": "95176f7d",
85 | "metadata": {},
86 | "outputs": [],
87 | "source": [
88 | "#pip install num2words\n",
89 | "from num2words import num2words"
90 | ]
91 | },
92 | {
93 | "cell_type": "code",
94 | "execution_count": 17,
95 | "id": "2c2ff64f",
96 | "metadata": {},
97 | "outputs": [],
98 | "source": [
99 | "\n",
100 | "# function to integer word to string\n",
101 | "def check(word):\n",
102 | " return len(num2words(word)), (num2words(word))\n",
103 | "\n",
104 | "#Main function\n",
105 | "def black_hole_number(word):\n",
106 | " print(\"*\"*20)\n",
107 | " word_ = ''.join(word.split())\n",
108 | " word= len(word) \n",
109 | " print(f\"\\nInput word is {word_} \\n\\n \\tNow, word {word_} have {word} letters\")\n",
110 | " \n",
111 | " while True:\n",
112 | " \n",
113 | " word , name= check(word)\n",
114 | " print(f\"\\tNow, word {name} have {word} letters\")\n",
115 | " \n",
116 | " #checking if 4 or not \n",
117 | " if word == 4:\n",
118 | " print(f\"\\nEnd of Process with 4 ! because {name} has 4 words! So reached the black hole\")\n",
119 | " print(\"*\"*20,\"\\n\")\n",
120 | " break\n",
121 | " \n",
122 | " else:\n",
123 | " check(word)\n",
124 | " "
125 | ]
126 | },
127 | {
128 | "cell_type": "markdown",
129 | "id": "54f3599f",
130 | "metadata": {},
131 | "source": [
132 | "* Run function"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 18,
138 | "id": "74b4659f",
139 | "metadata": {},
140 | "outputs": [
141 | {
142 | "name": "stdout",
143 | "output_type": "stream",
144 | "text": [
145 | "Input any word/string/name: Ashish Kumar\n",
146 | "********************\n",
147 | "\n",
148 | "Input word is AshishKumar \n",
149 | "\n",
150 | " \tNow, word AshishKumar have 12 letters\n",
151 | "\tNow, word twelve have 6 letters\n",
152 | "\tNow, word six have 3 letters\n",
153 | "\tNow, word three have 5 letters\n",
154 | "\tNow, word five have 4 letters\n",
155 | "\n",
156 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
157 | "******************** \n",
158 | "\n"
159 | ]
160 | }
161 | ],
162 | "source": [
163 | "#take any input\n",
164 | "word= input(\"Input any word/string/name: \")\n",
165 | "\n",
166 | "black_hole_number(word)"
167 | ]
168 | },
169 | {
170 | "cell_type": "markdown",
171 | "id": "707db752",
172 | "metadata": {},
173 | "source": [
174 | "*****"
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "id": "5383ebf6",
180 | "metadata": {},
181 | "source": [
182 | "## Multiple test Cases"
183 | ]
184 | },
185 | {
186 | "cell_type": "code",
187 | "execution_count": 21,
188 | "id": "75d44433",
189 | "metadata": {},
190 | "outputs": [],
191 | "source": [
192 | "test_cases =['1','', 'hi', \"elephant\",'laptop', \"india\", \"This is footboll ground\", 'twelve','loops']"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": 22,
198 | "id": "75384ad2",
199 | "metadata": {},
200 | "outputs": [
201 | {
202 | "name": "stdout",
203 | "output_type": "stream",
204 | "text": [
205 | "********************\n",
206 | "\n",
207 | "Input word is 1 \n",
208 | "\n",
209 | " \tNow, word 1 have 1 letters\n",
210 | "\tNow, word one have 3 letters\n",
211 | "\tNow, word three have 5 letters\n",
212 | "\tNow, word five have 4 letters\n",
213 | "\n",
214 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
215 | "******************** \n",
216 | "\n",
217 | "********************\n",
218 | "\n",
219 | "Input word is \n",
220 | "\n",
221 | " \tNow, word have 0 letters\n",
222 | "\tNow, word zero have 4 letters\n",
223 | "\n",
224 | "End of Process with 4 ! because zero has 4 words! So reached the black hole\n",
225 | "******************** \n",
226 | "\n",
227 | "********************\n",
228 | "\n",
229 | "Input word is hi \n",
230 | "\n",
231 | " \tNow, word hi have 2 letters\n",
232 | "\tNow, word two have 3 letters\n",
233 | "\tNow, word three have 5 letters\n",
234 | "\tNow, word five have 4 letters\n",
235 | "\n",
236 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
237 | "******************** \n",
238 | "\n",
239 | "********************\n",
240 | "\n",
241 | "Input word is elephant \n",
242 | "\n",
243 | " \tNow, word elephant have 8 letters\n",
244 | "\tNow, word eight have 5 letters\n",
245 | "\tNow, word five have 4 letters\n",
246 | "\n",
247 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
248 | "******************** \n",
249 | "\n",
250 | "********************\n",
251 | "\n",
252 | "Input word is laptop \n",
253 | "\n",
254 | " \tNow, word laptop have 6 letters\n",
255 | "\tNow, word six have 3 letters\n",
256 | "\tNow, word three have 5 letters\n",
257 | "\tNow, word five have 4 letters\n",
258 | "\n",
259 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
260 | "******************** \n",
261 | "\n",
262 | "********************\n",
263 | "\n",
264 | "Input word is india \n",
265 | "\n",
266 | " \tNow, word india have 5 letters\n",
267 | "\tNow, word five have 4 letters\n",
268 | "\n",
269 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
270 | "******************** \n",
271 | "\n",
272 | "********************\n",
273 | "\n",
274 | "Input word is Thisisfootbollground \n",
275 | "\n",
276 | " \tNow, word Thisisfootbollground have 23 letters\n",
277 | "\tNow, word twenty-three have 12 letters\n",
278 | "\tNow, word twelve have 6 letters\n",
279 | "\tNow, word six have 3 letters\n",
280 | "\tNow, word three have 5 letters\n",
281 | "\tNow, word five have 4 letters\n",
282 | "\n",
283 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
284 | "******************** \n",
285 | "\n",
286 | "********************\n",
287 | "\n",
288 | "Input word is twelve \n",
289 | "\n",
290 | " \tNow, word twelve have 6 letters\n",
291 | "\tNow, word six have 3 letters\n",
292 | "\tNow, word three have 5 letters\n",
293 | "\tNow, word five have 4 letters\n",
294 | "\n",
295 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
296 | "******************** \n",
297 | "\n",
298 | "********************\n",
299 | "\n",
300 | "Input word is loops \n",
301 | "\n",
302 | " \tNow, word loops have 5 letters\n",
303 | "\tNow, word five have 4 letters\n",
304 | "\n",
305 | "End of Process with 4 ! because five has 4 words! So reached the black hole\n",
306 | "******************** \n",
307 | "\n"
308 | ]
309 | },
310 | {
311 | "data": {
312 | "text/plain": [
313 | "[None, None, None, None, None, None, None, None, None]"
314 | ]
315 | },
316 | "execution_count": 22,
317 | "metadata": {},
318 | "output_type": "execute_result"
319 | }
320 | ],
321 | "source": [
322 | "#Run all cases\n",
323 | "list(map(black_hole_number, test_cases))"
324 | ]
325 | },
326 | {
327 | "cell_type": "markdown",
328 | "id": "3d0f7d68",
329 | "metadata": {},
330 | "source": [
331 | "*****"
332 | ]
333 | }
334 | ],
335 | "metadata": {
336 | "kernelspec": {
337 | "display_name": "Python 3",
338 | "language": "python",
339 | "name": "python3"
340 | },
341 | "language_info": {
342 | "codemirror_mode": {
343 | "name": "ipython",
344 | "version": 3
345 | },
346 | "file_extension": ".py",
347 | "mimetype": "text/x-python",
348 | "name": "python",
349 | "nbconvert_exporter": "python",
350 | "pygments_lexer": "ipython3",
351 | "version": "3.6.7"
352 | }
353 | },
354 | "nbformat": 4,
355 | "nbformat_minor": 5
356 | }
357 |
--------------------------------------------------------------------------------
/Connect mysql to python.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "2b05458c",
6 | "metadata": {},
7 | "source": [
8 | "## Install all Libraries"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "b1abc4ec",
14 | "metadata": {},
15 | "source": [
16 | "Install mysql database in linux by using terminal"
17 | ]
18 | },
19 | {
20 | "cell_type": "raw",
21 | "id": "2674d359",
22 | "metadata": {},
23 | "source": [
24 | "sudo apt install mysql-server\n",
25 | "install mysql python connector to connect daabase\n",
26 | "pip install mysql-connector-python"
27 | ]
28 | },
29 | {
30 | "cell_type": "markdown",
31 | "id": "279d7156",
32 | "metadata": {},
33 | "source": [
34 | "## Setup database using brew -mac os"
35 | ]
36 | },
37 | {
38 | "cell_type": "raw",
39 | "id": "9bcef0c4",
40 | "metadata": {},
41 | "source": [
42 | "brew install mysql"
43 | ]
44 | },
45 | {
46 | "cell_type": "raw",
47 | "id": "879564a0",
48 | "metadata": {},
49 | "source": [
50 | "brew services start mysql"
51 | ]
52 | },
53 | {
54 | "cell_type": "raw",
55 | "id": "8575145b",
56 | "metadata": {},
57 | "source": [
58 | "mysql_secure_installation"
59 | ]
60 | },
61 | {
62 | "cell_type": "raw",
63 | "id": "af829d82",
64 | "metadata": {},
65 | "source": [
66 | "mysql -u root -p"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "id": "d141efc0",
72 | "metadata": {},
73 | "source": [
74 | "## Create user"
75 | ]
76 | },
77 | {
78 | "cell_type": "raw",
79 | "id": "9649792a",
80 | "metadata": {},
81 | "source": [
82 | "mysql> CREATE USER 'ashish'@'localhost' IDENTIFIED BY 'my-strong-password-here';"
83 | ]
84 | },
85 | {
86 | "cell_type": "markdown",
87 | "id": "e1701abb",
88 | "metadata": {},
89 | "source": [
90 | "## Grant all permision"
91 | ]
92 | },
93 | {
94 | "cell_type": "raw",
95 | "id": "a458d2ab",
96 | "metadata": {},
97 | "source": [
98 | "GRANT ALL PRIVILEGES ON * . * TO 'ashish'@'localhost';"
99 | ]
100 | },
101 | {
102 | "cell_type": "markdown",
103 | "id": "f708df5f",
104 | "metadata": {},
105 | "source": [
106 | "## Python code to connect"
107 | ]
108 | },
109 | {
110 | "cell_type": "code",
111 | "execution_count": null,
112 | "id": "fa92d3b4",
113 | "metadata": {},
114 | "outputs": [],
115 | "source": [
116 | "import mysql.connector\n",
117 | "\n",
118 | "mydb = mysql.connector.connect(\n",
119 | " host=\"localhost\",\n",
120 | " user=\"ashish\",\n",
121 | " password=\"ashish@123\"\n",
122 | " database='storemviedata'\n",
123 | ")\n",
124 | "\n",
125 | "print(mydb)"
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "id": "7e1b9b69",
131 | "metadata": {},
132 | "source": [
133 | "# Create table\n"
134 | ]
135 | },
136 | {
137 | "cell_type": "code",
138 | "execution_count": null,
139 | "id": "1d36fdc6",
140 | "metadata": {},
141 | "outputs": [],
142 | "source": [
143 | "mycursor = mydb.cursor()\n",
144 | "\n",
145 | "mycursor.execute(\"CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))\")\n",
146 | "Show tables in DB"
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "id": "92ac777f",
152 | "metadata": {},
153 | "source": [
154 | "# Show tables in DB"
155 | ]
156 | },
157 | {
158 | "cell_type": "code",
159 | "execution_count": null,
160 | "id": "77c10222",
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "mycursor = mydb.cursor()\n",
165 | "\n",
166 | "mycursor.execute(\"SHOW TABLES\")\n",
167 | "\n",
168 | "for x in mycursor:\n",
169 | " print(x)"
170 | ]
171 | },
172 | {
173 | "cell_type": "markdown",
174 | "id": "348cd8b6",
175 | "metadata": {},
176 | "source": [
177 | "# Insert data to table customers\n"
178 | ]
179 | },
180 | {
181 | "cell_type": "code",
182 | "execution_count": null,
183 | "id": "e4b09f32",
184 | "metadata": {},
185 | "outputs": [],
186 | "source": [
187 | "mycursor = mydb.cursor()\n",
188 | "\n",
189 | "sql = \"INSERT INTO customers (name, address) VALUES (%s, %s)\"\n",
190 | "val = (\"John\", \"Highway 21\")\n",
191 | "mycursor.execute(sql, val)\n",
192 | "\n",
193 | "mydb.commit()\n",
194 | "\n",
195 | "print(mycursor.rowcount, \"record inserted.\")"
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "id": "db20f630",
201 | "metadata": {},
202 | "source": [
203 | "## Code to insert data in database"
204 | ]
205 | },
206 | {
207 | "cell_type": "code",
208 | "execution_count": null,
209 | "id": "445cd83f",
210 | "metadata": {},
211 | "outputs": [],
212 | "source": [
213 | "class Database:\n",
214 | " \"\"\" Class to work with database , Store data in database\"\"\"\n",
215 | "\n",
216 | " def __init__(self, host, username, password, database):\n",
217 | "\n",
218 | " self.host = host\n",
219 | " self.username = username\n",
220 | " self.password = password\n",
221 | " self.database = database\n",
222 | " \n",
223 | " self.connection_object = connector.connect(host=self.host,\n",
224 | " username=self.username,\n",
225 | " password=self.password,\n",
226 | " database=self.database,\n",
227 | " )\n",
228 | "\n",
229 | " def create_table(self, ):\n",
230 | "\n",
231 | " try:\n",
232 | " cur = self.connection_object.cursor()\n",
233 | "\n",
234 | " # create table if NOT exist\n",
235 | "\n",
236 | " cur.execute(\n",
237 | " \"create table if not exists Movie_table(MovieName varchar(200) not null, DaysfromRelease varchar(20) not null, Date varchar(30) not null, BoxOfficeCollection varchar(20) not null)\")\n",
238 | "\n",
239 | " except Exception as e:\n",
240 | "\n",
241 | " print(\"Error in creating table\", str(e))\n",
242 | " self.connection_object.rollback()\n",
243 | "\n",
244 | " def insert_data(self, movie_name_insert, day_insert, converted_day_insert, collection_insert):\n",
245 | " \n",
246 | " # insert data to table\n",
247 | "\n",
248 | " cur = self.connection_object.cursor()\n",
249 | " try:\n",
250 | " sql = \"insert into Movie_table(MovieName, DaysfromRelease, Date, BoxOfficeCollection) values (%s, %s, %s, %s)\"\n",
251 | "\n",
252 | " # The row values are provided in the form of tuple\n",
253 | " val = (movie_name_insert, day_insert, converted_day_insert, collection_insert)\n",
254 | "\n",
255 | " # inserting the values into the table\n",
256 | " cur.execute(sql, val)\n",
257 | "\n",
258 | " # commit the transaction\n",
259 | " self.connection_object.commit()\n",
260 | " # print(\"Record Inserted\")\n",
261 | "\n",
262 | " except Exception as e:\n",
263 | "\n",
264 | " print(\"Error in inserting record\", str(e))\n",
265 | " self.connection_object.rollback()\n",
266 | " \n",
267 | " def show_table_data(self):\n",
268 | "\n",
269 | " try:\n",
270 | " cur = self.connection_object.cursor()\n",
271 | " cur.execute(\"select * from Movie_table\")\n",
272 | " # fetching the rows from the cursor object\n",
273 | " result = cur.fetchall()\n",
274 | " # printing the result\n",
275 | "\n",
276 | " print(\"Fetching all data from table\")\n",
277 | "\n",
278 | " for data_fetch in result:\n",
279 | " print(data_fetch)\n",
280 | "\n",
281 | " except Exception as e:\n",
282 | " print(\"Error while fetching data\", str(e))\n",
283 | " self.connection_object.rollback()\n",
284 | "\n",
285 | " # close all now\n",
286 | " self.connection_object.close()\n"
287 | ]
288 | }
289 | ],
290 | "metadata": {
291 | "kernelspec": {
292 | "display_name": "Python 3",
293 | "language": "python",
294 | "name": "python3"
295 | },
296 | "language_info": {
297 | "codemirror_mode": {
298 | "name": "ipython",
299 | "version": 3
300 | },
301 | "file_extension": ".py",
302 | "mimetype": "text/x-python",
303 | "name": "python",
304 | "nbconvert_exporter": "python",
305 | "pygments_lexer": "ipython3",
306 | "version": "3.6.7"
307 | }
308 | },
309 | "nbformat": 4,
310 | "nbformat_minor": 5
311 | }
312 |
--------------------------------------------------------------------------------
/Data extraction Python libraries.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "colab_type": "text",
7 | "id": "view-in-github"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "markdown",
15 | "metadata": {
16 | "id": "-4dfsSoXlblO"
17 | },
18 | "source": [
19 | "##import all required libraries"
20 | ]
21 | },
22 | {
23 | "cell_type": "code",
24 | "execution_count": null,
25 | "metadata": {
26 | "id": "WiVXz7WCpIom"
27 | },
28 | "outputs": [],
29 | "source": [
30 | "import re\n",
31 | "import io\n",
32 | "import os\n",
33 | "import sys\n",
34 | "import PIL\n",
35 | "import json\n",
36 | "import time\n",
37 | "import spacy\n",
38 | "import fuckit\n",
39 | "import PyPDF2\n",
40 | "import tabula\n",
41 | "import string\n",
42 | "import pypdftk\n",
43 | "import secrets\n",
44 | "import pdfquery\n",
45 | "import winerror\n",
46 | "import textract\n",
47 | "import win32com\n",
48 | "import requests\n",
49 | "import pdf2image\n",
50 | "import pdftohtml\n",
51 | "import pdfreader\n",
52 | "import pdftotree\n",
53 | "import pdfplumber\n",
54 | "import pytesseract\n",
55 | "import numpy as np\n",
56 | "import PDFNetPython\n",
57 | "import pandas as pd\n",
58 | "import tesseract-ocr\n",
59 | "from PIL import Image\n",
60 | "from logging import *\n",
61 | "from io import StringIO\n",
62 | "from tika import parser\n",
63 | "import pytesseract as pt\n",
64 | "from pprint import pprint\n",
65 | "from tabula import read_pdf\n",
66 | "import miner_text_generator\n",
67 | "from xml.dom import minidom\n",
68 | "from wand.image import Image\n",
69 | "from itertools import compress\n",
70 | "from pytesseract import Output\n",
71 | "from google.cloud import vision\n",
72 | "import dateutil.parser as dparser\n",
73 | "import xml.etree.ElementTree as xml\n",
74 | "from pdfminer.layout import LAParams\n",
75 | "from pdfminer.pdfpage import PDFPage\n",
76 | "from difflib import get_close_matches\n",
77 | "from pdf2image import convert_from_path\n",
78 | "from pdfminer.pdfparser import PDFParser\n",
79 | "from werkzeug.utils import secure_filename\n",
80 | "from recursive_diff import recursive_diff x\n",
81 | "from botocore.exceptions import ClientError\n",
82 | "from win32com.client.dynamic import Dispatch\n",
83 | "from pdfminer.pdfdocument import PDFDocument\n",
84 | "from pdfminer.converter import TextConverter\n",
85 | "from pdfminer.high_level import extract_text\n",
86 | "from pdfminer.pdfinterp import PDFResourceManager\n",
87 | "from pdfminer.pdfinterp import PDFPageInterpreter\n",
88 | "from pdfreader import PDFDocument, SimplePDFViewer\n",
89 | "from miner_text_generator import extract_text_by_page\n",
90 | "from win32com.client.dynamic import Dispatch, ERRORS_BAD_CONTEXT\n",
91 | "from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter"
92 | ]
93 | },
94 | {
95 | "cell_type": "markdown",
96 | "metadata": {
97 | "id": "lN7qggo-pKaF"
98 | },
99 | "source": [
100 | "##Filename"
101 | ]
102 | },
103 | {
104 | "cell_type": "code",
105 | "execution_count": null,
106 | "metadata": {
107 | "id": "3YG_eUJ4lc9s"
108 | },
109 | "outputs": [],
110 | "source": [
111 | "filename=\"bank_statement_ashish.pdf\"\n",
112 | "image_filename='image.jpg'\n",
113 | "pdffilename='nlp_research.pdf'"
114 | ]
115 | },
116 | {
117 | "cell_type": "markdown",
118 | "metadata": {
119 | "id": "N9qzslQun4Cp"
120 | },
121 | "source": [
122 | "# Tabula\n"
123 | ]
124 | },
125 | {
126 | "cell_type": "markdown",
127 | "metadata": {
128 | "id": "xBTnsq-ln7aS"
129 | },
130 | "source": [
131 | "Extracting tabular data from tabula"
132 | ]
133 | },
134 | {
135 | "cell_type": "code",
136 | "execution_count": null,
137 | "metadata": {
138 | "id": "kA2KlqqLXKiB"
139 | },
140 | "outputs": [],
141 | "source": [
142 | "#pip install tabula-py\n",
143 | "\n",
144 | "import tabula\n",
145 | "\n",
146 | "df= tabula.read_pdf(filename, pages=\"all\",\n",
147 | " multiple_tables=True,\n",
148 | " guess=True, stream=False)\n",
149 | "print(\"output is\",\"*\"*40)\n",
150 | "\n",
151 | "print(\"Length of dataframe/data is\",len(df),\"\\n\")\n",
152 | "print(df[0])"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {
158 | "id": "kaeRnGUTpU9c"
159 | },
160 | "source": [
161 | "##combination of Pdfminer pdfplumber and pypdf2 to extract tabular data and string data"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": null,
167 | "metadata": {
168 | "id": "u9UTZWEoXKkh"
169 | },
170 | "outputs": [],
171 | "source": [
172 | "# pip install PyPDF2\n",
173 | "# pip install pdfplumber\n",
174 | "\n",
175 | "import PyPDF2\n",
176 | "import pdfplumber\n",
177 | "\n",
178 | "# @reading files\n",
179 | "\n",
180 | "#table_settings={\"vertical_strategy\": \"lines\", \"horizontal_strategy\": \"lines\"}\n",
181 | "\n",
182 | "\n",
183 | "def reading_list_data(filename):\n",
184 | " with pdfplumber.open(filename) as pdf:\n",
185 | " data = ''.join([page.extract_text() for page in pdf.pages])\n",
186 | " pages = PyPDF2.PdfFileReader(open(filename, 'rb')).numPages\n",
187 | " pdfdata = [pdfplumber.open(filename).pages[y].extract_table() for y in range(pages)]\n",
188 | " converting_data = lambda input_data: [item for sublist in pdfdata if sublist != None for item in sublist if\n",
189 | " item != None]\n",
190 | " clean_data = converting_data(pdfdata)\n",
191 | " list_of_list = []\n",
192 | " for single_list in clean_data:\n",
193 | " clean_list = []\n",
194 | " for word in single_list:\n",
195 | " if word == None:\n",
196 | " word = \"None\"\n",
197 | " clean_list.append(word)\n",
198 | " else:\n",
199 | " clean_list.append(word)\n",
200 | " list_of_list.append(clean_list)\n",
201 | " return list_of_list, data, pdfdata\n",
202 | "\n",
203 | "tabular_data,string_data,string_data_2=reading_list_data(filename)"
204 | ]
205 | },
206 | {
207 | "cell_type": "markdown",
208 | "metadata": {
209 | "id": "IpyjDFOkqpM6"
210 | },
211 | "source": [
212 | "##Pytessract"
213 | ]
214 | },
215 | {
216 | "cell_type": "markdown",
217 | "metadata": {
218 | "id": "1X701Smdqu06"
219 | },
220 | "source": [
221 | "Getting data from image"
222 | ]
223 | },
224 | {
225 | "cell_type": "code",
226 | "execution_count": null,
227 | "metadata": {
228 | "id": "skKj9VS6rB4P"
229 | },
230 | "outputs": [],
231 | "source": [
232 | "# pip install pdf2image\n",
233 | "# pip install pytesseract"
234 | ]
235 | },
236 | {
237 | "cell_type": "code",
238 | "execution_count": null,
239 | "metadata": {
240 | "id": "FRd9ansMrRnT"
241 | },
242 | "outputs": [],
243 | "source": [
244 | "import pytesseract\n",
245 | "from pdf2image import convert_from_path\n",
246 | "\n",
247 | "custom_config_lstm = '-c preserve_interword_spaces=1 --oem 3 --psm 1'\n",
248 | "for page in convert_from_path(image_filename):\n",
249 | " data= pytesseract.image_to_string(page, config=custom_config_lstm)\n",
250 | " print(data)"
251 | ]
252 | },
253 | {
254 | "cell_type": "markdown",
255 | "metadata": {
256 | "id": "M4eNvelhrxz5"
257 | },
258 | "source": [
259 | "##Get data from digital image using Textract"
260 | ]
261 | },
262 | {
263 | "cell_type": "code",
264 | "execution_count": null,
265 | "metadata": {
266 | "id": "pBIL-QXsXKnK"
267 | },
268 | "outputs": [],
269 | "source": [
270 | "# pip install textract"
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "execution_count": null,
276 | "metadata": {
277 | "id": "DjbD_9eRZhb0"
278 | },
279 | "outputs": [],
280 | "source": [
281 | "import textract\n",
282 | "\n",
283 | "text = textract.process(image_filename, method='tesseract', encoding='ascii',language='eng')\n",
284 | "print(text)"
285 | ]
286 | },
287 | {
288 | "cell_type": "markdown",
289 | "metadata": {
290 | "id": "-xRiHqnUZiJZ"
291 | },
292 | "source": [
293 | "Converting Pdf to image"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": null,
299 | "metadata": {
300 | "id": "ehbUNfx6Zhey"
301 | },
302 | "outputs": [],
303 | "source": [
304 | "from pdf2image import convert_from_path\n",
305 | "import pdf2image\n",
306 | "\n",
307 | "def pdf_to_img(pdf_file):\n",
308 | " return pdf2image.convert_from_path(pdf_file,600)\n",
309 | "\n",
310 | "\n",
311 | "def ocr_core(file):\n",
312 | " text = pytesseract.image_to_string(file)\n",
313 | " return text\n",
314 | "\n",
315 | "\n",
316 | "def print_pages(pdf_file):\n",
317 | " images = pdf_to_img(pdf_file)\n",
318 | " plt.figure(figsize=(20,20))\n",
319 | " for i in images:\n",
320 | " print(i)\n",
321 | " plt.imshow(i)\n",
322 | " i.save('out.jpg', 'JPEG')\n",
323 | "\n",
324 | " plt.show()\n",
325 | " for pg, img in enumerate(images):\n",
326 | " print(ocr_core(img))\n",
327 | "\n",
328 | "\n",
329 | "print_pages(image_filename) \n"
330 | ]
331 | },
332 | {
333 | "cell_type": "markdown",
334 | "metadata": {
335 | "id": "VWUdJKZ_Zxa-"
336 | },
337 | "source": [
338 | "## Covert Image data to string"
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": null,
344 | "metadata": {
345 | "id": "0yDQZ89pZy9U"
346 | },
347 | "outputs": [],
348 | "source": [
349 | "from PIL import Image\n",
350 | "data=pytesseract.image_to_string(Image.open(image_filename))\n",
351 | "print(data)"
352 | ]
353 | },
354 | {
355 | "cell_type": "markdown",
356 | "metadata": {
357 | "id": "TX2bM2DqZ1W4"
358 | },
359 | "source": [
360 | "## Reading files of Image PDF"
361 | ]
362 | },
363 | {
364 | "cell_type": "code",
365 | "execution_count": null,
366 | "metadata": {
367 | "id": "_72S6VRIZ1nl"
368 | },
369 | "outputs": [],
370 | "source": [
371 | "from pdf2image import convert_from_path\n",
372 | "\n",
373 | "pages = convert_from_path(filename, 500)\n",
374 | "i=1\n",
375 | "for page in pages:\n",
376 | " page.save('out'+str(i)+'.jpg', 'JPEG')\n",
377 | " text = pytesseract.image_to_string(Image.open('out'+str(i)+'.jpg'))\n",
378 | " #print(text)\n",
379 | " i=i+1"
380 | ]
381 | },
382 | {
383 | "cell_type": "markdown",
384 | "metadata": {
385 | "id": "Px5-Jecqs1yb"
386 | },
387 | "source": [
388 | "##Read File usig pdfplumber"
389 | ]
390 | },
391 | {
392 | "cell_type": "code",
393 | "execution_count": null,
394 | "metadata": {
395 | "id": "Lzr8STduZ7RG"
396 | },
397 | "outputs": [],
398 | "source": [
399 | "import pdfplumber\n",
400 | "with pdfplumber.open(filename) as pdf:\n",
401 | " pages=pdf.pages\n",
402 | " for page in pdf.pages:\n",
403 | " text=page.extract_text()\n",
404 | " print(text)\n",
405 | " break"
406 | ]
407 | },
408 | {
409 | "cell_type": "markdown",
410 | "metadata": {
411 | "id": "7QT4M9sMtFa1"
412 | },
413 | "source": [
414 | "## Read Heavy / Large fle in few seconds Optimize code of pdfminer"
415 | ]
416 | },
417 | {
418 | "cell_type": "code",
419 | "execution_count": null,
420 | "metadata": {
421 | "id": "Ja4TrjVOaNpS"
422 | },
423 | "outputs": [],
424 | "source": [
425 | "import io\n",
426 | "from pdfminer.converter import TextConverter\n",
427 | "from pdfminer.pdfinterp import PDFPageInterpreter\n",
428 | "from pdfminer.pdfinterp import PDFResourceManager\n",
429 | "from pdfminer.pdfpage import PDFPage\n",
430 | "\n",
431 | "\n",
432 | "def process_file(pdf_path):\n",
433 | " resource_manager = PDFResourceManager()\n",
434 | " fake_file_handle = io.StringIO()\n",
435 | " converter = TextConverter(resource_manager, fake_file_handle)\n",
436 | " page_interpreter = PDFPageInterpreter(resource_manager, converter)\n",
437 | " \n",
438 | " with open(pdf_path, 'rb') as fh:\n",
439 | " for page in PDFPage.get_pages(fh, caching=True, check_extractable=True):\n",
440 | " page_interpreter.process_page(page)\n",
441 | " text = fake_file_handle.getvalue()\n",
442 | " # close open handles\n",
443 | " converter.close()\n",
444 | " fake_file_handle.close()\n",
445 | " if text:\n",
446 | " return text\n",
447 | "data=process_file(filename)\n",
448 | "data"
449 | ]
450 | },
451 | {
452 | "cell_type": "markdown",
453 | "metadata": {
454 | "id": "IQ1dK2VFtTQh"
455 | },
456 | "source": [
457 | "## Read pdf fie from tika"
458 | ]
459 | },
460 | {
461 | "cell_type": "code",
462 | "execution_count": null,
463 | "metadata": {
464 | "id": "q01uxiaQaPy0"
465 | },
466 | "outputs": [],
467 | "source": [
468 | "from tika import parser\n",
469 | "parsedPDF = parser.from_file(\"testing.pdf\")\n",
470 | "#print(parsedPDF['content'].strip())"
471 | ]
472 | },
473 | {
474 | "cell_type": "markdown",
475 | "metadata": {
476 | "id": "EzCCXGSBlHNQ"
477 | },
478 | "source": [
479 | "## FITZ"
480 | ]
481 | },
482 | {
483 | "cell_type": "code",
484 | "execution_count": null,
485 | "metadata": {
486 | "id": "Ir71hvxoaP14"
487 | },
488 | "outputs": [],
489 | "source": [
490 | "\n",
491 | "import fitz\n",
492 | "myfile = 'testing.pdf'\n",
493 | "doc =fitz.open(myfile)\n",
494 | "page=doc[0]\n",
495 | "text = page.getText(\"text\")\n",
496 | "#rint(text)"
497 | ]
498 | },
499 | {
500 | "cell_type": "markdown",
501 | "metadata": {
502 | "id": "GUwiwHSQlByn"
503 | },
504 | "source": [
505 | "## PYPDF2 making XFA"
506 | ]
507 | },
508 | {
509 | "cell_type": "code",
510 | "execution_count": null,
511 | "metadata": {
512 | "id": "FTIPqx9RaUUI"
513 | },
514 | "outputs": [],
515 | "source": [
516 | "import PyPDF2 as pypdf\n",
517 | "def findInDict(needle, haystack):\n",
518 | " for key in haystack.keys():\n",
519 | " try:\n",
520 | " value=haystack[key]\n",
521 | " except:\n",
522 | " continue\n",
523 | " if key==needle:\n",
524 | " return value\n",
525 | " if isinstance(value,dict): \n",
526 | " x=findInDict(needle,value) \n",
527 | " if x is not None:\n",
528 | " return x\n",
529 | " \n",
530 | "pdfobject=open('test.pdf','rb')\n",
531 | "pdf=pypdf.PdfFileReader(pdfobject)\n",
532 | "\n",
533 | "xfa=findInDict('/XFA',pdf.resolvedObjects)\n",
534 | "xml=xfa[5].getObject().getData()\n",
535 | "\n",
536 | "\n",
537 | "#display(xml.decode('utf-8'))"
538 | ]
539 | },
540 | {
541 | "cell_type": "code",
542 | "execution_count": null,
543 | "metadata": {
544 | "id": "xDNbXmGxaUWh"
545 | },
546 | "outputs": [],
547 | "source": [
548 | "def extract_text_from_pdf(pdf_path):\n",
549 | " resource_manager = PDFResourceManager()\n",
550 | " fake_file_handle = io.StringIO()\n",
551 | " converter = TextConverter(resource_manager, fake_file_handle)\n",
552 | " page_interpreter = PDFPageInterpreter(resource_manager, converter)\n",
553 | " with open(pdf_path, 'rb') as fh:\n",
554 | " for page in PDFPage.get_pages(fh, \n",
555 | " caching=True,\n",
556 | " check_extractable=True):\n",
557 | " page_interpreter.process_page(page)\n",
558 | " text = fake_file_handle.getvalue()\n",
559 | " # close open handles\n",
560 | " converter.close()\n",
561 | " fake_file_handle.close()\n",
562 | " if text:\n",
563 | " return text\n",
564 | "if __name__ == '__main__':\n",
565 | " print(extract_text_from_pdf('testing.pdf'))"
566 | ]
567 | },
568 | {
569 | "cell_type": "markdown",
570 | "metadata": {
571 | "id": "LsCRcfpjkwlT"
572 | },
573 | "source": [
574 | "##PDFminer"
575 | ]
576 | },
577 | {
578 | "cell_type": "code",
579 | "execution_count": null,
580 | "metadata": {
581 | "id": "A59nfezkaYY_"
582 | },
583 | "outputs": [],
584 | "source": [
585 | "from io import StringIO\n",
586 | "from pdfminer.converter import TextConverter\n",
587 | "from pdfminer.layout import LAParams\n",
588 | "from pdfminer.pdfdocument import PDFDocument\n",
589 | "from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter\n",
590 | "from pdfminer.pdfpage import PDFPage\n",
591 | "from pdfminer.pdfparser import PDFParser\n",
592 | "\n",
593 | "output_string = StringIO()\n",
594 | "with open('testing.pdf', 'rb') as in_file:\n",
595 | " parser = PDFParser(in_file)\n",
596 | " doc = PDFDocument(parser)\n",
597 | " rsrcmgr = PDFResourceManager()\n",
598 | " device = TextConverter(rsrcmgr, output_string, laparams=LAParams())\n",
599 | " interpreter = PDFPageInterpreter(rsrcmgr, device)\n",
600 | " for page in PDFPage.create_pages(doc):\n",
601 | " interpreter.process_page(page)\n",
602 | "\n",
603 | "print(output_string.getvalue())"
604 | ]
605 | },
606 | {
607 | "cell_type": "markdown",
608 | "metadata": {
609 | "id": "LeimE_smkqY9"
610 | },
611 | "source": [
612 | "#Pypdf2"
613 | ]
614 | },
615 | {
616 | "cell_type": "code",
617 | "execution_count": null,
618 | "metadata": {
619 | "id": "NJKh8wkPaUct"
620 | },
621 | "outputs": [],
622 | "source": [
623 | "import PyPDF2\n",
624 | "import collections\n",
625 | "pdf_file = open('testing.pdf', 'rb')\n",
626 | "read_pdf = PyPDF2.PdfFileReader(pdf_file)\n",
627 | "number_of_pages = read_pdf.getNumPages()\n",
628 | "c = collections.Counter(range(number_of_pages))\n",
629 | "for i in c:\n",
630 | " page = read_pdf.getPage(i)\n",
631 | " page_content = page.extractText()\n",
632 | " #print (page_content.encode('utf-8'))"
633 | ]
634 | },
635 | {
636 | "cell_type": "markdown",
637 | "metadata": {
638 | "id": "Zmr4IV-ikjSR"
639 | },
640 | "source": [
641 | "#Textract"
642 | ]
643 | },
644 | {
645 | "cell_type": "code",
646 | "execution_count": null,
647 | "metadata": {
648 | "id": "WUrk6CU2adRp"
649 | },
650 | "outputs": [],
651 | "source": [
652 | "import textract\n",
653 | "text = textract.process(\"testing.pdf\")\n",
654 | "print(text)"
655 | ]
656 | },
657 | {
658 | "cell_type": "markdown",
659 | "metadata": {
660 | "id": "ZiBC0jE_kZ9_"
661 | },
662 | "source": [
663 | "#Textract"
664 | ]
665 | },
666 | {
667 | "cell_type": "code",
668 | "execution_count": null,
669 | "metadata": {
670 | "id": "SBXqdihlbMV2"
671 | },
672 | "outputs": [],
673 | "source": [
674 | "import webbrowser, os\n",
675 | "import json\n",
676 | "import secrets\n",
677 | "import pandas as pd\n",
678 | "import boto3\n",
679 | "import io\n",
680 | "import boto3\n",
681 | "import pytesseract\n",
682 | "from io import BytesIO\n",
683 | "import sys\n",
684 | "from pprint import pprint\n",
685 | "\n",
686 | "\n",
687 | "def get_rows_columns_map(table_result, blocks_map):\n",
688 | " rows = {}\n",
689 | " for relationship in table_result['Relationships']:\n",
690 | " if relationship['Type'] == 'CHILD':\n",
691 | " for child_id in relationship['Ids']:\n",
692 | " cell = blocks_map[child_id]\n",
693 | " if cell['BlockType'] == 'CELL':\n",
694 | " row_index = cell['RowIndex']\n",
695 | " col_index = cell['ColumnIndex']\n",
696 | " if row_index not in rows:\n",
697 | " # create new row\n",
698 | " rows[row_index] = {}\n",
699 | " \n",
700 | " # get the text value\n",
701 | " rows[row_index][col_index] = get_text(cell, blocks_map)\n",
702 | " return rows\n",
703 | "\n",
704 | "\n",
705 | "def get_text(result, blocks_map):\n",
706 | " text = ''\n",
707 | " if 'Relationships' in result:\n",
708 | " for relationship in result['Relationships']:\n",
709 | " if relationship['Type'] == 'CHILD':\n",
710 | " for child_id in relationship['Ids']:\n",
711 | " word = blocks_map[child_id]\n",
712 | " if word['BlockType'] == 'WORD':\n",
713 | " text += word['Text'] + ' '\n",
714 | " if word['BlockType'] == 'SELECTION_ELEMENT':\n",
715 | " if word['SelectionStatus'] =='SELECTED':\n",
716 | " text += 'X ' \n",
717 | " return text\n",
718 | "\n",
719 | "\n",
720 | "def get_table_csv_results(file_name):\n",
721 | "\n",
722 | " with open(file_name, 'rb') as file:\n",
723 | " img_test = file.read()\n",
724 | " bytes_test = bytearray(img_test)\n",
725 | " #print('Image loaded', file_name)\n",
726 | "\n",
727 | " # process using image bytes\n",
728 | " # get the results\n",
729 | " #client = boto3.client('textract')\n",
730 | " AWSAccessKeyId='AKICIO552JQAJCIO552JQOLNRQ'\n",
731 | " AWSSecretKey='Lxu6BXFuP0z9Lxu6BLxu6BXFuP0z9uP0UGlzXiuP0UGlzXi'\n",
732 | "\n",
733 | " client = boto3.client(\n",
734 | " 'textract',\n",
735 | " region_name='us-west-2',\n",
736 | " aws_access_key_id=AWSAccessKeyId,\n",
737 | " aws_secret_access_key=AWSSecretKey,\n",
738 | " )\n",
739 | "\n",
740 | "\n",
741 | " response = client.analyze_document(Document={'Bytes': bytes_test}, FeatureTypes=['TABLES'])\n",
742 | "\n",
743 | " # Get the text blocks\n",
744 | " blocks=response['Blocks']\n",
745 | " #pprint(blocks)\n",
746 | "\n",
747 | " blocks_map = {}\n",
748 | " table_blocks = []\n",
749 | " for block in blocks:\n",
750 | " blocks_map[block['Id']] = block\n",
751 | " if block['BlockType'] == \"TABLE\":\n",
752 | " table_blocks.append(block)\n",
753 | "\n",
754 | " if len(table_blocks) <= 0:\n",
755 | " return \" NO Table FOUND \"\n",
756 | "\n",
757 | " csv = ''\n",
758 | " for index, table in enumerate(table_blocks):\n",
759 | " csv += generate_table_csv(table, blocks_map, index +1)\n",
760 | " csv += '\\n\\n'\n",
761 | "\n",
762 | " return csv\n",
763 | "\n",
764 | "def generate_table_csv(table_result, blocks_map, table_index):\n",
765 | " rows = get_rows_columns_map(table_result, blocks_map)\n",
766 | "\n",
767 | " table_id = 'Table_' + str(table_index)\n",
768 | " \n",
769 | " # get cells.\n",
770 | " csv = 'Table: {0}\\n\\n'.format(table_id)\n",
771 | "\n",
772 | " for row_index, cols in rows.items():\n",
773 | " \n",
774 | " for col_index, text in cols.items():\n",
775 | " csv += '{}'.format(text) + \",\"\n",
776 | " csv += '\\n'\n",
777 | " \n",
778 | " csv += '\\n\\n\\n'\n",
779 | " return csv\n",
780 | "\n",
781 | "def main(file_name):\n",
782 | " global table_csv\n",
783 | " table_csv = get_table_csv_results(file_name)\n",
784 | "\n",
785 | " output_file = 'output_.csv'\n",
786 | "\n",
787 | " # replace content\n",
788 | " with open(output_file, \"wt\") as fout:\n",
789 | " fout.write(table_csv)\n",
790 | "\n",
791 | " # show the results\n",
792 | " print('CSV OUTPUT FILE: ', output_file)\n",
793 | "\n",
794 | "\n",
795 | "\n",
796 | "if __name__ == \"__main__\":\n",
797 | " file_name = sys.argv[1]\n",
798 | " \n",
799 | " main('ebcaadbc98a2af17479493e1d4fed601d496d1c142d3a9.jpeg')"
800 | ]
801 | },
802 | {
803 | "cell_type": "markdown",
804 | "metadata": {
805 | "id": "UHkKqcljl5Pq"
806 | },
807 | "source": [
808 | "## ** Common Libraries install using pip in Requirments.txt"
809 | ]
810 | },
811 | {
812 | "cell_type": "code",
813 | "execution_count": null,
814 | "metadata": {
815 | "id": "L35Kt3jCbN4h"
816 | },
817 | "outputs": [],
818 | "source": [
819 | "pdfplumber\n",
820 | "PyMuPDF\n",
821 | "pdf2image\n",
822 | "botocore\n",
823 | "Keras\n",
824 | "lxml\n",
825 | "pandas\n",
826 | "fuckit\n",
827 | "imutils\n",
828 | "wheel\n",
829 | "tabula-py\n",
830 | "Pillow\n",
831 | "numpy\n",
832 | "flask\n",
833 | "PyPDF2\n",
834 | "pytesseract\n",
835 | "filecache\n",
836 | "python-poppler\n",
837 | "opencv-python\n",
838 | "pdfminer\n",
839 | "pdfquery\n",
840 | "Flask\n",
841 | "spacy\n",
842 | "scikit-image\n",
843 | "google-cloud-vision"
844 | ]
845 | },
846 | {
847 | "cell_type": "markdown",
848 | "metadata": {
849 | "id": "qDKu84eamIM7"
850 | },
851 | "source": [
852 | "## ** Commands using termainal\n"
853 | ]
854 | },
855 | {
856 | "cell_type": "code",
857 | "execution_count": null,
858 | "metadata": {
859 | "id": "mRiAgyFtbmlF"
860 | },
861 | "outputs": [],
862 | "source": [
863 | "python3 -m spacy download en_core_web_sm\n",
864 | "sudo apt-get install python-poppler\n",
865 | "sudo apt-get install tesseract-ocr\n",
866 | "sudo apt-get install -y poppler-utils\n",
867 | "sudo apt-get install poppler-utils\n",
868 | "python3 setup.py bdist_wheel\n",
869 | "twine upload dist/*"
870 | ]
871 | }
872 | ],
873 | "metadata": {
874 | "colab": {
875 | "authorship_tag": "ABX9TyO6RNUEY/y/PuUYfuFBr6DO",
876 | "collapsed_sections": [],
877 | "include_colab_link": true,
878 | "name": "Data_extraction Python libraries.ipynb",
879 | "private_outputs": true,
880 | "provenance": []
881 | },
882 | "kernelspec": {
883 | "display_name": "Python 3",
884 | "language": "python",
885 | "name": "python3"
886 | },
887 | "language_info": {
888 | "codemirror_mode": {
889 | "name": "ipython",
890 | "version": 3
891 | },
892 | "file_extension": ".py",
893 | "mimetype": "text/x-python",
894 | "name": "python",
895 | "nbconvert_exporter": "python",
896 | "pygments_lexer": "ipython3",
897 | "version": "3.6.7"
898 | }
899 | },
900 | "nbformat": 4,
901 | "nbformat_minor": 1
902 | }
903 |
--------------------------------------------------------------------------------
/Distance calculation between two Geographic location locations.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "kernelspec": {
6 | "display_name": "Python 3",
7 | "language": "python",
8 | "name": "python3"
9 | },
10 | "language_info": {
11 | "codemirror_mode": {
12 | "name": "ipython",
13 | "version": 3
14 | },
15 | "file_extension": ".py",
16 | "mimetype": "text/x-python",
17 | "name": "python",
18 | "nbconvert_exporter": "python",
19 | "pygments_lexer": "ipython3",
20 | "version": "3.8.5"
21 | },
22 | "colab": {
23 | "name": "Distance calculation between two Geographic location locations.ipynb",
24 | "provenance": [],
25 | "collapsed_sections": [],
26 | "include_colab_link": true
27 | }
28 | },
29 | "cells": [
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "view-in-github",
34 | "colab_type": "text"
35 | },
36 | "source": [
37 | "
"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {
43 | "id": "S9KvQsEJxasn"
44 | },
45 | "source": [
46 | "# Distance calculation between two Geographic location locations"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {
52 | "id": "23j410URxasw"
53 | },
54 | "source": [
55 | "# Google API implementation"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {
61 | "id": "EtRP8yy4xasx"
62 | },
63 | "source": [
64 | "Levenshtein Algorithm\n"
65 | ]
66 | },
67 | {
68 | "cell_type": "markdown",
69 | "metadata": {
70 | "id": "D9JBccO5xasx"
71 | },
72 | "source": [
73 | "Google map API "
74 | ]
75 | },
76 | {
77 | "cell_type": "code",
78 | "metadata": {
79 | "id": "sSH7TDKsxasy",
80 | "outputId": "2443ed70-0158-4d42-ef12-db5fb51b2af6"
81 | },
82 | "source": [
83 | "api='AIzaSyBoyBoJmy4zBoJmy4zmy4z4DRfKyBoJmy4zuo'\n",
84 | "print(api)"
85 | ],
86 | "execution_count": null,
87 | "outputs": [
88 | {
89 | "output_type": "stream",
90 | "text": [
91 | "AIzaSyBoyBoJmy4zBoJmy4zmy4z4DRfKyBoJmy4zuo\n"
92 | ],
93 | "name": "stdout"
94 | }
95 | ]
96 | },
97 | {
98 | "cell_type": "markdown",
99 | "metadata": {
100 | "id": "pvMTMicIxas1"
101 | },
102 | "source": [
103 | "### Response"
104 | ]
105 | },
106 | {
107 | "cell_type": "code",
108 | "metadata": {
109 | "id": "5pkcbFKKxas1",
110 | "outputId": "054cabea-5de5-4d9c-bef7-58534b8709b9"
111 | },
112 | "source": [
113 | "import urllib.request\n",
114 | "import json\n",
115 | "def google_api():\n",
116 | "\n",
117 | " url='https://maps.googleapis.com/maps/api/directions/json?'\n",
118 | " api='AIzaSyBoyBoJmy4zBoJmy4zmy4z4DRfKyBoJmy4zuo'\n",
119 | " \n",
120 | " # inputs get\n",
121 | " \n",
122 | " origin=input(\"\\n \\n '{Enter Start Location}' :- \").replace(' ','+')\n",
123 | " destination=input(\"\\n \\n '{Enter End Location}' :- \").replace(' ','+')\n",
124 | " \n",
125 | " # sending to api \n",
126 | " \n",
127 | " nav_request='origin={}&destination={}&key={}'.format(origin,destination,api)\n",
128 | " request=url+nav_request\n",
129 | " \n",
130 | " #getting data\n",
131 | " \n",
132 | " response=urllib.request.urlopen(request).read().decode('utf-8')\n",
133 | " data=json.loads(response)\n",
134 | " return data\n",
135 | "\n",
136 | "# run function\n",
137 | "data=google_api()"
138 | ],
139 | "execution_count": null,
140 | "outputs": [
141 | {
142 | "output_type": "stream",
143 | "text": [
144 | "\n",
145 | " \n",
146 | " '{Enter Start Location}' :- Delhi Rohini\n",
147 | "\n",
148 | " \n",
149 | " '{Enter End Location}' :- Mumbai \n"
150 | ],
151 | "name": "stdout"
152 | }
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {
158 | "id": "pLtMXAckxas2"
159 | },
160 | "source": [
161 | "### All Keys in Data"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "metadata": {
167 | "id": "t7aOHiElxas3",
168 | "outputId": "0c5adb5d-8ad8-40b0-a8bc-89af1ffd7e41"
169 | },
170 | "source": [
171 | "keys=(list(data['routes'][0]['legs'][0].keys()))\n",
172 | "keys"
173 | ],
174 | "execution_count": null,
175 | "outputs": [
176 | {
177 | "output_type": "execute_result",
178 | "data": {
179 | "text/plain": [
180 | "['distance',\n",
181 | " 'duration',\n",
182 | " 'end_address',\n",
183 | " 'end_location',\n",
184 | " 'start_address',\n",
185 | " 'start_location',\n",
186 | " 'steps',\n",
187 | " 'traffic_speed_entry',\n",
188 | " 'via_waypoint']"
189 | ]
190 | },
191 | "metadata": {
192 | "tags": []
193 | },
194 | "execution_count": 3
195 | }
196 | ]
197 | },
198 | {
199 | "cell_type": "markdown",
200 | "metadata": {
201 | "id": "f8yWPNhIxas4"
202 | },
203 | "source": [
204 | "#### Getting All Detais"
205 | ]
206 | },
207 | {
208 | "cell_type": "code",
209 | "metadata": {
210 | "id": "CuYMIGojxas5",
211 | "outputId": "899098ee-5ed9-4593-8bc1-56d54dff4139"
212 | },
213 | "source": [
214 | "### Calcunating Distance\n",
215 | "\n",
216 | "distance=data['routes'][0]['legs'][0]['distance'].get(\"text\")\n",
217 | "print(\"\\n \\n '{Distance}' :- \",distance,\"\\n\")\n",
218 | "\n",
219 | "\n",
220 | "### calculating Time\n",
221 | "time_foot=data['routes'][0]['legs'][0]['duration'].get(\"text\")\n",
222 | "print(\"'{Time Taken}' :- \",time_foot,\"\\n\")\n",
223 | "\n",
224 | "### End address\n",
225 | "\n",
226 | "end_address=data['routes'][0]['legs'][0]['end_address']\n",
227 | "print(\"'{End Address}' :- \",end_address,\"\\n\")\n",
228 | "\n",
229 | "### Start address\n",
230 | "\n",
231 | "start_address=data['routes'][0]['legs'][0]['start_address']\n",
232 | "print(\"'{Start Address}' :- \",start_address,\"\\n \\n \")"
233 | ],
234 | "execution_count": null,
235 | "outputs": [
236 | {
237 | "output_type": "stream",
238 | "text": [
239 | "\n",
240 | " \n",
241 | " '{Distance}' :- 1,419 km \n",
242 | "\n",
243 | "'{Time Taken}' :- 1 day 1 hour \n",
244 | "\n",
245 | "'{End Address}' :- Mumbai, Maharashtra, India \n",
246 | "\n",
247 | "'{Start Address}' :- Rohini, New Delhi, Delhi, India \n",
248 | " \n",
249 | " \n"
250 | ],
251 | "name": "stdout"
252 | }
253 | ]
254 | },
255 | {
256 | "cell_type": "markdown",
257 | "metadata": {
258 | "id": "f6AofH9Cxas5"
259 | },
260 | "source": [
261 | "#### Getting Directions"
262 | ]
263 | },
264 | {
265 | "cell_type": "code",
266 | "metadata": {
267 | "id": "Yi9ZnhFGxas6",
268 | "outputId": "c2d73a73-5900-4042-a7fb-976c866b575f"
269 | },
270 | "source": [
271 | "def get_directions(data):\n",
272 | " for i in range (len(data['routes'][0]['legs'][0]['steps'])):\n",
273 | " clean=data['routes'][0]['legs'][0]['steps'][i]\n",
274 | " clean.pop(\"polyline\")\n",
275 | " clean.pop(\"html_instructions\")\n",
276 | " print(\" -- Step -- \", {i})\n",
277 | " print(\"\\n\",clean,'\\n \\n ')\n",
278 | " \n",
279 | "## call function\n",
280 | "#data=google_api()\n",
281 | "get_directions(data)"
282 | ],
283 | "execution_count": null,
284 | "outputs": [
285 | {
286 | "output_type": "stream",
287 | "text": [
288 | " -- Step -- {0}\n",
289 | "\n",
290 | " {'distance': {'text': '6.1 km', 'value': 6130}, 'duration': {'text': '16 mins', 'value': 956}, 'end_location': {'lat': 28.7042711, 'lng': 77.1308658}, 'start_location': {'lat': 28.7382715, 'lng': 77.0822278}, 'travel_mode': 'DRIVING'} \n",
291 | " \n",
292 | " \n",
293 | " -- Step -- {1}\n",
294 | "\n",
295 | " {'distance': {'text': '24 m', 'value': 24}, 'duration': {'text': '1 min', 'value': 15}, 'end_location': {'lat': 28.7041454, 'lng': 77.1310649}, 'maneuver': 'straight', 'start_location': {'lat': 28.7042711, 'lng': 77.1308658}, 'travel_mode': 'DRIVING'} \n",
296 | " \n",
297 | " \n",
298 | " -- Step -- {2}\n",
299 | "\n",
300 | " {'distance': {'text': '2.3 km', 'value': 2264}, 'duration': {'text': '7 mins', 'value': 399}, 'end_location': {'lat': 28.6940172, 'lng': 77.1509913}, 'maneuver': 'straight', 'start_location': {'lat': 28.7041454, 'lng': 77.1310649}, 'travel_mode': 'DRIVING'} \n",
301 | " \n",
302 | " \n",
303 | " -- Step -- {3}\n",
304 | "\n",
305 | " {'distance': {'text': '3.1 km', 'value': 3101}, 'duration': {'text': '5 mins', 'value': 295}, 'end_location': {'lat': 28.6768915, 'lng': 77.141162}, 'maneuver': 'ramp-right', 'start_location': {'lat': 28.6940172, 'lng': 77.1509913}, 'travel_mode': 'DRIVING'} \n",
306 | " \n",
307 | " \n",
308 | " -- Step -- {4}\n",
309 | "\n",
310 | " {'distance': {'text': '0.9 km', 'value': 860}, 'duration': {'text': '1 min', 'value': 68}, 'end_location': {'lat': 28.6696612, 'lng': 77.13814599999999}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.6768915, 'lng': 77.141162}, 'travel_mode': 'DRIVING'} \n",
311 | " \n",
312 | " \n",
313 | " -- Step -- {5}\n",
314 | "\n",
315 | " {'distance': {'text': '0.7 km', 'value': 709}, 'duration': {'text': '1 min', 'value': 62}, 'end_location': {'lat': 28.6651601, 'lng': 77.1330061}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.6696612, 'lng': 77.13814599999999}, 'travel_mode': 'DRIVING'} \n",
316 | " \n",
317 | " \n",
318 | " -- Step -- {6}\n",
319 | "\n",
320 | " {'distance': {'text': '0.4 km', 'value': 380}, 'duration': {'text': '1 min', 'value': 49}, 'end_location': {'lat': 28.6627528, 'lng': 77.13025130000001}, 'maneuver': 'keep-left', 'start_location': {'lat': 28.6651601, 'lng': 77.1330061}, 'travel_mode': 'DRIVING'} \n",
321 | " \n",
322 | " \n",
323 | " -- Step -- {7}\n",
324 | "\n",
325 | " {'distance': {'text': '53 m', 'value': 53}, 'duration': {'text': '1 min', 'value': 6}, 'end_location': {'lat': 28.6624228, 'lng': 77.1298534}, 'start_location': {'lat': 28.6627528, 'lng': 77.13025130000001}, 'travel_mode': 'DRIVING'} \n",
326 | " \n",
327 | " \n",
328 | " -- Step -- {8}\n",
329 | "\n",
330 | " {'distance': {'text': '2.7 km', 'value': 2682}, 'duration': {'text': '4 mins', 'value': 262}, 'end_location': {'lat': 28.6405647, 'lng': 77.1289359}, 'start_location': {'lat': 28.6624228, 'lng': 77.1298534}, 'travel_mode': 'DRIVING'} \n",
331 | " \n",
332 | " \n",
333 | " -- Step -- {9}\n",
334 | "\n",
335 | " {'distance': {'text': '7.0 km', 'value': 6950}, 'duration': {'text': '10 mins', 'value': 586}, 'end_location': {'lat': 28.5943381, 'lng': 77.16659279999999}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.6405647, 'lng': 77.1289359}, 'travel_mode': 'DRIVING'} \n",
336 | " \n",
337 | " \n",
338 | " -- Step -- {10}\n",
339 | "\n",
340 | " {'distance': {'text': '0.3 km', 'value': 281}, 'duration': {'text': '1 min', 'value': 30}, 'end_location': {'lat': 28.5928058, 'lng': 77.16834}, 'maneuver': 'ramp-left', 'start_location': {'lat': 28.5943381, 'lng': 77.16659279999999}, 'travel_mode': 'DRIVING'} \n",
341 | " \n",
342 | " \n",
343 | " -- Step -- {11}\n",
344 | "\n",
345 | " {'distance': {'text': '0.2 km', 'value': 219}, 'duration': {'text': '1 min', 'value': 24}, 'end_location': {'lat': 28.5932142, 'lng': 77.1668218}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.5928058, 'lng': 77.16834}, 'travel_mode': 'DRIVING'} \n",
346 | " \n",
347 | " \n",
348 | " -- Step -- {12}\n",
349 | "\n",
350 | " {'distance': {'text': '0.2 km', 'value': 204}, 'duration': {'text': '1 min', 'value': 24}, 'end_location': {'lat': 28.5937079, 'lng': 77.1651352}, 'maneuver': 'turn-slight-left', 'start_location': {'lat': 28.5932142, 'lng': 77.1668218}, 'travel_mode': 'DRIVING'} \n",
351 | " \n",
352 | " \n",
353 | " -- Step -- {13}\n",
354 | "\n",
355 | " {'distance': {'text': '1.1 km', 'value': 1099}, 'duration': {'text': '2 mins', 'value': 100}, 'end_location': {'lat': 28.5895361, 'lng': 77.1549393}, 'maneuver': 'merge', 'start_location': {'lat': 28.5937079, 'lng': 77.1651352}, 'travel_mode': 'DRIVING'} \n",
356 | " \n",
357 | " \n",
358 | " -- Step -- {14}\n",
359 | "\n",
360 | " {'distance': {'text': '1.7 km', 'value': 1682}, 'duration': {'text': '2 mins', 'value': 128}, 'end_location': {'lat': 28.5788521, 'lng': 77.1447658}, 'maneuver': 'straight', 'start_location': {'lat': 28.5895361, 'lng': 77.1549393}, 'travel_mode': 'DRIVING'} \n",
361 | " \n",
362 | " \n",
363 | " -- Step -- {15}\n",
364 | "\n",
365 | " {'distance': {'text': '1.3 km', 'value': 1262}, 'duration': {'text': '2 mins', 'value': 91}, 'end_location': {'lat': 28.5714225, 'lng': 77.1358619}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.5788521, 'lng': 77.1447658}, 'travel_mode': 'DRIVING'} \n",
366 | " \n",
367 | " \n",
368 | " -- Step -- {16}\n",
369 | "\n",
370 | " {'distance': {'text': '3.8 km', 'value': 3828}, 'duration': {'text': '4 mins', 'value': 240}, 'end_location': {'lat': 28.5431002, 'lng': 77.1185441}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.5714225, 'lng': 77.1358619}, 'travel_mode': 'DRIVING'} \n",
371 | " \n",
372 | " \n",
373 | " -- Step -- {17}\n",
374 | "\n",
375 | " {'distance': {'text': '4.4 km', 'value': 4404}, 'duration': {'text': '5 mins', 'value': 276}, 'end_location': {'lat': 28.5119964, 'lng': 77.0935233}, 'maneuver': 'keep-left', 'start_location': {'lat': 28.5431002, 'lng': 77.1185441}, 'travel_mode': 'DRIVING'} \n",
376 | " \n",
377 | " \n",
378 | " -- Step -- {18}\n",
379 | "\n",
380 | " {'distance': {'text': '34.0 km', 'value': 33959}, 'duration': {'text': '37 mins', 'value': 2232}, 'end_location': {'lat': 28.3042192, 'lng': 76.8766566}, 'maneuver': 'keep-right', 'start_location': {'lat': 28.5119964, 'lng': 77.0935233}, 'travel_mode': 'DRIVING'} \n",
381 | " \n",
382 | " \n",
383 | " -- Step -- {19}\n",
384 | "\n",
385 | " {'distance': {'text': '18.9 km', 'value': 18899}, 'duration': {'text': '21 mins', 'value': 1230}, 'end_location': {'lat': 28.1985818, 'lng': 76.7436515}, 'maneuver': 'straight', 'start_location': {'lat': 28.3042192, 'lng': 76.8766566}, 'travel_mode': 'DRIVING'} \n",
386 | " \n",
387 | " \n",
388 | " -- Step -- {20}\n",
389 | "\n",
390 | " {'distance': {'text': '100 km', 'value': 100336}, 'duration': {'text': '1 hour 32 mins', 'value': 5502}, 'end_location': {'lat': 27.5663436, 'lng': 76.07372889999999}, 'maneuver': 'keep-left', 'start_location': {'lat': 28.1985818, 'lng': 76.7436515}, 'travel_mode': 'DRIVING'} \n",
391 | " \n",
392 | " \n",
393 | " -- Step -- {21}\n",
394 | "\n",
395 | " {'distance': {'text': '2.3 km', 'value': 2260}, 'duration': {'text': '2 mins', 'value': 119}, 'end_location': {'lat': 27.5464583, 'lng': 76.07293709999999}, 'maneuver': 'keep-left', 'start_location': {'lat': 27.5663436, 'lng': 76.07372889999999}, 'travel_mode': 'DRIVING'} \n",
396 | " \n",
397 | " \n",
398 | " -- Step -- {22}\n",
399 | "\n",
400 | " {'distance': {'text': '79 m', 'value': 79}, 'duration': {'text': '1 min', 'value': 4}, 'end_location': {'lat': 27.545796, 'lng': 76.0726456}, 'maneuver': 'keep-left', 'start_location': {'lat': 27.5464583, 'lng': 76.07293709999999}, 'travel_mode': 'DRIVING'} \n",
401 | " \n",
402 | " \n",
403 | " -- Step -- {23}\n",
404 | "\n",
405 | " {'distance': {'text': '40.2 km', 'value': 40220}, 'duration': {'text': '38 mins', 'value': 2269}, 'end_location': {'lat': 27.2342851, 'lng': 75.9487588}, 'maneuver': 'keep-left', 'start_location': {'lat': 27.545796, 'lng': 76.0726456}, 'travel_mode': 'DRIVING'} \n",
406 | " \n",
407 | " \n",
408 | " -- Step -- {24}\n",
409 | "\n",
410 | " {'distance': {'text': '53.1 km', 'value': 53051}, 'duration': {'text': '51 mins', 'value': 3051}, 'end_location': {'lat': 26.8702523, 'lng': 75.69467759999999}, 'maneuver': 'keep-left', 'start_location': {'lat': 27.2342851, 'lng': 75.9487588}, 'travel_mode': 'DRIVING'} \n",
411 | " \n",
412 | " \n",
413 | " -- Step -- {25}\n",
414 | "\n",
415 | " {'distance': {'text': '72.3 km', 'value': 72348}, 'duration': {'text': '1 hour 5 mins', 'value': 3910}, 'end_location': {'lat': 26.6119952, 'lng': 75.0374154}, 'maneuver': 'keep-left', 'start_location': {'lat': 26.8702523, 'lng': 75.69467759999999}, 'travel_mode': 'DRIVING'} \n",
416 | " \n",
417 | " \n",
418 | " -- Step -- {26}\n",
419 | "\n",
420 | " {'distance': {'text': '13.2 km', 'value': 13227}, 'duration': {'text': '13 mins', 'value': 789}, 'end_location': {'lat': 26.5900183, 'lng': 74.9073998}, 'maneuver': 'keep-left', 'start_location': {'lat': 26.6119952, 'lng': 75.0374154}, 'travel_mode': 'DRIVING'} \n",
421 | " \n",
422 | " \n",
423 | " -- Step -- {27}\n",
424 | "\n",
425 | " {'distance': {'text': '5.0 km', 'value': 5001}, 'duration': {'text': '6 mins', 'value': 338}, 'end_location': {'lat': 26.601224, 'lng': 74.86272400000001}, 'start_location': {'lat': 26.5900183, 'lng': 74.9073998}, 'travel_mode': 'DRIVING'} \n",
426 | " \n",
427 | " \n",
428 | " -- Step -- {28}\n",
429 | "\n",
430 | " {'distance': {'text': '31.3 km', 'value': 31309}, 'duration': {'text': '28 mins', 'value': 1708}, 'end_location': {'lat': 26.417781, 'lng': 74.6710507}, 'maneuver': 'keep-right', 'start_location': {'lat': 26.601224, 'lng': 74.86272400000001}, 'travel_mode': 'DRIVING'} \n",
431 | " \n",
432 | " \n",
433 | " -- Step -- {29}\n",
434 | "\n",
435 | " {'distance': {'text': '13.7 km', 'value': 13669}, 'duration': {'text': '12 mins', 'value': 697}, 'end_location': {'lat': 26.3370585, 'lng': 74.5738909}, 'start_location': {'lat': 26.417781, 'lng': 74.6710507}, 'travel_mode': 'DRIVING'} \n",
436 | " \n",
437 | " \n",
438 | " -- Step -- {30}\n",
439 | "\n",
440 | " {'distance': {'text': '1.2 km', 'value': 1176}, 'duration': {'text': '2 mins', 'value': 96}, 'end_location': {'lat': 26.3315463, 'lng': 74.5639501}, 'start_location': {'lat': 26.3370585, 'lng': 74.5738909}, 'travel_mode': 'DRIVING'} \n",
441 | " \n",
442 | " \n",
443 | " -- Step -- {31}\n",
444 | "\n",
445 | " {'distance': {'text': '46.7 km', 'value': 46654}, 'duration': {'text': '43 mins', 'value': 2598}, 'end_location': {'lat': 26.0309542, 'lng': 74.2817055}, 'maneuver': 'merge', 'start_location': {'lat': 26.3315463, 'lng': 74.5639501}, 'travel_mode': 'DRIVING'} \n",
446 | " \n",
447 | " \n",
448 | " -- Step -- {32}\n",
449 | "\n",
450 | " {'distance': {'text': '190 km', 'value': 190391}, 'duration': {'text': '3 hours 15 mins', 'value': 11698}, 'end_location': {'lat': 24.6576235, 'lng': 73.7211052}, 'maneuver': 'straight', 'start_location': {'lat': 26.0309542, 'lng': 74.2817055}, 'travel_mode': 'DRIVING'} \n",
451 | " \n",
452 | " \n",
453 | " -- Step -- {33}\n",
454 | "\n",
455 | " {'distance': {'text': '3.6 km', 'value': 3597}, 'duration': {'text': '5 mins', 'value': 273}, 'end_location': {'lat': 24.6265989, 'lng': 73.7116375}, 'start_location': {'lat': 24.6576235, 'lng': 73.7211052}, 'travel_mode': 'DRIVING'} \n",
456 | " \n",
457 | " \n",
458 | " -- Step -- {34}\n",
459 | "\n",
460 | " {'distance': {'text': '0.8 km', 'value': 752}, 'duration': {'text': '2 mins', 'value': 90}, 'end_location': {'lat': 24.6227157, 'lng': 73.717726}, 'maneuver': 'roundabout-left', 'start_location': {'lat': 24.6265989, 'lng': 73.7116375}, 'travel_mode': 'DRIVING'} \n",
461 | " \n",
462 | " \n",
463 | " -- Step -- {35}\n",
464 | "\n",
465 | " {'distance': {'text': '2.0 km', 'value': 1984}, 'duration': {'text': '3 mins', 'value': 187}, 'end_location': {'lat': 24.6076043, 'lng': 73.7090569}, 'maneuver': 'turn-right', 'start_location': {'lat': 24.6227157, 'lng': 73.717726}, 'travel_mode': 'DRIVING'} \n",
466 | " \n",
467 | " \n",
468 | " -- Step -- {36}\n",
469 | "\n",
470 | " {'distance': {'text': '2.3 km', 'value': 2288}, 'duration': {'text': '5 mins', 'value': 279}, 'end_location': {'lat': 24.5886647, 'lng': 73.7136747}, 'maneuver': 'roundabout-left', 'start_location': {'lat': 24.6076043, 'lng': 73.7090569}, 'travel_mode': 'DRIVING'} \n",
471 | " \n",
472 | " \n",
473 | " -- Step -- {37}\n",
474 | "\n",
475 | " {'distance': {'text': '0.5 km', 'value': 518}, 'duration': {'text': '1 min', 'value': 82}, 'end_location': {'lat': 24.5864906, 'lng': 73.7094138}, 'maneuver': 'turn-right', 'start_location': {'lat': 24.5886647, 'lng': 73.7136747}, 'travel_mode': 'DRIVING'} \n",
476 | " \n",
477 | " \n",
478 | " -- Step -- {38}\n",
479 | "\n",
480 | " {'distance': {'text': '0.8 km', 'value': 768}, 'duration': {'text': '2 mins', 'value': 96}, 'end_location': {'lat': 24.5796035, 'lng': 73.7095643}, 'maneuver': 'turn-left', 'start_location': {'lat': 24.5864906, 'lng': 73.7094138}, 'travel_mode': 'DRIVING'} \n",
481 | " \n",
482 | " \n",
483 | " -- Step -- {39}\n",
484 | "\n",
485 | " {'distance': {'text': '1.1 km', 'value': 1052}, 'duration': {'text': '2 mins', 'value': 118}, 'end_location': {'lat': 24.579413, 'lng': 73.6994572}, 'maneuver': 'roundabout-left', 'start_location': {'lat': 24.5796035, 'lng': 73.7095643}, 'travel_mode': 'DRIVING'} \n",
486 | " \n",
487 | " \n",
488 | " -- Step -- {40}\n",
489 | "\n",
490 | " {'distance': {'text': '0.2 km', 'value': 210}, 'duration': {'text': '1 min', 'value': 45}, 'end_location': {'lat': 24.5775635, 'lng': 73.6991395}, 'maneuver': 'turn-left', 'start_location': {'lat': 24.579413, 'lng': 73.6994572}, 'travel_mode': 'DRIVING'} \n",
491 | " \n",
492 | " \n",
493 | " -- Step -- {41}\n",
494 | "\n",
495 | " {'distance': {'text': '0.2 km', 'value': 174}, 'duration': {'text': '1 min', 'value': 34}, 'end_location': {'lat': 24.5762797, 'lng': 73.7000681}, 'maneuver': 'turn-left', 'start_location': {'lat': 24.5775635, 'lng': 73.6991395}, 'travel_mode': 'DRIVING'} \n",
496 | " \n",
497 | " \n",
498 | " -- Step -- {42}\n",
499 | "\n",
500 | " {'distance': {'text': '2.1 km', 'value': 2139}, 'duration': {'text': '4 mins', 'value': 227}, 'end_location': {'lat': 24.5601893, 'lng': 73.6929832}, 'maneuver': 'roundabout-left', 'start_location': {'lat': 24.5762797, 'lng': 73.7000681}, 'travel_mode': 'DRIVING'} \n",
501 | " \n",
502 | " \n",
503 | " -- Step -- {43}\n",
504 | "\n",
505 | " {'distance': {'text': '1.7 km', 'value': 1731}, 'duration': {'text': '3 mins', 'value': 166}, 'end_location': {'lat': 24.5458193, 'lng': 73.6870393}, 'maneuver': 'straight', 'start_location': {'lat': 24.5601893, 'lng': 73.6929832}, 'travel_mode': 'DRIVING'} \n",
506 | " \n",
507 | " \n",
508 | " -- Step -- {44}\n",
509 | "\n",
510 | " {'distance': {'text': '2.6 km', 'value': 2605}, 'duration': {'text': '4 mins', 'value': 245}, 'end_location': {'lat': 24.5245304, 'lng': 73.6770818}, 'start_location': {'lat': 24.5458193, 'lng': 73.6870393}, 'travel_mode': 'DRIVING'} \n",
511 | " \n",
512 | " \n",
513 | " -- Step -- {45}\n",
514 | "\n",
515 | " {'distance': {'text': '3.8 km', 'value': 3776}, 'duration': {'text': '5 mins', 'value': 278}, 'end_location': {'lat': 24.4979953, 'lng': 73.6553964}, 'maneuver': 'turn-right', 'start_location': {'lat': 24.5245304, 'lng': 73.6770818}, 'travel_mode': 'DRIVING'} \n",
516 | " \n",
517 | " \n",
518 | " -- Step -- {46}\n",
519 | "\n",
520 | " {'distance': {'text': '54.1 km', 'value': 54059}, 'duration': {'text': '53 mins', 'value': 3151}, 'end_location': {'lat': 24.0742644, 'lng': 73.6840017}, 'maneuver': 'straight', 'start_location': {'lat': 24.4979953, 'lng': 73.6553964}, 'travel_mode': 'DRIVING'} \n",
521 | " \n",
522 | " \n",
523 | " -- Step -- {47}\n",
524 | "\n",
525 | " {'distance': {'text': '62.3 km', 'value': 62318}, 'duration': {'text': '1 hour 10 mins', 'value': 4216}, 'end_location': {'lat': 23.6885068, 'lng': 73.3835485}, 'maneuver': 'straight', 'start_location': {'lat': 24.0742644, 'lng': 73.6840017}, 'travel_mode': 'DRIVING'} \n",
526 | " \n",
527 | " \n",
528 | " -- Step -- {48}\n",
529 | "\n",
530 | " {'distance': {'text': '0.2 km', 'value': 170}, 'duration': {'text': '1 min', 'value': 45}, 'end_location': {'lat': 23.6870915, 'lng': 73.38291889999999}, 'maneuver': 'turn-left', 'start_location': {'lat': 23.6885068, 'lng': 73.3835485}, 'travel_mode': 'DRIVING'} \n",
531 | " \n",
532 | " \n",
533 | " -- Step -- {49}\n",
534 | "\n",
535 | " {'distance': {'text': '2.5 km', 'value': 2514}, 'duration': {'text': '5 mins', 'value': 295}, 'end_location': {'lat': 23.6710895, 'lng': 73.3709945}, 'maneuver': 'turn-right', 'start_location': {'lat': 23.6870915, 'lng': 73.38291889999999}, 'travel_mode': 'DRIVING'} \n",
536 | " \n",
537 | " \n",
538 | " -- Step -- {50}\n",
539 | "\n",
540 | " {'distance': {'text': '23.2 km', 'value': 23161}, 'duration': {'text': '23 mins', 'value': 1404}, 'end_location': {'lat': 23.4873765, 'lng': 73.2988909}, 'maneuver': 'turn-left', 'start_location': {'lat': 23.6710895, 'lng': 73.3709945}, 'travel_mode': 'DRIVING'} \n",
541 | " \n",
542 | " \n",
543 | " -- Step -- {51}\n",
544 | "\n",
545 | " {'distance': {'text': '4.3 km', 'value': 4305}, 'duration': {'text': '5 mins', 'value': 318}, 'end_location': {'lat': 23.4532964, 'lng': 73.3157461}, 'maneuver': 'turn-left', 'start_location': {'lat': 23.4873765, 'lng': 73.2988909}, 'travel_mode': 'DRIVING'} \n",
546 | " \n",
547 | " \n",
548 | " -- Step -- {52}\n",
549 | "\n",
550 | " {'distance': {'text': '91.4 km', 'value': 91439}, 'duration': {'text': '1 hour 26 mins', 'value': 5137}, 'end_location': {'lat': 22.8189373, 'lng': 73.6217061}, 'maneuver': 'turn-left', 'start_location': {'lat': 23.4532964, 'lng': 73.3157461}, 'travel_mode': 'DRIVING'} \n",
551 | " \n",
552 | " \n",
553 | " -- Step -- {53}\n",
554 | "\n",
555 | " {'distance': {'text': '0.5 km', 'value': 492}, 'duration': {'text': '1 min', 'value': 42}, 'end_location': {'lat': 22.8170747, 'lng': 73.6256878}, 'maneuver': 'turn-left', 'start_location': {'lat': 22.8189373, 'lng': 73.6217061}, 'travel_mode': 'DRIVING'} \n",
556 | " \n",
557 | " \n",
558 | " -- Step -- {54}\n",
559 | "\n",
560 | " {'distance': {'text': '6.2 km', 'value': 6160}, 'duration': {'text': '6 mins', 'value': 354}, 'end_location': {'lat': 22.7879818, 'lng': 73.6724156}, 'start_location': {'lat': 22.8170747, 'lng': 73.6256878}, 'travel_mode': 'DRIVING'} \n",
561 | " \n",
562 | " \n",
563 | " -- Step -- {55}\n",
564 | "\n",
565 | " {'distance': {'text': '10.5 km', 'value': 10499}, 'duration': {'text': '10 mins', 'value': 585}, 'end_location': {'lat': 22.7374606, 'lng': 73.5953147}, 'maneuver': 'straight', 'start_location': {'lat': 22.7879818, 'lng': 73.6724156}, 'travel_mode': 'DRIVING'} \n",
566 | " \n",
567 | " \n",
568 | " -- Step -- {56}\n",
569 | "\n",
570 | " {'distance': {'text': '26.9 km', 'value': 26928}, 'duration': {'text': '32 mins', 'value': 1905}, 'end_location': {'lat': 22.5563416, 'lng': 73.4576692}, 'maneuver': 'turn-left', 'start_location': {'lat': 22.7374606, 'lng': 73.5953147}, 'travel_mode': 'DRIVING'} \n",
571 | " \n",
572 | " \n",
573 | " -- Step -- {57}\n",
574 | "\n",
575 | " {'distance': {'text': '7.8 km', 'value': 7805}, 'duration': {'text': '8 mins', 'value': 471}, 'end_location': {'lat': 22.4920426, 'lng': 73.4676303}, 'start_location': {'lat': 22.5563416, 'lng': 73.4576692}, 'travel_mode': 'DRIVING'} \n",
576 | " \n",
577 | " \n",
578 | " -- Step -- {58}\n",
579 | "\n",
580 | " {'distance': {'text': '31.4 km', 'value': 31399}, 'duration': {'text': '31 mins', 'value': 1868}, 'end_location': {'lat': 22.3513705, 'lng': 73.2319122}, 'maneuver': 'roundabout-left', 'start_location': {'lat': 22.4920426, 'lng': 73.4676303}, 'travel_mode': 'DRIVING'} \n",
581 | " \n",
582 | " \n",
583 | " -- Step -- {59}\n",
584 | "\n",
585 | " {'distance': {'text': '0.5 km', 'value': 464}, 'duration': {'text': '1 min', 'value': 62}, 'end_location': {'lat': 22.34974, 'lng': 73.2357542}, 'maneuver': 'turn-sharp-left', 'start_location': {'lat': 22.3513705, 'lng': 73.2319122}, 'travel_mode': 'DRIVING'} \n",
586 | " \n",
587 | " \n",
588 | " -- Step -- {60}\n",
589 | "\n",
590 | " {'distance': {'text': '14.5 km', 'value': 14517}, 'duration': {'text': '14 mins', 'value': 834}, 'end_location': {'lat': 22.2391844, 'lng': 73.21020349999999}, 'start_location': {'lat': 22.34974, 'lng': 73.2357542}, 'travel_mode': 'DRIVING'} \n",
591 | " \n",
592 | " \n",
593 | " -- Step -- {61}\n",
594 | "\n",
595 | " {'distance': {'text': '65.6 km', 'value': 65636}, 'duration': {'text': '1 hour 0 mins', 'value': 3598}, 'end_location': {'lat': 21.7202569, 'lng': 73.0395315}, 'maneuver': 'straight', 'start_location': {'lat': 22.2391844, 'lng': 73.21020349999999}, 'travel_mode': 'DRIVING'} \n",
596 | " \n",
597 | " \n",
598 | " -- Step -- {62}\n",
599 | "\n",
600 | " {'distance': {'text': '0.1 km', 'value': 100}, 'duration': {'text': '1 min', 'value': 9}, 'end_location': {'lat': 21.7198171, 'lng': 73.04036090000001}, 'maneuver': 'turn-slight-left', 'start_location': {'lat': 21.7202569, 'lng': 73.0395315}, 'travel_mode': 'DRIVING'} \n",
601 | " \n",
602 | " \n",
603 | " -- Step -- {63}\n",
604 | "\n",
605 | " {'distance': {'text': '2.5 km', 'value': 2493}, 'duration': {'text': '3 mins', 'value': 208}, 'end_location': {'lat': 21.70465, 'lng': 73.0581151}, 'maneuver': 'turn-slight-right', 'start_location': {'lat': 21.7198171, 'lng': 73.04036090000001}, 'travel_mode': 'DRIVING'} \n",
606 | " \n",
607 | " \n",
608 | " -- Step -- {64}\n",
609 | "\n",
610 | " {'distance': {'text': '118 km', 'value': 117896}, 'duration': {'text': '2 hours 5 mins', 'value': 7482}, 'end_location': {'lat': 20.7633323, 'lng': 73.05775120000001}, 'maneuver': 'turn-slight-left', 'start_location': {'lat': 21.70465, 'lng': 73.0581151}, 'travel_mode': 'DRIVING'} \n",
611 | " \n",
612 | " \n",
613 | " -- Step -- {65}\n",
614 | "\n",
615 | " {'distance': {'text': '163 km', 'value': 163093}, 'duration': {'text': '2 hours 35 mins', 'value': 9312}, 'end_location': {'lat': 19.4698091, 'lng': 72.8906058}, 'maneuver': 'keep-right', 'start_location': {'lat': 20.7633323, 'lng': 73.05775120000001}, 'travel_mode': 'DRIVING'} \n",
616 | " \n",
617 | " \n",
618 | " -- Step -- {66}\n",
619 | "\n",
620 | " {'distance': {'text': '0.3 km', 'value': 321}, 'duration': {'text': '1 min', 'value': 20}, 'end_location': {'lat': 19.4677972, 'lng': 72.8884139}, 'maneuver': 'ramp-right', 'start_location': {'lat': 19.4698091, 'lng': 72.8906058}, 'travel_mode': 'DRIVING'} \n",
621 | " \n",
622 | " \n",
623 | " -- Step -- {67}\n",
624 | "\n",
625 | " {'distance': {'text': '22.4 km', 'value': 22399}, 'duration': {'text': '29 mins', 'value': 1764}, 'end_location': {'lat': 19.2853277, 'lng': 72.90446}, 'start_location': {'lat': 19.4677972, 'lng': 72.8884139}, 'travel_mode': 'DRIVING'} \n",
626 | " \n",
627 | " \n",
628 | " -- Step -- {68}\n",
629 | "\n",
630 | " {'distance': {'text': '13.2 km', 'value': 13182}, 'duration': {'text': '22 mins', 'value': 1304}, 'end_location': {'lat': 19.1931496, 'lng': 72.859518}, 'maneuver': 'straight', 'start_location': {'lat': 19.2853277, 'lng': 72.90446}, 'travel_mode': 'DRIVING'} \n",
631 | " \n",
632 | " \n",
633 | " -- Step -- {69}\n",
634 | "\n",
635 | " {'distance': {'text': '5.0 km', 'value': 4990}, 'duration': {'text': '7 mins', 'value': 433}, 'end_location': {'lat': 19.1488141, 'lng': 72.8563939}, 'maneuver': 'keep-right', 'start_location': {'lat': 19.1931496, 'lng': 72.859518}, 'travel_mode': 'DRIVING'} \n",
636 | " \n",
637 | " \n",
638 | " -- Step -- {70}\n",
639 | "\n",
640 | " {'distance': {'text': '2.5 km', 'value': 2488}, 'duration': {'text': '4 mins', 'value': 264}, 'end_location': {'lat': 19.1266428, 'lng': 72.85559409999999}, 'maneuver': 'keep-right', 'start_location': {'lat': 19.1488141, 'lng': 72.8563939}, 'travel_mode': 'DRIVING'} \n",
641 | " \n",
642 | " \n",
643 | " -- Step -- {71}\n",
644 | "\n",
645 | " {'distance': {'text': '0.8 km', 'value': 798}, 'duration': {'text': '1 min', 'value': 68}, 'end_location': {'lat': 19.1197693, 'lng': 72.85578509999999}, 'maneuver': 'keep-right', 'start_location': {'lat': 19.1266428, 'lng': 72.85559409999999}, 'travel_mode': 'DRIVING'} \n",
646 | " \n",
647 | " \n",
648 | " -- Step -- {72}\n",
649 | "\n",
650 | " {'distance': {'text': '2.3 km', 'value': 2346}, 'duration': {'text': '3 mins', 'value': 180}, 'end_location': {'lat': 19.0991497, 'lng': 72.8540131}, 'maneuver': 'keep-right', 'start_location': {'lat': 19.1197693, 'lng': 72.85578509999999}, 'travel_mode': 'DRIVING'} \n",
651 | " \n",
652 | " \n",
653 | " -- Step -- {73}\n",
654 | "\n",
655 | " {'distance': {'text': '2.3 km', 'value': 2344}, 'duration': {'text': '3 mins', 'value': 186}, 'end_location': {'lat': 19.0841631, 'lng': 72.84623909999999}, 'maneuver': 'keep-right', 'start_location': {'lat': 19.0991497, 'lng': 72.8540131}, 'travel_mode': 'DRIVING'} \n",
656 | " \n",
657 | " \n",
658 | " -- Step -- {74}\n",
659 | "\n",
660 | " {'distance': {'text': '1.1 km', 'value': 1076}, 'duration': {'text': '3 mins', 'value': 188}, 'end_location': {'lat': 19.0745818, 'lng': 72.8469898}, 'maneuver': 'ramp-left', 'start_location': {'lat': 19.0841631, 'lng': 72.84623909999999}, 'travel_mode': 'DRIVING'} \n",
661 | " \n",
662 | " \n",
663 | " -- Step -- {75}\n",
664 | "\n",
665 | " {'distance': {'text': '1.5 km', 'value': 1467}, 'duration': {'text': '4 mins', 'value': 220}, 'end_location': {'lat': 19.0744964, 'lng': 72.86047909999999}, 'maneuver': 'turn-left', 'start_location': {'lat': 19.0745818, 'lng': 72.8469898}, 'travel_mode': 'DRIVING'} \n",
666 | " \n",
667 | " \n",
668 | " -- Step -- {76}\n",
669 | "\n",
670 | " {'distance': {'text': '1.8 km', 'value': 1850}, 'duration': {'text': '5 mins', 'value': 319}, 'end_location': {'lat': 19.0761471, 'lng': 72.87458099999999}, 'maneuver': 'turn-right', 'start_location': {'lat': 19.0744964, 'lng': 72.86047909999999}, 'travel_mode': 'DRIVING'} \n",
671 | " \n",
672 | " \n",
673 | " -- Step -- {77}\n",
674 | "\n",
675 | " {'distance': {'text': '0.1 km', 'value': 98}, 'duration': {'text': '1 min', 'value': 16}, 'end_location': {'lat': 19.0765581, 'lng': 72.875379}, 'maneuver': 'turn-slight-left', 'start_location': {'lat': 19.0761471, 'lng': 72.87458099999999}, 'travel_mode': 'DRIVING'} \n",
676 | " \n",
677 | " \n",
678 | " -- Step -- {78}\n",
679 | "\n",
680 | " {'distance': {'text': '0.3 km', 'value': 260}, 'duration': {'text': '1 min', 'value': 50}, 'end_location': {'lat': 19.075963, 'lng': 72.877641}, 'maneuver': 'straight', 'start_location': {'lat': 19.0765581, 'lng': 72.875379}, 'travel_mode': 'DRIVING'} \n",
681 | " \n",
682 | " \n"
683 | ],
684 | "name": "stdout"
685 | }
686 | ]
687 | },
688 | {
689 | "cell_type": "markdown",
690 | "metadata": {
691 | "id": "JSZeWwz_xas7"
692 | },
693 | "source": [
694 | "******"
695 | ]
696 | },
697 | {
698 | "cell_type": "markdown",
699 | "metadata": {
700 | "id": "ct1lpPVWxas7"
701 | },
702 | "source": [
703 | "*****"
704 | ]
705 | },
706 | {
707 | "cell_type": "markdown",
708 | "metadata": {
709 | "id": "7lhtI_Nyxas7"
710 | },
711 | "source": [
712 | "# Google Distance Matrix API in Python "
713 | ]
714 | },
715 | {
716 | "cell_type": "code",
717 | "metadata": {
718 | "id": "nnG1aYS-xas8",
719 | "outputId": "e0c83d18-f288-4ded-bf28-5db99a34f8dc"
720 | },
721 | "source": [
722 | "# importing Google Matrix module \n",
723 | "import googlemaps \n",
724 | "\n",
725 | "# Requires API key \n",
726 | "gmaps = googlemaps.Client(key=api) \n",
727 | "\n",
728 | "start=input(\"\\n \\n '{Enter Start Location}' :- \")\n",
729 | "end=input(\"\\n \\n '{Enter End Location}' :- \")\n",
730 | "\n",
731 | "# Requires cities name \n",
732 | "my_dist = gmaps.distance_matrix(start,end)\n",
733 | "\n",
734 | "# Printing the result "
735 | ],
736 | "execution_count": null,
737 | "outputs": [
738 | {
739 | "output_type": "stream",
740 | "text": [
741 | "\n",
742 | " \n",
743 | " '{Enter Start Location}' :- rajasthan\n",
744 | "\n",
745 | " \n",
746 | " '{Enter End Location}' :- delhi\n"
747 | ],
748 | "name": "stdout"
749 | }
750 | ]
751 | },
752 | {
753 | "cell_type": "markdown",
754 | "metadata": {
755 | "id": "t5f77h8Cxas8"
756 | },
757 | "source": [
758 | "*****"
759 | ]
760 | },
761 | {
762 | "cell_type": "markdown",
763 | "metadata": {
764 | "id": "uuyLGZ2Yxas8"
765 | },
766 | "source": [
767 | "#### Getting Details from Google matrix api"
768 | ]
769 | },
770 | {
771 | "cell_type": "code",
772 | "metadata": {
773 | "id": "5nkY47R9xas9",
774 | "outputId": "5ae8fbaf-983a-4a03-c08d-6df78a7ae1fb"
775 | },
776 | "source": [
777 | "origin_addresses=my_dist.get(\"origin_addresses\")[0]\n",
778 | "print(\"\\n \\n \",\"'{Origin Addresses}':-\",origin_addresses,\"\\n\")\n",
779 | "\n",
780 | "# Destination\n",
781 | "\n",
782 | "destination_addresses=my_dist.get(\"destination_addresses\")[0]\n",
783 | "print(\"'{Destination Addresses}':-\",destination_addresses,\"\\n\")\n",
784 | "\n",
785 | "#Distance\n",
786 | "\n",
787 | "distance=my_dist.get(\"rows\")[0]['elements'][0]['distance']\n",
788 | "distance=distance.get('text')\n",
789 | "print(\"'{Distance}':-\",distance,\"\\n\")\n",
790 | "\n",
791 | "#Duration\n",
792 | "\n",
793 | "duration=my_dist.get(\"rows\")[0]['elements'][0]['duration']\n",
794 | "duration=duration.get('text')\n",
795 | "print(\"'{Duration}':-\",duration,\"\\n \\n \")"
796 | ],
797 | "execution_count": null,
798 | "outputs": [
799 | {
800 | "output_type": "stream",
801 | "text": [
802 | "\n",
803 | " \n",
804 | " '{Origin Addresses}':- Rajasthan, India \n",
805 | "\n",
806 | "'{Destination Addresses}':- Delhi, India \n",
807 | "\n",
808 | "'{Distance}':- 406 km \n",
809 | "\n",
810 | "'{Duration}':- 8 hours 21 mins \n",
811 | " \n",
812 | " \n"
813 | ],
814 | "name": "stdout"
815 | }
816 | ]
817 | },
818 | {
819 | "cell_type": "markdown",
820 | "metadata": {
821 | "id": "IZ_x6Kmuxas-"
822 | },
823 | "source": [
824 | "*****"
825 | ]
826 | },
827 | {
828 | "cell_type": "markdown",
829 | "metadata": {
830 | "id": "yTkY1trRxas-"
831 | },
832 | "source": [
833 | "# Finding Address distance using 'Geopy' Python Library "
834 | ]
835 | },
836 | {
837 | "cell_type": "markdown",
838 | "metadata": {
839 | "id": "5gEEhLR3xas-"
840 | },
841 | "source": [
842 | "#### geopy"
843 | ]
844 | },
845 | {
846 | "cell_type": "code",
847 | "metadata": {
848 | "id": "M03WXqNWxas_",
849 | "outputId": "76c7938d-0a3c-4c4a-ad21-46270c32e613"
850 | },
851 | "source": [
852 | "from geopy.geocoders import Nominatim\n",
853 | "from geopy.distance import geodesic\n",
854 | "\n",
855 | "geolocator = Nominatim(user_agent=\"ashish\")\n",
856 | "\n",
857 | "# enter first location\n",
858 | "\n",
859 | "\n",
860 | "start=input(\"\\n \\n '{Enter Start Location}' :- \")\n",
861 | "end=input(\"\\n \\n '{Enter End Location}' :- \")\n",
862 | "\n",
863 | "\n",
864 | "\n",
865 | "location_1 = geolocator.geocode(start)\n",
866 | "\n",
867 | "# enter second location\n",
868 | "\n",
869 | "location_2= geolocator.geocode(end)\n",
870 | "\n",
871 | "print(\"\\n \\n '{First Location}' :-\",location_1.address,\"\\n\")\n",
872 | "print(\"\\n \\n '{Second Location}':-\",location_2.address,\"\\n\")\n",
873 | "\n",
874 | "### Getting Latitude and Longitude\n",
875 | "\n",
876 | "first_=(location_1.latitude, location_1.longitude)\n",
877 | "second_=(location_2.latitude, location_2.longitude)\n",
878 | "\n",
879 | "\n",
880 | "print(\"\\n \\n '{First Location latitude & Longitude}' :-\",first_,\"\\n\")\n",
881 | "print(\"\\n \\n '{Second Location latitude & Longitude}':-\",second_,\"\\n\")\n",
882 | "\n",
883 | "#Distance\n",
884 | "\n",
885 | "distance=round(geodesic(first_, second_).kilometers,3)\n",
886 | "print(\"\\n \\n '{Distance}' :- \",distance,\"\\n \\n \")"
887 | ],
888 | "execution_count": null,
889 | "outputs": [
890 | {
891 | "output_type": "stream",
892 | "text": [
893 | "\n",
894 | " \n",
895 | " '{Enter Start Location}' :- manali\n",
896 | "\n",
897 | " \n",
898 | " '{Enter End Location}' :- delhi\n",
899 | "\n",
900 | " \n",
901 | " '{First Location}' :- Manali, Kullu, Himachal Pradesh, India \n",
902 | "\n",
903 | "\n",
904 | " \n",
905 | " '{Second Location}':- Delhi, Kotwali Tehsil, Central Delhi, Delhi, 110006, India \n",
906 | "\n",
907 | "\n",
908 | " \n",
909 | " '{First Location latitude & Longitude}' :- (32.26309405, 77.18812183241408) \n",
910 | "\n",
911 | "\n",
912 | " \n",
913 | " '{Second Location latitude & Longitude}':- (28.6517178, 77.2219388) \n",
914 | "\n",
915 | "\n",
916 | " \n",
917 | " '{Distance}' :- 400.372 \n",
918 | " \n",
919 | " \n"
920 | ],
921 | "name": "stdout"
922 | }
923 | ]
924 | },
925 | {
926 | "cell_type": "markdown",
927 | "metadata": {
928 | "id": "pFTFmbrpxas_"
929 | },
930 | "source": [
931 | "*****"
932 | ]
933 | },
934 | {
935 | "cell_type": "markdown",
936 | "metadata": {
937 | "id": "_Ufnn7vRxatA"
938 | },
939 | "source": [
940 | "*****"
941 | ]
942 | }
943 | ]
944 | }
--------------------------------------------------------------------------------
/Django Noes.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "id": "worthy-promise",
6 | "metadata": {},
7 | "source": [
8 | "# * Django Flask Theory *"
9 | ]
10 | },
11 | {
12 | "cell_type": "markdown",
13 | "id": "south-meeting",
14 | "metadata": {},
15 | "source": [
16 | "*****"
17 | ]
18 | },
19 | {
20 | "cell_type": "markdown",
21 | "id": "headed-jackson",
22 | "metadata": {},
23 | "source": [
24 | "# Model Manager "
25 | ]
26 | },
27 | {
28 | "cell_type": "markdown",
29 | "id": "abandoned-telephone",
30 | "metadata": {},
31 | "source": [
32 | "\n",
33 | "\n",
34 | "* A Manager is the interface through which\n",
35 | "database query operations are provided to \n",
36 | "Django models. At least one Manager exists\n",
37 | "for every model in a Django application.\n",
38 | "Model manager is used to interact with database. \n",
39 | "By default, Django adds a Manager with the name\n",
40 | "objects to every Django model class.\n",
41 | "djanga.db.models.manager.Manager \n"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "id": "corresponding-comparative",
47 | "metadata": {},
48 | "source": [
49 | "# signals "
50 | ]
51 | },
52 | {
53 | "cell_type": "markdown",
54 | "id": "bacterial-emission",
55 | "metadata": {},
56 | "source": [
57 | "\n",
58 | "\n",
59 | "* The signals are utilities that allow us to associate events with actions.\n",
60 | "Signals allow certain senders to notify a set of receivers that some action \n",
61 | "has taken place.\n",
62 | "* \n",
63 | "• Login and Logout Signals \n",
64 | "• Model Signals \n",
65 | "• Management Signals\n",
66 | "• Request/Response Signals \n",
67 | "• Test Signals \n"
68 | ]
69 | },
70 | {
71 | "cell_type": "markdown",
72 | "id": "social-cedar",
73 | "metadata": {},
74 | "source": [
75 | "# Abstract Base Classes "
76 | ]
77 | },
78 | {
79 | "cell_type": "markdown",
80 | "id": "enhanced-helen",
81 | "metadata": {},
82 | "source": [
83 | "\n",
84 | "* Abstract base classes are useful when you want to put some common information \n",
85 | "into a number of other models. You write your base class and put abstract—True \n",
86 | "in the Meta class. This model will then not be used to create any database table.\n",
87 | "Instead, when it is used m a base class for other models, its fields will be \n",
88 | "added to those of the child elms. It does not generate a database table or \n",
89 | "have a manager, and cannot be instantiated or saved directly. "
90 | ]
91 | },
92 | {
93 | "cell_type": "markdown",
94 | "id": "aggregate-voluntary",
95 | "metadata": {},
96 | "source": [
97 | "# JSON Web Token (JWT) "
98 | ]
99 | },
100 | {
101 | "cell_type": "markdown",
102 | "id": "suspected-range",
103 | "metadata": {},
104 | "source": [
105 | "\n",
106 | "* JSON Web Token is a fairly new standard which can be used for token-based \n",
107 | "authentication. Unlike the built-in TokenAuthentication scheme, \n",
108 | "JWT Authentication doesn't need to use a database to validate a token. "
109 | ]
110 | },
111 | {
112 | "cell_type": "markdown",
113 | "id": "architectural-makeup",
114 | "metadata": {},
115 | "source": [
116 | "# User Authentication System"
117 | ]
118 | },
119 | {
120 | "cell_type": "markdown",
121 | "id": "frozen-literacy",
122 | "metadata": {},
123 | "source": [
124 | "\n",
125 | "* Django comes with a user authentication system. It handles user accounts, groups, permissions and cookie-based user sessions. Django authentication provides both authentication and authorization together and is generally referred to as the authentication system. By default, the required configuration is already included in the settings.py generated by django-admin startproject, these consist of two items listed in your INSTALLED_APPS setting: 'django.contrib.auth' contains the core of the authentication framework, and its default models. 'django.contrib.contenttypes' is the Django content type system, which allows permissions to be associated with models you create. "
126 | ]
127 | },
128 | {
129 | "cell_type": "markdown",
130 | "id": "retired-background",
131 | "metadata": {},
132 | "source": [
133 | "# What's the Difference Between HTTP and HTTPS? "
134 | ]
135 | },
136 | {
137 | "cell_type": "markdown",
138 | "id": "professional-manual",
139 | "metadata": {},
140 | "source": [
141 | "\n",
142 | "\n",
143 | "* HTTP \n",
144 | "✓ HTTP stands for Hyper Text Transfer Protocol. \n",
145 | "✓ HTTP is less secure. ✓ HTTP works in the application layer of 051 model. ✓ HTTP does not uses certificate. ✓ There is no encryption and decryption. \n",
146 | "\n"
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "id": "threatened-mustang",
152 | "metadata": {},
153 | "source": [
154 | "\n",
155 | "* HTTPs \n",
156 | "✓ HTTPs stands for Hyper Text Transfer Protocol Secure. \n",
157 | "✓ HTTPs is more secure. ✓ HTTPs works in the transport layer of OSI model. ✓ HTTPs uses SSL certificate. ✓ There is encryption and decryption "
158 | ]
159 | },
160 | {
161 | "cell_type": "markdown",
162 | "id": "fitting-resolution",
163 | "metadata": {},
164 | "source": [
165 | "# Cookies "
166 | ]
167 | },
168 | {
169 | "cell_type": "markdown",
170 | "id": "vulnerable-yield",
171 | "metadata": {},
172 | "source": [
173 | "\n",
174 | "* A cookie is a small piece of text data set by Web server that resided on the client's machine. Once it's been set, the client automatically returns the cookie to the web server with each request that it makes. This allows the server to place value it wishes to 'remember' in the cookie, and have access to them when creating a response. "
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "id": "military-stewart",
180 | "metadata": {},
181 | "source": [
182 | "# Session "
183 | ]
184 | },
185 | {
186 | "cell_type": "markdown",
187 | "id": "exempt-shelter",
188 | "metadata": {},
189 | "source": [
190 | "* The session framework lets you store and retrieve arbitrary data on a per-site-visitor basis. It stores data on the server side and abstracts the sending and receiving of cookies. Cookies contain a session ID not the data itself. By default, Django stores sessions in your database. As it stores sessions in database so it is mandatory to run makemigmtions and migrate to use session. It will create required tables. The Django sessions framework is entirely, and solely, cookie-based. "
191 | ]
192 | },
193 | {
194 | "cell_type": "markdown",
195 | "id": "democratic-france",
196 | "metadata": {},
197 | "source": [
198 | "# Cache "
199 | ]
200 | },
201 | {
202 | "cell_type": "markdown",
203 | "id": "decreased-technology",
204 | "metadata": {},
205 | "source": [
206 | "\n",
207 | "* A Cache is an information technology for the temporary storage (caching) of Web documents, such as Web pages, images, and other types of Web multimedia, to reduce server lag. Caching is one of those methods which a website implements to become faster. It is cost efficient and saves CPU processing time. Django comes with a robust cache system that lets you save dynamic pages so they don't have to be calculated for each request. You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site. Following are the options of caching:- • Database Caching • File System Caching • Local Memory Caching "
208 | ]
209 | },
210 | {
211 | "cell_type": "markdown",
212 | "id": "exposed-order",
213 | "metadata": {},
214 | "source": [
215 | "# Object Relational Mapper (ORM)"
216 | ]
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "id": "numeric-quality",
221 | "metadata": {},
222 | "source": [
223 | "* Object-Relational Mapper (ORM), which enables application to interact with database\n",
224 | "such as SQLite, MySQL, PostgreSQL, Oracle. ORMs automatically create a database schema \n",
225 | "from defined classes or models. It generate SQL from Python code for a particular \n",
226 | "database which means developer do not need to write SQL Code. ORM maps objects \n",
227 | "attributes to respective table fields. It is easier to change the database \n",
228 | "if we use ORMs hence project becomes more portable.\n",
229 | "Django's ORM is just a way to create SQL to query and manipulate your databasle \n",
230 | "and get results in a pythonic fashion. \n"
231 | ]
232 | },
233 | {
234 | "cell_type": "markdown",
235 | "id": "ae90f9df",
236 | "metadata": {},
237 | "source": [
238 | "# What Is The Difference Between select_related & prefetch_ related?"
239 | ]
240 | },
241 | {
242 | "cell_type": "markdown",
243 | "id": "132a43a0",
244 | "metadata": {},
245 | "source": [
246 | "* * select_related: \n",
247 | "* It Returns a QuerySet that will “follow” foreign-key relationships, selecting additional related-object when it executes its query.\n",
248 | "* This is a performance booster which results in a single more complex query but means later use of foreign-key relationships won’t require database queries.\n",
249 | " \n",
250 | "* * prefetch_related:\n",
251 | "* We use prefetch_related when we’ 're going to get a set of things.\n",
252 | "* That means forward ManyToMany and backward ManyToMany, ForeignKey. \n",
253 | "* prefetch_related does a separate lookup for each relationship and performs the “joining” in Python"
254 | ]
255 | },
256 | {
257 | "cell_type": "markdown",
258 | "id": "c010f4a3",
259 | "metadata": {},
260 | "source": [
261 | "# Q.3 Django Default files"
262 | ]
263 | },
264 | {
265 | "cell_type": "markdown",
266 | "id": "efe760f9",
267 | "metadata": {},
268 | "source": [
269 | "* a) __ init__.py: An empty file that tells Python that the current directory should be considered as a Python package.\n",
270 | "* b) admin.py: Reads model metadata and provides an interface to manage app content\n",
271 | "* c) app.py: Application configuration details for the app are included e.g custom app name.\n",
272 | "* d) migrations.py: Contains migrated model details with the corresponding database table structure\n",
273 | "* e) models.py: A class for each model is defined with the model structure layout\n",
274 | "* f) tests.py: App unit test automation classes are included in this\n",
275 | "* g) views.py: Web-based requests and response is configured in this file"
276 | ]
277 | },
278 | {
279 | "cell_type": "markdown",
280 | "id": "9eb1b16e",
281 | "metadata": {},
282 | "source": [
283 | "# Explain The Migration In Django."
284 | ]
285 | },
286 | {
287 | "cell_type": "markdown",
288 | "id": "3f59feae",
289 | "metadata": {},
290 | "source": [
291 | "\n",
292 | "\n",
293 | "There are several commands you use to interact with Migrations In Django:\n",
294 | "* a) makemigration - This command is used to create a migration file.\n",
295 | "* b) migrate - This command creates a table according to the schema defined in the migration file.\n",
296 | "* c) showmigrations - This command list all the migrations and their status.\n",
297 | "* d) sqlmigrate - This command is used to show a raw SQL query of the applied migration.\n",
298 | "\n"
299 | ]
300 | },
301 | {
302 | "cell_type": "markdown",
303 | "id": "ca870557",
304 | "metadata": {},
305 | "source": [
306 | "# How to setup a database in Django?"
307 | ]
308 | },
309 | {
310 | "cell_type": "markdown",
311 | "id": "10284ec3",
312 | "metadata": {},
313 | "source": [
314 | "* In the case of other databases you have to the following keys in the DATABASE ‘default’ item to match your database connection settings.\n",
315 | "* Engines: you can change database by using ‘django.db.backends.sqlite3’ , ‘django.db.backeneds.mysql’,\n",
316 | "* * ‘django.db.backends.postgresql_psycopg2’, ‘django.db.backends.oracle’ and so on\n",
317 | "* * 'ENGINE': 'django.db.backends.postgresql_psycopg2',\n",
318 | "* Now we should replace the above code with our connection credentials to Mysql. The updated code should look like the code below.\n",
319 | "* * {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2'}}"
320 | ]
321 | },
322 | {
323 | "cell_type": "markdown",
324 | "id": "eb891d64",
325 | "metadata": {},
326 | "source": [
327 | "# What do u mean by CSRF token?"
328 | ]
329 | },
330 | {
331 | "cell_type": "markdown",
332 | "id": "fcbb0549",
333 | "metadata": {},
334 | "source": [
335 | "* CSRF stands for Cross-Site Request Forgery.\n",
336 | "* The csrf_token is used for protection against Cross-Site Request Forgeries.\n",
337 | "* This kind of attack takes place when a malicious website consists of a link, some JavaScript or a form whose aim is to perform some action on your website by using the login credentials of a genuine user.\n",
338 | "* CSRF tokens can prevent CSREF attacks by making it impossible for an attacker to construct a fully valid HTTP request suitable for feeding to a victim user.\n",
339 | "* A CSRF token is a unique, secret, unpredictable value that is generated by the server-side application and transmitted to the client in such a way that it is included in a subsequent HTTP request made by the client.\n",
340 | "* When the later request is made, the server-side application validates that the request includes the expected token and rejects the request if the token is missing or invalid.\n",
341 | "\n"
342 | ]
343 | },
344 | {
345 | "cell_type": "markdown",
346 | "id": "4fb64940",
347 | "metadata": {},
348 | "source": [
349 | "# whar are the Limitation of Django ORM?"
350 | ]
351 | },
352 | {
353 | "cell_type": "markdown",
354 | "id": "fb975d9c",
355 | "metadata": {},
356 | "source": [
357 | "* If the data is complex and consists of multiple joins using the SQL will be clearer.\n",
358 | "* If Performance is a concern for you, ORM isn’t your choice.\n",
359 | "* Generally. Object-relation-mapping is considered a good option to construct an optimized query, SQL has an upper hand when compared to ORM.\n",
360 | "\n"
361 | ]
362 | },
363 | {
364 | "cell_type": "markdown",
365 | "id": "77cee1fb",
366 | "metadata": {},
367 | "source": [
368 | "# What is QuerySet in Django ?"
369 | ]
370 | },
371 | {
372 | "cell_type": "markdown",
373 | "id": "486698ef",
374 | "metadata": {},
375 | "source": [
376 | "\n",
377 | "* QuerySet is a collection of SQL queries.\n",
378 | "* QuerySet in Django is basically a collection of objects from our database.\n",
379 | "* QuerySets are used by the Django ORM. When we use our models to get a single record or a group of records from the database, they are returned as QuerySets.\n",
380 | "* It is comparable to a database select operation.\n",
381 | "\n",
382 | "Ex\n",
383 | "* users.objects.all()\n",
384 | "* users.objects.filter(name=\"nitin\")\n",
385 | "* users.objects.get(id=3)"
386 | ]
387 | },
388 | {
389 | "cell_type": "markdown",
390 | "id": "b6a6cef5",
391 | "metadata": {},
392 | "source": [
393 | "# What Is The Command To Create A Project & An App In Django?"
394 | ]
395 | },
396 | {
397 | "cell_type": "markdown",
398 | "id": "b16e63db",
399 | "metadata": {},
400 | "source": [
401 | "* Command To Create A Project: django-admin startproject ProjectAshish\n",
402 | "* Command To Create An App: python manage.py startapp AppAshish"
403 | ]
404 | },
405 | {
406 | "cell_type": "markdown",
407 | "id": "b530796a",
408 | "metadata": {},
409 | "source": [
410 | "# what is the Project Structure in Django?"
411 | ]
412 | },
413 | {
414 | "cell_type": "markdown",
415 | "id": "8c211044",
416 | "metadata": {},
417 | "source": [
418 | "* a) manage.py: A command-line utility that allows you to interact with your Django project & this file is used to control your Django project on the server or even to begin one.\n",
419 | "* b) ___init___.py: An empty file that tells Python that the current directory should be considered as a Python package\n",
420 | "* c) settings.py: Comprises the configurations of the current project like DB connections, middlewares etc \n",
421 | "* d) urls.py: All the URLs of the project are present here \n",
422 | "* e) wsgi.py: This is an entry point for your application which is used by the web servers to serve the project you have created\n",
423 | "\n"
424 | ]
425 | },
426 | {
427 | "cell_type": "markdown",
428 | "id": "a380eded",
429 | "metadata": {},
430 | "source": [
431 | "# What Is The Difference Between Flask, Pyramid And Django?"
432 | ]
433 | },
434 | {
435 | "cell_type": "markdown",
436 | "id": "ec00a9c1",
437 | "metadata": {},
438 | "source": [
439 | "\n",
440 | "\n",
441 | "** Django\n",
442 | "* Django is a high-level Python framework while Flask is a low-level Python Framework providing you with the minimum functionality, a server would require.\n",
443 | "* Django comes with lots of built-in functionality like Django ORM, Admin Panel, Web-templating System, Inheritance, and serialization while Flask comes with a development server, NoSQL support, and support for unit testing, which are already there in Django.\n",
444 | "\n",
445 | "** Flask\n",
446 | "* Flask is more customizable than Django as Flask comes with no predefined structure or scaffold while Django’s file structure is fixed.\n",
447 | "* Flask settings are user made and can be altered completely by the user. Django settings are not customizable to that degree, it has variables where only values are modifiable.\n",
448 | "* Flask has more speed than Django when it comes to processing requests but that comes without any APIs or functionality that Django gives you in-built.\n",
449 | "* Flask is for the developers who want more flexibility on their website and don’t need lots of built-in extra functions, while Django is for developers who want rapid development of their applications that can sustain dynamic changes to their environment.\n",
450 | "\n"
451 | ]
452 | },
453 | {
454 | "cell_type": "markdown",
455 | "id": "d941d644",
456 | "metadata": {},
457 | "source": [
458 | "# what is Command to create a migration file inside the migration folder?\n",
459 | "\n"
460 | ]
461 | },
462 | {
463 | "cell_type": "markdown",
464 | "id": "45624d84",
465 | "metadata": {},
466 | "source": [
467 | "\n",
468 | "\n",
469 | "* python manage.py makemigrations \n",
470 | "\n",
471 | "* After creating a migration to reflect changes in the database permanently execute migrate command:\n",
472 | "python manage.py migrate\n",
473 | "\n",
474 | "* To see raw SQL query executing behind applied migration execute the command:\n",
475 | "* * python manage.py sqlmigrate app_name migration_name\n",
476 | "* * python manage.py sqlmigrate nitapp 0001\n",
477 | "\n",
478 | "* To see all migrations, execute the command:\n",
479 | "* * python manage.py showmigrations \n"
480 | ]
481 | },
482 | {
483 | "cell_type": "markdown",
484 | "id": "0196cedb",
485 | "metadata": {},
486 | "source": [
487 | "# How To Filter Items In The Model Using Django QuerySet?\n",
488 | "\n",
489 | "\n"
490 | ]
491 | },
492 | {
493 | "cell_type": "markdown",
494 | "id": "b296a065",
495 | "metadata": {},
496 | "source": [
497 | "\n",
498 | "* Django Command To Filter Items In A Model:\n",
499 | "* Users.objects.filter(name=\"Nitin\")\n",
500 | "***?** where “User” is a model name\n"
501 | ]
502 | },
503 | {
504 | "cell_type": "markdown",
505 | "id": "65fcc27d",
506 | "metadata": {},
507 | "source": [
508 | "# How To Get A Particular Item In The Model Using Django QuerySet?"
509 | ]
510 | },
511 | {
512 | "cell_type": "markdown",
513 | "id": "bc1f2cbe",
514 | "metadata": {},
515 | "source": [
516 | "* Django Command To Get A Particular Item In A Model:\n",
517 | "\n",
518 | "* * Users.objects.get(id=25)\n",
519 | "\n",
520 | "***?** where “User” is a model name."
521 | ]
522 | },
523 | {
524 | "cell_type": "markdown",
525 | "id": "47e392a7",
526 | "metadata": {},
527 | "source": [
528 | "# How to Delete/Insert/Update An Object Using QuerySet In Django?\n"
529 | ]
530 | },
531 | {
532 | "cell_type": "markdown",
533 | "id": "8ecd6d44",
534 | "metadata": {},
535 | "source": [
536 | "* * QuerySet To Update An Object:\n",
537 | " \n",
538 | "* user_to_be_modify = User.objects.get(pk = 3)\n",
539 | "* user_to_be_modify.city = \"Pune\"\n",
540 | "* user_to_be_modify.save()"
541 | ]
542 | },
543 | {
544 | "cell_type": "markdown",
545 | "id": "1a260218",
546 | "metadata": {},
547 | "source": [
548 | "# How to Delete/Insert/Update An Object Using QuerySet In Django?\n",
549 | "\n",
550 | "\n"
551 | ]
552 | },
553 | {
554 | "cell_type": "markdown",
555 | "id": "8645040a",
556 | "metadata": {},
557 | "source": [
558 | "* Users. objects.filter(id= 54).delete()"
559 | ]
560 | },
561 | {
562 | "cell_type": "markdown",
563 | "id": "92a1654b",
564 | "metadata": {},
565 | "source": [
566 | "# How to Delete/Insert/Update An Object Using QuerySet In Django?\n"
567 | ]
568 | },
569 | {
570 | "cell_type": "markdown",
571 | "id": "149ce02a",
572 | "metadata": {},
573 | "source": [
574 | "* new_user = User(name = \"Nitin Mangotra\", city=\"Gurgaon\")\n",
575 | "* new_user.save()"
576 | ]
577 | },
578 | {
579 | "cell_type": "markdown",
580 | "id": "34474492",
581 | "metadata": {},
582 | "source": [
583 | "*****"
584 | ]
585 | }
586 | ],
587 | "metadata": {
588 | "kernelspec": {
589 | "display_name": "Python 3",
590 | "language": "python",
591 | "name": "python3"
592 | },
593 | "language_info": {
594 | "codemirror_mode": {
595 | "name": "ipython",
596 | "version": 3
597 | },
598 | "file_extension": ".py",
599 | "mimetype": "text/x-python",
600 | "name": "python",
601 | "nbconvert_exporter": "python",
602 | "pygments_lexer": "ipython3",
603 | "version": "3.6.7"
604 | }
605 | },
606 | "nbformat": 4,
607 | "nbformat_minor": 5
608 | }
609 |
--------------------------------------------------------------------------------
/Face Recognition Machine Learning.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "kernelspec": {
6 | "display_name": "Python 3",
7 | "language": "python",
8 | "name": "python3"
9 | },
10 | "language_info": {
11 | "codemirror_mode": {
12 | "name": "ipython",
13 | "version": 3
14 | },
15 | "file_extension": ".py",
16 | "mimetype": "text/x-python",
17 | "name": "python",
18 | "nbconvert_exporter": "python",
19 | "pygments_lexer": "ipython3",
20 | "version": "3.8.5"
21 | },
22 | "colab": {
23 | "name": "Face Recognition Machine Learning.ipynb",
24 | "provenance": [],
25 | "collapsed_sections": [],
26 | "include_colab_link": true
27 | }
28 | },
29 | "cells": [
30 | {
31 | "cell_type": "markdown",
32 | "metadata": {
33 | "id": "view-in-github",
34 | "colab_type": "text"
35 | },
36 | "source": [
37 | "
"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {
43 | "id": "2Uervu5neg3e"
44 | },
45 | "source": [
46 | "##Facial Recognition Algorithms for Machine Learning"
47 | ]
48 | },
49 | {
50 | "cell_type": "markdown",
51 | "metadata": {
52 | "id": "MTFXLzC_e0pt"
53 | },
54 | "source": [
55 | "facial recognition is a technology capable of recognizing a person based on their face. It is grounded in complex mathematical AI and machine learning algorithms which capture, store and analyze facial features in order to match them with images of individuals in a pre-existing database and, often, information about them in that database. Facial recognition is a part of a tech umbrella term of biometrics that also includes fingerprint, palm print, eye scanning, gait, voice and signature recognition."
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {
61 | "id": "VkxdkY7WeilX"
62 | },
63 | "source": [
64 | "##Code"
65 | ]
66 | },
67 | {
68 | "cell_type": "code",
69 | "metadata": {
70 | "id": "Qi0_rN1MdtkP",
71 | "outputId": "1a803fc2-0124-4a6e-9459-eb6f52db57fe"
72 | },
73 | "source": [
74 | "import os\n",
75 | "import cv2\n",
76 | "from PIL import Image\n",
77 | "\n",
78 | "\n",
79 | "class Validation:\n",
80 | "\n",
81 | " def __init__(self, filename):\n",
82 | " self.filename = filename\n",
83 | "\n",
84 | " def person_image_check(self, filenames_change):\n",
85 | " image = cv2.imread(filenames_change)\n",
86 | " print(repr(image))\n",
87 | " gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
88 | " print(repr(gray))\n",
89 | " faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + \"haarcascade_frontalface_default.xml\")\n",
90 | " print(faceCascade)\n",
91 | " faces = faceCascade.detectMultiScale(gray, scaleFactor=1.3,\n",
92 | " minNeighbors=3,\n",
93 | " minSize=(30, 30))\n",
94 | " if len(faces) >= 1:\n",
95 | " return True\n",
96 | " else:\n",
97 | " return False\n",
98 | "\n",
99 | " def validating_input(self):\n",
100 | " for angle in range(0, 360, 90):\n",
101 | " im = Image.open(self.filename)\n",
102 | " filenames_change = f\"{angle}.png\"\n",
103 | " out = im.rotate(angle, expand=True)\n",
104 | " print(out)\n",
105 | " out.save(filenames_change)\n",
106 | " return_output = self.person_image_check(filenames_change)\n",
107 | " os.remove(filenames_change)\n",
108 | " if return_output:\n",
109 | " print(\"Angle is:-\",angle,\"degree\")\n",
110 | " return True\n",
111 | " else:\n",
112 | " return False\n",
113 | "\n",
114 | "# run it\n",
115 | "\n",
116 | "aa = Validation('live_image.png')\n",
117 | "aa.validating_input()"
118 | ],
119 | "execution_count": null,
120 | "outputs": [
121 | {
122 | "output_type": "stream",
123 | "text": [
124 | "\n",
125 | "array([[[255, 255, 255],\n",
126 | " [255, 255, 255],\n",
127 | " [255, 255, 255],\n",
128 | " ...,\n",
129 | " [255, 255, 255],\n",
130 | " [255, 255, 255],\n",
131 | " [255, 255, 255]],\n",
132 | "\n",
133 | " [[255, 255, 255],\n",
134 | " [255, 255, 255],\n",
135 | " [255, 255, 255],\n",
136 | " ...,\n",
137 | " [255, 255, 255],\n",
138 | " [255, 255, 255],\n",
139 | " [255, 255, 255]],\n",
140 | "\n",
141 | " [[255, 255, 255],\n",
142 | " [255, 255, 255],\n",
143 | " [255, 255, 255],\n",
144 | " ...,\n",
145 | " [255, 255, 255],\n",
146 | " [255, 255, 255],\n",
147 | " [255, 255, 255]],\n",
148 | "\n",
149 | " ...,\n",
150 | "\n",
151 | " [[255, 255, 255],\n",
152 | " [255, 255, 255],\n",
153 | " [255, 255, 255],\n",
154 | " ...,\n",
155 | " [255, 255, 255],\n",
156 | " [255, 255, 255],\n",
157 | " [255, 255, 255]],\n",
158 | "\n",
159 | " [[255, 255, 255],\n",
160 | " [255, 255, 255],\n",
161 | " [255, 255, 255],\n",
162 | " ...,\n",
163 | " [255, 255, 255],\n",
164 | " [255, 255, 255],\n",
165 | " [255, 255, 255]],\n",
166 | "\n",
167 | " [[255, 255, 255],\n",
168 | " [255, 255, 255],\n",
169 | " [255, 255, 255],\n",
170 | " ...,\n",
171 | " [255, 255, 255],\n",
172 | " [255, 255, 255],\n",
173 | " [255, 255, 255]]], dtype=uint8)\n",
174 | "array([[255, 255, 255, ..., 255, 255, 255],\n",
175 | " [255, 255, 255, ..., 255, 255, 255],\n",
176 | " [255, 255, 255, ..., 255, 255, 255],\n",
177 | " ...,\n",
178 | " [255, 255, 255, ..., 255, 255, 255],\n",
179 | " [255, 255, 255, ..., 255, 255, 255],\n",
180 | " [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)\n",
181 | "\n",
182 | "\n",
183 | "array([[[255, 255, 255],\n",
184 | " [255, 255, 255],\n",
185 | " [255, 255, 255],\n",
186 | " ...,\n",
187 | " [255, 255, 255],\n",
188 | " [255, 255, 255],\n",
189 | " [255, 255, 255]],\n",
190 | "\n",
191 | " [[255, 255, 255],\n",
192 | " [255, 255, 255],\n",
193 | " [255, 255, 255],\n",
194 | " ...,\n",
195 | " [255, 255, 255],\n",
196 | " [255, 255, 255],\n",
197 | " [255, 255, 255]],\n",
198 | "\n",
199 | " [[255, 255, 255],\n",
200 | " [255, 255, 255],\n",
201 | " [255, 255, 255],\n",
202 | " ...,\n",
203 | " [255, 255, 255],\n",
204 | " [255, 255, 255],\n",
205 | " [255, 255, 255]],\n",
206 | "\n",
207 | " ...,\n",
208 | "\n",
209 | " [[255, 255, 255],\n",
210 | " [255, 255, 255],\n",
211 | " [255, 255, 255],\n",
212 | " ...,\n",
213 | " [255, 255, 255],\n",
214 | " [255, 255, 255],\n",
215 | " [255, 255, 255]],\n",
216 | "\n",
217 | " [[255, 255, 255],\n",
218 | " [255, 255, 255],\n",
219 | " [255, 255, 255],\n",
220 | " ...,\n",
221 | " [255, 255, 255],\n",
222 | " [255, 255, 255],\n",
223 | " [255, 255, 255]],\n",
224 | "\n",
225 | " [[255, 255, 255],\n",
226 | " [255, 255, 255],\n",
227 | " [255, 255, 255],\n",
228 | " ...,\n",
229 | " [255, 255, 255],\n",
230 | " [255, 255, 255],\n",
231 | " [255, 255, 255]]], dtype=uint8)\n",
232 | "array([[255, 255, 255, ..., 255, 255, 255],\n",
233 | " [255, 255, 255, ..., 255, 255, 255],\n",
234 | " [255, 255, 255, ..., 255, 255, 255],\n",
235 | " ...,\n",
236 | " [255, 255, 255, ..., 255, 255, 255],\n",
237 | " [255, 255, 255, ..., 255, 255, 255],\n",
238 | " [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)\n",
239 | "\n",
240 | "Angle is:- 90 degree\n"
241 | ],
242 | "name": "stdout"
243 | },
244 | {
245 | "output_type": "execute_result",
246 | "data": {
247 | "text/plain": [
248 | "True"
249 | ]
250 | },
251 | "metadata": {
252 | "tags": []
253 | },
254 | "execution_count": 9
255 | }
256 | ]
257 | }
258 | ]
259 | }
--------------------------------------------------------------------------------
/Levenshtein Distance Algorithm .ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "kernelspec": {
6 | "display_name": "Python 3",
7 | "language": "python",
8 | "name": "python3"
9 | },
10 | "language_info": {
11 | "codemirror_mode": {
12 | "name": "ipython",
13 | "version": 3
14 | },
15 | "file_extension": ".py",
16 | "mimetype": "text/x-python",
17 | "name": "python",
18 | "nbconvert_exporter": "python",
19 | "pygments_lexer": "ipython3",
20 | "version": "3.8.5"
21 | },
22 | "colab": {
23 | "name": "Levenshtein Distance Algorithm.ipynb",
24 | "provenance": [],
25 | "include_colab_link": true
26 | }
27 | },
28 | "cells": [
29 | {
30 | "cell_type": "markdown",
31 | "metadata": {
32 | "id": "view-in-github",
33 | "colab_type": "text"
34 | },
35 | "source": [
36 | "
"
37 | ]
38 | },
39 | {
40 | "cell_type": "markdown",
41 | "metadata": {
42 | "id": "4cbRS_f7zxAA"
43 | },
44 | "source": [
45 | "# Implementing The Levenshtein Distance for Word Autocompletion and Autocorrection"
46 | ]
47 | },
48 | {
49 | "cell_type": "markdown",
50 | "metadata": {
51 | "id": "2ymoKfKxzxAG"
52 | },
53 | "source": [
54 | "The Levenshtein distance is a text similarity measure that compares two words and returns a numeric value representing the distance between them. The distance reflects the total number of single-character edits required to transform one word into another. The more similar the two words are the less distance between them, and vice versa. One common use for this distance is in the autocompletion or autocorrection features of text processors or chat applications."
55 | ]
56 | },
57 | {
58 | "cell_type": "markdown",
59 | "metadata": {
60 | "id": "kRMa1jLczxAH"
61 | },
62 | "source": [
63 | "* Creating the distances matrix\n",
64 | "* Initializing the distances matrix\n",
65 | "* Printing the distances matrix\n",
66 | "* Calculating distances between all prefixes\n",
67 | "* Dictionary search for autocompletion/autocorrection"
68 | ]
69 | },
70 | {
71 | "cell_type": "code",
72 | "metadata": {
73 | "id": "gphZ8sC5zxAH"
74 | },
75 | "source": [
76 | "import numpy as np"
77 | ],
78 | "execution_count": null,
79 | "outputs": []
80 | },
81 | {
82 | "cell_type": "markdown",
83 | "metadata": {
84 | "id": "uTCNVCdWzxAI"
85 | },
86 | "source": [
87 | "### Code"
88 | ]
89 | },
90 | {
91 | "cell_type": "code",
92 | "metadata": {
93 | "id": "0WFQ7lGyzxAI"
94 | },
95 | "source": [
96 | "\n",
97 | "def levenshtein_ratio_and_distance(s, t, ratio_calc = False):\n",
98 | " \n",
99 | " # Initialize matrix of zeros\n",
100 | " \n",
101 | " rows = len(s)+1\n",
102 | " cols = len(t)+1\n",
103 | " distance = np.zeros((rows,cols),dtype = int)\n",
104 | " \n",
105 | " # Populate matrix of zeros with the indeces of each character of both strings\n",
106 | " \n",
107 | " for i in range(1, rows):\n",
108 | " for k in range(1,cols):\n",
109 | " distance[i][0] = i\n",
110 | " distance[0][k] = k\n",
111 | " \n",
112 | " # Iterate over the matrix to compute the cost of deletions,insertions and/or substitutions \n",
113 | " \n",
114 | " for col in range(1, cols):\n",
115 | " for row in range(1, rows):\n",
116 | " if s[row-1] == t[col-1]:\n",
117 | " \n",
118 | " cost = 0 # If the characters are the same in the two strings in a given position [i,j] then the cost is 0\n",
119 | " \n",
120 | " else:\n",
121 | " \n",
122 | " # the cost of a substitution is 2. If we calculate just distance, then the cost of a substitution is 1.\n",
123 | " \n",
124 | " if ratio_calc == True:\n",
125 | " cost = 2\n",
126 | " else:\n",
127 | " cost = 1\n",
128 | " \n",
129 | " distance[row][col] = min(distance[row-1][col] + 1, # Cost of deletions\n",
130 | " distance[row][col-1] + 1, # Cost of insertions\n",
131 | " distance[row-1][col-1] + cost) # Cost of substitutions\n",
132 | " if ratio_calc == True:\n",
133 | " \n",
134 | " # Computation of the Levenshtein Distance Ratio\n",
135 | " \n",
136 | " Ratio = ((len(s)+len(t)) - distance[row][col]) / (len(s)+len(t))\n",
137 | " \n",
138 | " return Ratio\n",
139 | " \n",
140 | " else:\n",
141 | " \n",
142 | " return \"The strings are {} Distance away\".format(distance[row][col])"
143 | ],
144 | "execution_count": null,
145 | "outputs": []
146 | },
147 | {
148 | "cell_type": "markdown",
149 | "metadata": {
150 | "id": "wiKmRJSpzxAJ"
151 | },
152 | "source": [
153 | "## Output"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "metadata": {
159 | "id": "ZEMqfjalzxAJ"
160 | },
161 | "source": [
162 | "string_1='Delhi India'\n",
163 | "string_2='New Delhi India'"
164 | ],
165 | "execution_count": null,
166 | "outputs": []
167 | },
168 | {
169 | "cell_type": "code",
170 | "metadata": {
171 | "id": "y7zo0DJ-zxAJ",
172 | "outputId": "5010685b-f800-438e-8db8-6102f42b5234"
173 | },
174 | "source": [
175 | "levenshtein_ratio_and_distance(string_1, string_2, ratio_calc = False)"
176 | ],
177 | "execution_count": null,
178 | "outputs": [
179 | {
180 | "output_type": "execute_result",
181 | "data": {
182 | "text/plain": [
183 | "'The strings are 4 Distance away'"
184 | ]
185 | },
186 | "metadata": {
187 | "tags": []
188 | },
189 | "execution_count": 21
190 | }
191 | ]
192 | },
193 | {
194 | "cell_type": "markdown",
195 | "metadata": {
196 | "id": "G8mHgshLzxAK"
197 | },
198 | "source": [
199 | "*****"
200 | ]
201 | }
202 | ]
203 | }
--------------------------------------------------------------------------------
/Mutable immutable concept .ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "colab_type": "text",
7 | "id": "view-in-github"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": 1,
16 | "metadata": {
17 | "id": "jukVXl3FC82W"
18 | },
19 | "outputs": [],
20 | "source": [
21 | "# Mutable -Immutable objects python"
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "1. Difference between mutable vs immutable in Python?\n",
29 | "\n",
30 | "\n",
31 | "mutable - State of the object can be modified after it is created. They are not thread safe.Mutable classes are not final.\n",
32 | "\n",
33 | "immutable - State of the object can’t be modified once it is created.They are thread safe\n",
34 | "It is important to make the class final before creating an immutable object.\n",
35 | "\n",
36 | "2. What are the mutable and immutable data types in Python?\n",
37 | "\n",
38 | "Some mutable data types in Python are:\n",
39 | "list, dictionary, set, user-defined classes.\n",
40 | "\n",
41 | "Some immutable data types are: \n",
42 | "int, float, decimal, bool, string, tuple, range.\n",
43 | "\n",
44 | "3. Are lists mutable in Python?\n",
45 | "\n",
46 | "Lists in Python are mutable data types as the elements of the list can be modified, individual elements can be replaced, and the order of elements can be changed even after the list has been created.\n",
47 | "(Examples related to lists have been discussed earlier in this blog.)\n",
48 | "\n",
49 | "4. Why are tuples called immutable types?\n",
50 | "\n",
51 | "Tuple and list data structures are very similar, but one big difference between the data types is that lists are mutable, whereas tuples are immutable. The reason for the tuple’s immutability is that once the elements are added to the tuple and the tuple has been created; it remains unchanged.\n",
52 | "\n",
53 | "A programmer would always prefer building a code that can be reused instead of making the whole data object again. Still, even though tuples are immutable, like lists, they can contain any Python object, including mutable objects.\n",
54 | "\n",
55 | "5. Are sets mutable in Python?\n",
56 | "\n",
57 | "A set is an iterable unordered collection of data type which can be used to perform mathematical operations (like union, intersection, difference etc.). Every element in a set is unique and immutable, i.e. no duplicate values should be there, and the values can’t be changed. However, we can add or remove items from the set as the set itself is mutable.\n",
58 | "\n",
59 | "6. Are strings mutable in Python?\n",
60 | "\n",
61 | "Strings are not mutable in Python. Strings are a immutable data types which means that its value cannot be updated."
62 | ]
63 | },
64 | {
65 | "cell_type": "markdown",
66 | "metadata": {},
67 | "source": [
68 | "#### @ Integer in between range -5 to 256 and character between -4 -255"
69 | ]
70 | },
71 | {
72 | "cell_type": "code",
73 | "execution_count": 23,
74 | "metadata": {},
75 | "outputs": [],
76 | "source": [
77 | "# Check first"
78 | ]
79 | },
80 | {
81 | "cell_type": "code",
82 | "execution_count": 107,
83 | "metadata": {},
84 | "outputs": [
85 | {
86 | "data": {
87 | "text/plain": [
88 | "(140299941803464, 140299941804936)"
89 | ]
90 | },
91 | "execution_count": 107,
92 | "metadata": {},
93 | "output_type": "execute_result"
94 | }
95 | ],
96 | "source": [
97 | "a=['ashish','naveen']\n",
98 | "b=['ashish','naveen']\n",
99 | "id(a), id(b)"
100 | ]
101 | },
102 | {
103 | "cell_type": "code",
104 | "execution_count": 80,
105 | "metadata": {},
106 | "outputs": [
107 | {
108 | "data": {
109 | "text/plain": [
110 | "(140299679571656, 140299133426632)"
111 | ]
112 | },
113 | "execution_count": 80,
114 | "metadata": {},
115 | "output_type": "execute_result"
116 | }
117 | ],
118 | "source": [
119 | "a=[]\n",
120 | "b=[]\n",
121 | "id(a),id(b)"
122 | ]
123 | },
124 | {
125 | "cell_type": "code",
126 | "execution_count": 61,
127 | "metadata": {},
128 | "outputs": [],
129 | "source": [
130 | "#ch3eck second"
131 | ]
132 | },
133 | {
134 | "cell_type": "code",
135 | "execution_count": 120,
136 | "metadata": {},
137 | "outputs": [
138 | {
139 | "data": {
140 | "text/plain": [
141 | "(140299133378696, 140299133380936)"
142 | ]
143 | },
144 | "execution_count": 120,
145 | "metadata": {},
146 | "output_type": "execute_result"
147 | }
148 | ],
149 | "source": [
150 | "a=('ashish','naveen')\n",
151 | "b=('ashish','naveen')\n",
152 | "id(a), id(b)"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": 121,
158 | "metadata": {},
159 | "outputs": [
160 | {
161 | "data": {
162 | "text/plain": [
163 | "(140299416952088, 140299416952088)"
164 | ]
165 | },
166 | "execution_count": 121,
167 | "metadata": {},
168 | "output_type": "execute_result"
169 | }
170 | ],
171 | "source": [
172 | "a=(\"one\")\n",
173 | "b=(\"one\")\n",
174 | "id(a), id(b)"
175 | ]
176 | },
177 | {
178 | "cell_type": "code",
179 | "execution_count": 81,
180 | "metadata": {},
181 | "outputs": [
182 | {
183 | "data": {
184 | "text/plain": [
185 | "(140299133423568, 140299133423536)"
186 | ]
187 | },
188 | "execution_count": 81,
189 | "metadata": {},
190 | "output_type": "execute_result"
191 | }
192 | ],
193 | "source": [
194 | "a=(3333333)\n",
195 | "b=(3333333)\n",
196 | "id(a), id(b)"
197 | ]
198 | },
199 | {
200 | "cell_type": "code",
201 | "execution_count": 64,
202 | "metadata": {},
203 | "outputs": [
204 | {
205 | "data": {
206 | "text/plain": [
207 | "True"
208 | ]
209 | },
210 | "execution_count": 64,
211 | "metadata": {},
212 | "output_type": "execute_result"
213 | }
214 | ],
215 | "source": [
216 | "a=()\n",
217 | "b=()\n",
218 | "id(a)==id(b)"
219 | ]
220 | },
221 | {
222 | "cell_type": "code",
223 | "execution_count": 65,
224 | "metadata": {},
225 | "outputs": [
226 | {
227 | "data": {
228 | "text/plain": [
229 | "(140299404197592, 140299404197592)"
230 | ]
231 | },
232 | "execution_count": 65,
233 | "metadata": {},
234 | "output_type": "execute_result"
235 | }
236 | ],
237 | "source": [
238 | "a='ashish'\n",
239 | "b='ashish'\n",
240 | "id(a), id(b)"
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": 67,
246 | "metadata": {},
247 | "outputs": [
248 | {
249 | "data": {
250 | "text/plain": [
251 | "True"
252 | ]
253 | },
254 | "execution_count": 67,
255 | "metadata": {},
256 | "output_type": "execute_result"
257 | }
258 | ],
259 | "source": [
260 | "id(a)==id(b) #bcz a and b are immutable strings "
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": 68,
266 | "metadata": {},
267 | "outputs": [],
268 | "source": [
269 | "#concept --"
270 | ]
271 | },
272 | {
273 | "cell_type": "code",
274 | "execution_count": 122,
275 | "metadata": {},
276 | "outputs": [
277 | {
278 | "data": {
279 | "text/plain": [
280 | "(140299416973744, 140299416975280)"
281 | ]
282 | },
283 | "execution_count": 122,
284 | "metadata": {},
285 | "output_type": "execute_result"
286 | }
287 | ],
288 | "source": [
289 | "a=\"ashish 1\"\n",
290 | "b=\"ashish 1\"\n",
291 | "id(a), id(b)"
292 | ]
293 | },
294 | {
295 | "cell_type": "code",
296 | "execution_count": 123,
297 | "metadata": {},
298 | "outputs": [
299 | {
300 | "data": {
301 | "text/plain": [
302 | "False"
303 | ]
304 | },
305 | "execution_count": 123,
306 | "metadata": {},
307 | "output_type": "execute_result"
308 | }
309 | ],
310 | "source": [
311 | "id(a)==id(b) # ?? false y "
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 124,
317 | "metadata": {},
318 | "outputs": [],
319 | "source": [
320 | "#Now if we want to store in same location"
321 | ]
322 | },
323 | {
324 | "cell_type": "code",
325 | "execution_count": 125,
326 | "metadata": {},
327 | "outputs": [],
328 | "source": [
329 | "from sys import intern"
330 | ]
331 | },
332 | {
333 | "cell_type": "code",
334 | "execution_count": 126,
335 | "metadata": {},
336 | "outputs": [],
337 | "source": [
338 | "c = intern(a)\n",
339 | "d = intern(b)"
340 | ]
341 | },
342 | {
343 | "cell_type": "code",
344 | "execution_count": 127,
345 | "metadata": {},
346 | "outputs": [
347 | {
348 | "data": {
349 | "text/plain": [
350 | "(140299941804784, 140299941804784)"
351 | ]
352 | },
353 | "execution_count": 127,
354 | "metadata": {},
355 | "output_type": "execute_result"
356 | }
357 | ],
358 | "source": [
359 | "id(c), id(d)"
360 | ]
361 | },
362 | {
363 | "cell_type": "code",
364 | "execution_count": null,
365 | "metadata": {},
366 | "outputs": [],
367 | "source": []
368 | },
369 | {
370 | "cell_type": "code",
371 | "execution_count": null,
372 | "metadata": {},
373 | "outputs": [],
374 | "source": []
375 | }
376 | ],
377 | "metadata": {
378 | "colab": {
379 | "authorship_tag": "ABX9TyPWj9pfIRnMvdSMC/zChT7t",
380 | "collapsed_sections": [],
381 | "include_colab_link": true,
382 | "name": "others.ipynb",
383 | "private_outputs": true,
384 | "provenance": []
385 | },
386 | "kernelspec": {
387 | "display_name": "Python 3",
388 | "language": "python",
389 | "name": "python3"
390 | },
391 | "language_info": {
392 | "codemirror_mode": {
393 | "name": "ipython",
394 | "version": 3
395 | },
396 | "file_extension": ".py",
397 | "mimetype": "text/x-python",
398 | "name": "python",
399 | "nbconvert_exporter": "python",
400 | "pygments_lexer": "ipython3",
401 | "version": "3.6.7"
402 | }
403 | },
404 | "nbformat": 4,
405 | "nbformat_minor": 1
406 | }
407 |
--------------------------------------------------------------------------------
/NLP -Spacy Custom Entity Ruler.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# NLP- Spacy Custom Entity ruler Model to find Entity "
8 | ]
9 | },
10 | {
11 | "cell_type": "markdown",
12 | "metadata": {},
13 | "source": [
14 | "## Rule-based matching"
15 | ]
16 | },
17 | {
18 | "cell_type": "markdown",
19 | "metadata": {},
20 | "source": [
21 | "Load All lib"
22 | ]
23 | },
24 | {
25 | "cell_type": "code",
26 | "execution_count": 1,
27 | "metadata": {},
28 | "outputs": [],
29 | "source": [
30 | "import spacy\n",
31 | "import pandas as pd"
32 | ]
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | "## Spacy Default Model"
39 | ]
40 | },
41 | {
42 | "cell_type": "code",
43 | "execution_count": 2,
44 | "metadata": {},
45 | "outputs": [],
46 | "source": [
47 | "#pip install en_core_web_sm"
48 | ]
49 | },
50 | {
51 | "cell_type": "raw",
52 | "metadata": {},
53 | "source": [
54 | "# download default spacy model using\n",
55 | "python -m spacy download en_core_web_sm"
56 | ]
57 | },
58 | {
59 | "cell_type": "markdown",
60 | "metadata": {},
61 | "source": [
62 | "Load default spacy model"
63 | ]
64 | },
65 | {
66 | "cell_type": "code",
67 | "execution_count": 3,
68 | "metadata": {},
69 | "outputs": [],
70 | "source": [
71 | "nlp = spacy.load(\"en_core_web_sm\")"
72 | ]
73 | },
74 | {
75 | "cell_type": "markdown",
76 | "metadata": {},
77 | "source": [
78 | "pass any string"
79 | ]
80 | },
81 | {
82 | "cell_type": "code",
83 | "execution_count": 4,
84 | "metadata": {},
85 | "outputs": [],
86 | "source": [
87 | "string=\"India is a countrty and state is RAJASTHAN\""
88 | ]
89 | },
90 | {
91 | "cell_type": "markdown",
92 | "metadata": {},
93 | "source": [
94 | "pass string to obj nlp"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": 5,
100 | "metadata": {},
101 | "outputs": [],
102 | "source": [
103 | "nlp_data=nlp(string)"
104 | ]
105 | },
106 | {
107 | "cell_type": "markdown",
108 | "metadata": {},
109 | "source": [
110 | "Extract entity"
111 | ]
112 | },
113 | {
114 | "cell_type": "code",
115 | "execution_count": 6,
116 | "metadata": {},
117 | "outputs": [
118 | {
119 | "name": "stdout",
120 | "output_type": "stream",
121 | "text": [
122 | "GPE is:- India\n"
123 | ]
124 | }
125 | ],
126 | "source": [
127 | "for ent in nlp_data.ents:\n",
128 | " word,explain=ent.text,ent.label_\n",
129 | " print(explain,\"is:-\", word)"
130 | ]
131 | },
132 | {
133 | "cell_type": "markdown",
134 | "metadata": {},
135 | "source": [
136 | "** unable to get state in india whic is rajasthan \n",
137 | "*** for this we train our model , and make custom model .."
138 | ]
139 | },
140 | {
141 | "cell_type": "markdown",
142 | "metadata": {},
143 | "source": [
144 | "## Spacy Custom Model "
145 | ]
146 | },
147 | {
148 | "cell_type": "markdown",
149 | "metadata": {},
150 | "source": [
151 | "Load Data of States (state name )"
152 | ]
153 | },
154 | {
155 | "cell_type": "code",
156 | "execution_count": 7,
157 | "metadata": {},
158 | "outputs": [],
159 | "source": [
160 | "state_data=['MAHARASHTRA', 'MEGHALAYA', 'JHARKHAND', 'MANIPUR', 'UTTARANCHAL', 'MIZORAM', 'JAMMU & KASHMIR', 'ANDAMAN NICOBAR', 'BIHAR', 'SIKKIM', 'HIMACHAL PRADESH', 'ARUNACHAL PRADESH', 'CHANDIGARG', 'ASSAM', 'CHHATISGARH', 'DELHI', 'ANDHRA PRADESH', 'KARNATAKA', 'APS CIRCLE', 'TRIPURA', 'ARMY POSTAL SERVICE', 'NAGALAND', 'KERALA', 'RAJASTHAN', 'WEST BENGAL', 'GOA', 'HARYANA', 'TAMIL NADU', 'GUJARAT', 'CHANDIGARH', 'ORISSA', 'UTTAR PRADESH', 'PUNJAB', 'MADHYA PRADESH']"
161 | ]
162 | },
163 | {
164 | "cell_type": "markdown",
165 | "metadata": {},
166 | "source": [
167 | "Load data of Pin Code of india (pincode)"
168 | ]
169 | },
170 | {
171 | "cell_type": "code",
172 | "execution_count": 25,
173 | "metadata": {},
174 | "outputs": [],
175 | "source": [
176 | "pincode_data=['917504', '524289', '524290', '524291', '524292', '524293', '524294', '524295', '524296', '524297', '524298', '524299', '917511', '524301', '524302', '524303', '524304', '524305', '524306', '524307', '524308', '524309', '524310', '524311', '524312', '524313', '524314', '524315', '524316', '131101']"
177 | ]
178 | },
179 | {
180 | "cell_type": "markdown",
181 | "metadata": {},
182 | "source": [
183 | "Load a blank model"
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": 9,
189 | "metadata": {},
190 | "outputs": [],
191 | "source": [
192 | "nlp=spacy.blank(\"en\")"
193 | ]
194 | },
195 | {
196 | "cell_type": "markdown",
197 | "metadata": {},
198 | "source": [
199 | "create pipe "
200 | ]
201 | },
202 | {
203 | "cell_type": "code",
204 | "execution_count": 10,
205 | "metadata": {},
206 | "outputs": [],
207 | "source": [
208 | "ruler = nlp.create_pipe(\"entity_ruler\")"
209 | ]
210 | },
211 | {
212 | "cell_type": "markdown",
213 | "metadata": {},
214 | "source": [
215 | "And add pattern to ruler"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": 11,
221 | "metadata": {},
222 | "outputs": [],
223 | "source": [
224 | "ruler.add_patterns([{\"label\": \"STATE\", \"pattern\": state} for state in state_data ])\n",
225 | "ruler.add_patterns([{\"label\": \"PINCODE\", \"pattern\": pincode} for pincode in pincode_data])"
226 | ]
227 | },
228 | {
229 | "cell_type": "markdown",
230 | "metadata": {},
231 | "source": [
232 | "Add Pipe to nlp"
233 | ]
234 | },
235 | {
236 | "cell_type": "code",
237 | "execution_count": 12,
238 | "metadata": {},
239 | "outputs": [],
240 | "source": [
241 | "nlp.add_pipe(ruler)"
242 | ]
243 | },
244 | {
245 | "cell_type": "markdown",
246 | "metadata": {},
247 | "source": [
248 | "Save model "
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": 13,
254 | "metadata": {},
255 | "outputs": [],
256 | "source": [
257 | "nlp.to_disk(\"entity_ruler\")"
258 | ]
259 | },
260 | {
261 | "cell_type": "markdown",
262 | "metadata": {},
263 | "source": [
264 | "Load Model "
265 | ]
266 | },
267 | {
268 | "cell_type": "code",
269 | "execution_count": 14,
270 | "metadata": {},
271 | "outputs": [],
272 | "source": [
273 | "nlp=spacy.load(\"entity_ruler\")"
274 | ]
275 | },
276 | {
277 | "cell_type": "markdown",
278 | "metadata": {},
279 | "source": [
280 | "### Check Model working"
281 | ]
282 | },
283 | {
284 | "cell_type": "markdown",
285 | "metadata": {},
286 | "source": [
287 | "String"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": 19,
293 | "metadata": {},
294 | "outputs": [],
295 | "source": [
296 | "string=\"Tej .Parkash . JANAKIRAMAN AHRPR5043C NEW NO. 32, 3RD MAIN ROAD, 75 FLAT NO 7,2ND FLOOR, R.A PURAM RAJASTHAN \\nINDIA\""
297 | ]
298 | },
299 | {
300 | "cell_type": "markdown",
301 | "metadata": {},
302 | "source": [
303 | "Pass \"String\" to model"
304 | ]
305 | },
306 | {
307 | "cell_type": "code",
308 | "execution_count": 20,
309 | "metadata": {},
310 | "outputs": [],
311 | "source": [
312 | "data_nlp=nlp(string)"
313 | ]
314 | },
315 | {
316 | "cell_type": "markdown",
317 | "metadata": {},
318 | "source": [
319 | "Get Data"
320 | ]
321 | },
322 | {
323 | "cell_type": "code",
324 | "execution_count": 21,
325 | "metadata": {},
326 | "outputs": [
327 | {
328 | "name": "stdout",
329 | "output_type": "stream",
330 | "text": [
331 | "PINCODE is:- RAJASTHAN\n"
332 | ]
333 | }
334 | ],
335 | "source": [
336 | "for ent in data_nlp.ents:\n",
337 | " word,explain=ent.text,ent.label_\n",
338 | " print(explain,\"is:-\", word)"
339 | ]
340 | },
341 | {
342 | "cell_type": "markdown",
343 | "metadata": {},
344 | "source": [
345 | "** Now we Have rajasthan as state"
346 | ]
347 | },
348 | {
349 | "cell_type": "markdown",
350 | "metadata": {},
351 | "source": [
352 | "*****"
353 | ]
354 | }
355 | ],
356 | "metadata": {
357 | "kernelspec": {
358 | "display_name": "Python 3",
359 | "language": "python",
360 | "name": "python3"
361 | },
362 | "language_info": {
363 | "codemirror_mode": {
364 | "name": "ipython",
365 | "version": 3
366 | },
367 | "file_extension": ".py",
368 | "mimetype": "text/x-python",
369 | "name": "python",
370 | "nbconvert_exporter": "python",
371 | "pygments_lexer": "ipython3",
372 | "version": "3.7.6"
373 | }
374 | },
375 | "nbformat": 4,
376 | "nbformat_minor": 4
377 | }
378 |
--------------------------------------------------------------------------------
/Solutions.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {
6 | "id": "view-in-github",
7 | "colab_type": "text"
8 | },
9 | "source": [
10 | "
"
11 | ]
12 | },
13 | {
14 | "cell_type": "code",
15 | "execution_count": null,
16 | "id": "333d5883",
17 | "metadata": {
18 | "id": "333d5883"
19 | },
20 | "outputs": [],
21 | "source": [
22 | "#balanced paranthesis"
23 | ]
24 | },
25 | {
26 | "cell_type": "code",
27 | "execution_count": null,
28 | "id": "1d2b0878",
29 | "metadata": {
30 | "id": "1d2b0878"
31 | },
32 | "outputs": [],
33 | "source": [
34 | "# Python3 code to Check for\n",
35 | "# balanced parentheses in an expression\n",
36 | "\n",
37 | "#easy\n",
38 | "def balanced(string):\n",
39 | "\n",
40 | " # using 2 loops\n",
41 | "\n",
42 | " brackets = ['()', '{}', '[]']\n",
43 | " for i in string:\n",
44 | " for x in brackets:\n",
45 | " if x in string:\n",
46 | " string =string.replace(x,'').strip()\n",
47 | "\n",
48 | " if string=='':\n",
49 | " return \"balanced\"\n",
50 | " return \"unbalanced\"\n",
51 | "\n",
52 | "\n",
53 | "#shortcut\n",
54 | "def balanced2(string):\n",
55 | "\n",
56 | " # using one loop\n",
57 | "\n",
58 | " brackets = {'(':\"()\", '{':'{}', '[':'[]',\n",
59 | " ')':\"()\", '}':'{}', ']':'[]'}\n",
60 | "\n",
61 | " for i in string:\n",
62 | " if i in brackets.keys():\n",
63 | " value = brackets.get(i)\n",
64 | " string = string.replace(value,'').strip()\n",
65 | "\n",
66 | " if string=='':\n",
67 | " return \"balanced\"\n",
68 | " return \"unbalanced\"\n",
69 | "\n",
70 | "\n",
71 | "#mostly used\n",
72 | "def balanced3(myStr):\n",
73 | "\n",
74 | " #using stack\n",
75 | "\n",
76 | " open_list = [\"[\",\"{\",\"(\"]\n",
77 | " close_list = [\"]\",\"}\",\")\"]\n",
78 | " stack =[]\n",
79 | "\n",
80 | " for symbl in myStr:\n",
81 | " if symbl in open_list:\n",
82 | " # if available in open list\n",
83 | " stack.append(symbl)\n",
84 | " else:\n",
85 | " # if avbailable in close list\n",
86 | " index= close_list.index(symbl) # to find the opposite of symbol stored in [-1] loc of stack\n",
87 | " match_symbol = open_list[index]\n",
88 | " if match_symbol==stack[-1]:\n",
89 | " stack.pop()\n",
90 | " else:\n",
91 | " stack.append(symbl)\n",
92 | "\n",
93 | " if len(stack)!=0:\n",
94 | " return \"Unbalanced\"\n",
95 | " return \"balanced\""
96 | ]
97 | },
98 | {
99 | "cell_type": "code",
100 | "source": [
101 | "balanced(\"[(]\")"
102 | ],
103 | "metadata": {
104 | "id": "tZGQ94Kahnr4",
105 | "outputId": "b66599b2-d505-4534-a7e9-b070781b142a",
106 | "colab": {
107 | "base_uri": "https://localhost:8080/",
108 | "height": 36
109 | }
110 | },
111 | "id": "tZGQ94Kahnr4",
112 | "execution_count": null,
113 | "outputs": [
114 | {
115 | "output_type": "execute_result",
116 | "data": {
117 | "text/plain": [
118 | "'Unbalanced'"
119 | ],
120 | "application/vnd.google.colaboratory.intrinsic+json": {
121 | "type": "string"
122 | }
123 | },
124 | "metadata": {},
125 | "execution_count": 4
126 | }
127 | ]
128 | },
129 | {
130 | "cell_type": "code",
131 | "execution_count": null,
132 | "id": "082ca6fe",
133 | "metadata": {
134 | "id": "082ca6fe"
135 | },
136 | "outputs": [],
137 | "source": [
138 | "#longest string present"
139 | ]
140 | },
141 | {
142 | "cell_type": "code",
143 | "execution_count": null,
144 | "id": "83498cff",
145 | "metadata": {
146 | "id": "83498cff"
147 | },
148 | "outputs": [],
149 | "source": [
150 | "def longeststringpresent(strs):\n",
151 | " \"\"\"\n",
152 | " :type strs: List[str]\n",
153 | " :rtype: str\n",
154 | " \"\"\"\n",
155 | " arr = strs\n",
156 | "\n",
157 | " arr = list(set(arr))\n",
158 | " arr.sort(key=len)\n",
159 | "\n",
160 | " s = arr[0]\n",
161 | " l = len(s)\n",
162 | "\n",
163 | " maxx=[]\n",
164 | " for i in range(l):\n",
165 | " for j in range(i + 1, l + 1):\n",
166 | " stem = s[i:j]\n",
167 | " maxx.append(stem)\n",
168 | "\n",
169 | " maxx = list(set(maxx))\n",
170 | " maxx.sort(key=len)\n",
171 | " maxx_final=[]\n",
172 | "\n",
173 | " for x in maxx:\n",
174 | " for y in arr:\n",
175 | " if x in y:\n",
176 | " maxx_final.append(x)\n",
177 | "\n",
178 | " fi = list(set([x for x in maxx_final if maxx_final.count(x) == len(arr)]))\n",
179 | " if fi:\n",
180 | "\n",
181 | " fi.sort(key=len)\n",
182 | " if len(fi)==2:\n",
183 | " if len(fi[-1]) ==len(fi[0]):\n",
184 | " return fi[0]\n",
185 | "\n",
186 | " fi.sort(key=len)\n",
187 | " return fi[-1]\n",
188 | " else:\n",
189 | " return \"\""
190 | ]
191 | },
192 | {
193 | "cell_type": "code",
194 | "execution_count": null,
195 | "id": "e6c7a3b7",
196 | "metadata": {
197 | "id": "e6c7a3b7"
198 | },
199 | "outputs": [],
200 | "source": [
201 | "#longest common prefeix"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": null,
207 | "id": "497532bb",
208 | "metadata": {
209 | "id": "497532bb"
210 | },
211 | "outputs": [],
212 | "source": [
213 | "def longestCommonPrefix(strs):\n",
214 | " \"\"\"\n",
215 | " :type strs: List[str]\n",
216 | " :rtype: str\n",
217 | " \"\"\"\n",
218 | " prefix=[]\n",
219 | " num = len(strs)\n",
220 | " for x in zip(*strs):\n",
221 | " if len(set(x)) == 1:\n",
222 | " prefix.append(x[0])\n",
223 | " else:\n",
224 | " break\n",
225 | " return \"\".join(prefix)\n",
226 | "\n"
227 | ]
228 | },
229 | {
230 | "cell_type": "code",
231 | "execution_count": null,
232 | "id": "751afa19",
233 | "metadata": {
234 | "id": "751afa19"
235 | },
236 | "outputs": [],
237 | "source": [
238 | "#sum of two numbers"
239 | ]
240 | },
241 | {
242 | "cell_type": "code",
243 | "execution_count": null,
244 | "id": "5fbfcf97",
245 | "metadata": {
246 | "id": "5fbfcf97",
247 | "outputId": "9fda76ad-9170-4856-df46-c20791937224"
248 | },
249 | "outputs": [
250 | {
251 | "name": "stdout",
252 | "output_type": "stream",
253 | "text": [
254 | "1 2\n"
255 | ]
256 | }
257 | ],
258 | "source": [
259 | "\n",
260 | "nums = [2,3,3]\n",
261 | "target =6 # op shoule be 1,2 bxz 3+3=6\n",
262 | "\n",
263 | "\n",
264 | "dic={}\n",
265 | "for i, val in enumerate(nums):\n",
266 | " op = target-val\n",
267 | " if op not in dic:\n",
268 | " dic[val]=i\n",
269 | " else:\n",
270 | " print(dic[op], i )"
271 | ]
272 | },
273 | {
274 | "cell_type": "code",
275 | "execution_count": 1,
276 | "id": "cfde295c",
277 | "metadata": {
278 | "id": "cfde295c"
279 | },
280 | "outputs": [],
281 | "source": [
282 | "# find missing number"
283 | ]
284 | },
285 | {
286 | "cell_type": "code",
287 | "source": [
288 | "def find_missing_number(nums):\n",
289 | " n = len(nums) + 1 # The total number of integers if none were missing\n",
290 | " expected_sum = n * (n + 1) // 2 # Sum of the first n natural numbers\n",
291 | " actual_sum = sum(nums) # Sum of numbers in the list\n",
292 | " return expected_sum - actual_sum # The difference is the missing number\n",
293 | "\n",
294 | "# Example usage :\n",
295 | "\n",
296 | "nums = [1, 2, 4, 5, 6] # Example list\n",
297 | "missing_number = find_missing_number(nums)\n",
298 | "print(f\"The missing number is: {missing_number}\")"
299 | ],
300 | "metadata": {
301 | "colab": {
302 | "base_uri": "https://localhost:8080/"
303 | },
304 | "id": "PZGdnk6pPsBV",
305 | "outputId": "8922878e-c52c-4c8b-9886-c49ee0648e4d"
306 | },
307 | "id": "PZGdnk6pPsBV",
308 | "execution_count": 2,
309 | "outputs": [
310 | {
311 | "output_type": "stream",
312 | "name": "stdout",
313 | "text": [
314 | "The missing number is: 3\n"
315 | ]
316 | }
317 | ]
318 | },
319 | {
320 | "cell_type": "code",
321 | "source": [
322 | "# find target"
323 | ],
324 | "metadata": {
325 | "id": "UlKc2JVHPsD3"
326 | },
327 | "id": "UlKc2JVHPsD3",
328 | "execution_count": null,
329 | "outputs": []
330 | },
331 | {
332 | "cell_type": "code",
333 | "source": [
334 | "class Solution(object):\n",
335 | " def twoSum(self, nums, target):\n",
336 | " \"\"\"\n",
337 | " :type nums: List[int]\n",
338 | " :type target: int\n",
339 | " :rtype: List[int]\n",
340 | " \"\"\"\n",
341 | " dict_map = {}\n",
342 | " for key, val in enumerate(nums):\n",
343 | " output = target - val\n",
344 | " if output not in dict_map:\n",
345 | " dict_map[val]=key\n",
346 | " else:\n",
347 | " return dict_map[output],key\n"
348 | ],
349 | "metadata": {
350 | "id": "AyfZw-vaPzpS"
351 | },
352 | "id": "AyfZw-vaPzpS",
353 | "execution_count": null,
354 | "outputs": []
355 | },
356 | {
357 | "cell_type": "code",
358 | "source": [
359 | "#Longest Substring Without Repeating Characters"
360 | ],
361 | "metadata": {
362 | "id": "vo7WbyLBPztt"
363 | },
364 | "id": "vo7WbyLBPztt",
365 | "execution_count": 3,
366 | "outputs": []
367 | },
368 | {
369 | "cell_type": "code",
370 | "source": [
371 | "class Solution(object):\n",
372 | " def lengthOfLongestSubstring(self, s):\n",
373 | " \"\"\"\n",
374 | " :type s: str\n",
375 | " :rtype: int\n",
376 | " \"\"\"\n",
377 | " a= s\n",
378 | " data=[]\n",
379 | " for i in range(len(a)):\n",
380 | " for j in range(len(a)+2):\n",
381 | " op = a[i:j]\n",
382 | " if op:\n",
383 | " if len(op)>1:\n",
384 | " check=[]\n",
385 | " for x in op:\n",
386 | " if x not in check:\n",
387 | " check.append(x)\n",
388 | " else:\n",
389 | " break\n",
390 | " else:\n",
391 | " data.append(op)\n",
392 | " else:\n",
393 | " data.append(op)\n",
394 | " data.sort(key=len)\n",
395 | " if data:\n",
396 | " return len(data[-1])\n",
397 | " return 0"
398 | ],
399 | "metadata": {
400 | "id": "uH1Bsx5cP6Wg"
401 | },
402 | "id": "uH1Bsx5cP6Wg",
403 | "execution_count": 4,
404 | "outputs": []
405 | },
406 | {
407 | "cell_type": "code",
408 | "source": [
409 | "#Letter Combinations of a Phone Number"
410 | ],
411 | "metadata": {
412 | "id": "X7L41ur6P6Yx"
413 | },
414 | "id": "X7L41ur6P6Yx",
415 | "execution_count": null,
416 | "outputs": []
417 | },
418 | {
419 | "cell_type": "code",
420 | "source": [
421 | "class Solution(object):\n",
422 | " def letterCombinations(self, digits):\n",
423 | " \"\"\"\n",
424 | " :type digits: str\n",
425 | " :rtype: List[str]\n",
426 | " \"\"\"\n",
427 | " keyboard = {\n",
428 | " \"2\": \"abc\",\n",
429 | " \"3\": \"def\",\n",
430 | " \"4\": \"ghi\",\n",
431 | " \"5\": \"jkl\",\n",
432 | " \"6\": \"mno\",\n",
433 | " \"7\": \"pqrs\",\n",
434 | " \"8\": \"tuv\",\n",
435 | " \"9\": \"wxyz\"\n",
436 | " }\n",
437 | " from itertools import product\n",
438 | " letters = [keyboard.get(x) for x in list(digits)]\n",
439 | " combinations = [''.join(x) for x in list(product(*letters))]\n",
440 | " if combinations==[\"\"]:\n",
441 | " return []\n",
442 | " return combinations\n"
443 | ],
444 | "metadata": {
445 | "id": "6xVlKPiqQAOO"
446 | },
447 | "id": "6xVlKPiqQAOO",
448 | "execution_count": null,
449 | "outputs": []
450 | },
451 | {
452 | "cell_type": "code",
453 | "source": [
454 | "# valid anagram"
455 | ],
456 | "metadata": {
457 | "id": "c1uiBKFlQRgf"
458 | },
459 | "id": "c1uiBKFlQRgf",
460 | "execution_count": null,
461 | "outputs": []
462 | },
463 | {
464 | "cell_type": "code",
465 | "source": [
466 | "# valid anagram\n",
467 | "class Solution(object):\n",
468 | " def isAnagram(self, s, t):\n",
469 | " \"\"\"\n",
470 | " :type s: str\n",
471 | " :type t: str\n",
472 | " :rtype: bool\n",
473 | " \"\"\"\n",
474 | " s = list(s)\n",
475 | " s.sort()\n",
476 | " t= list(t)\n",
477 | " t.sort()\n",
478 | " return s==t"
479 | ],
480 | "metadata": {
481 | "id": "lBdtg88YQDh-"
482 | },
483 | "id": "lBdtg88YQDh-",
484 | "execution_count": null,
485 | "outputs": []
486 | },
487 | {
488 | "cell_type": "code",
489 | "source": [
490 | "# show the best days to buy and sell stock"
491 | ],
492 | "metadata": {
493 | "id": "p-T2vI99QKKt"
494 | },
495 | "id": "p-T2vI99QKKt",
496 | "execution_count": null,
497 | "outputs": []
498 | },
499 | {
500 | "cell_type": "code",
501 | "source": [
502 | "price_list = [7, 1, 2, 4, 5, 3] # Renamed to avoid confusion with the price variable\n",
503 | "price = 0\n",
504 | "dicts = {}\n",
505 | "\n",
506 | "for i in range(len(price_list)):\n",
507 | " for j in range(i + 1, len(price_list)):\n",
508 | " if price_list[j] - price_list[i] > price:\n",
509 | " price = price_list[j] - price_list[i]\n",
510 | " dicts['buy'] = i\n",
511 | " dicts['sell'] = j\n",
512 | "\n",
513 | "dicts # This will show the best days to buy and sell\n"
514 | ],
515 | "metadata": {
516 | "id": "4PljkBRYQE6O"
517 | },
518 | "id": "4PljkBRYQE6O",
519 | "execution_count": null,
520 | "outputs": []
521 | },
522 | {
523 | "cell_type": "code",
524 | "source": [
525 | "#Permutation"
526 | ],
527 | "metadata": {
528 | "id": "gIuuav61QPXh"
529 | },
530 | "id": "gIuuav61QPXh",
531 | "execution_count": null,
532 | "outputs": []
533 | },
534 | {
535 | "cell_type": "code",
536 | "source": [
537 | "def permute(nums):\n",
538 | " result = []\n",
539 | "\n",
540 | " def backtrack(path, options):\n",
541 | " if not options:\n",
542 | " result.append(path)\n",
543 | " return\n",
544 | "\n",
545 | " for i in range(len(options)):\n",
546 | " backtrack(path + [options[i]], options[:i] + options[i+1:])\n",
547 | "\n",
548 | " backtrack([], nums)\n",
549 | " return result\n",
550 | "\n",
551 | "nums = [1, 2, 3]\n",
552 | "print(permute(nums))"
553 | ],
554 | "metadata": {
555 | "id": "euHPI7R4QE8j"
556 | },
557 | "id": "euHPI7R4QE8j",
558 | "execution_count": null,
559 | "outputs": []
560 | }
561 | ],
562 | "metadata": {
563 | "kernelspec": {
564 | "display_name": "Python 3",
565 | "language": "python",
566 | "name": "python3"
567 | },
568 | "language_info": {
569 | "codemirror_mode": {
570 | "name": "ipython",
571 | "version": 3
572 | },
573 | "file_extension": ".py",
574 | "mimetype": "text/x-python",
575 | "name": "python",
576 | "nbconvert_exporter": "python",
577 | "pygments_lexer": "ipython3",
578 | "version": "3.6.7"
579 | },
580 | "colab": {
581 | "provenance": [],
582 | "include_colab_link": true
583 | }
584 | },
585 | "nbformat": 4,
586 | "nbformat_minor": 5
587 | }
--------------------------------------------------------------------------------
/Speech Recognition Libraries.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": null,
6 | "metadata": {},
7 | "outputs": [],
8 | "source": [
9 | "#pip install SpeechRecognition pydub"
10 | ]
11 | },
12 | {
13 | "cell_type": "markdown",
14 | "metadata": {},
15 | "source": [
16 | "# Multithreading "
17 | ]
18 | },
19 | {
20 | "cell_type": "code",
21 | "execution_count": null,
22 | "metadata": {},
23 | "outputs": [],
24 | "source": [
25 | "# # Python program to illustrate the concept \n",
26 | "# # of threading \n",
27 | "# import threading \n",
28 | "# import os \n",
29 | "# import time\n",
30 | "\n",
31 | "# def task1(): \n",
32 | "# import pyaudio\n",
33 | "# import speech_recognition as sr\n",
34 | "# import speech_recognition as sr\n",
35 | "# r = sr.Recognizer()\n",
36 | "# with sr.Microphone() as source:\n",
37 | "# print(\"Microphone On:\") \n",
38 | "# wave_obj = sa.WaveObject.from_wave_file(\"two.wav\")\n",
39 | "# print(\"End speaking\")\n",
40 | "# play_obj = wave_obj.play()\n",
41 | "# play_obj.wait_done()\n",
42 | "# audio = r.listen(source)\n",
43 | "# print(\"Done\")\n",
44 | "# text = r.recognize_google(audio)\n",
45 | "# print(\"Microphone OFF : {}\".format(text))\n",
46 | "\n",
47 | " \n",
48 | " \n",
49 | " \n",
50 | "# def task2(): \n",
51 | "# import simpleaudio as sa\n",
52 | "# print(\"Start speaking\")\n",
53 | "# wave_obj = sa.WaveObject.from_wave_file(\"two.wav\")\n",
54 | "# print(\"End speaking\")\n",
55 | "# play_obj = wave_obj.play()\n",
56 | "# play_obj.wait_done()\n",
57 | " \n",
58 | "# if __name__ == \"__main__\": \n",
59 | "# # print ID of current process \n",
60 | "# # creating threads \n",
61 | "# t1 = threading.Thread(target=task1, name='t1') \n",
62 | "# t2 = threading.Thread(target=task2, name='t2') \n",
63 | " \n",
64 | "# # starting threads \n",
65 | "# t1.start() \n",
66 | "# t2.start()"
67 | ]
68 | },
69 | {
70 | "cell_type": "markdown",
71 | "metadata": {},
72 | "source": [
73 | "*******"
74 | ]
75 | },
76 | {
77 | "cell_type": "markdown",
78 | "metadata": {},
79 | "source": [
80 | "# Record sound"
81 | ]
82 | },
83 | {
84 | "cell_type": "code",
85 | "execution_count": 49,
86 | "metadata": {},
87 | "outputs": [
88 | {
89 | "name": "stdout",
90 | "output_type": "stream",
91 | "text": [
92 | "Recording...\n",
93 | "Finished recording.\n"
94 | ]
95 | }
96 | ],
97 | "source": [
98 | "import pyaudio\n",
99 | "import wave\n",
100 | "\n",
101 | "# the file name output you want to record into\n",
102 | "filename = \"recorded.wav\"\n",
103 | "# set the chunk size of 1024 samples\n",
104 | "chunk = 1024\n",
105 | "# sample format\n",
106 | "FORMAT = pyaudio.paInt16\n",
107 | "# mono, change to 2 if you want stereo\n",
108 | "channels = 1\n",
109 | "# 44100 samples per second\n",
110 | "sample_rate = 44100\n",
111 | "record_seconds = 5\n",
112 | "# initialize PyAudio object\n",
113 | "p = pyaudio.PyAudio()\n",
114 | "# open stream object as input & output\n",
115 | "stream = p.open(format=FORMAT,\n",
116 | " channels=channels,\n",
117 | " rate=sample_rate,\n",
118 | " input=True,\n",
119 | " output=True,\n",
120 | " frames_per_buffer=chunk)\n",
121 | "frames = []\n",
122 | "print(\"Recording...\")\n",
123 | "for i in range(int(44100 / chunk * record_seconds)):\n",
124 | " data = stream.read(chunk)\n",
125 | " # if you want to hear your voice while recording\n",
126 | " # stream.write(data)\n",
127 | " frames.append(data)\n",
128 | "print(\"Finished recording.\")\n",
129 | "# stop and close stream\n",
130 | "stream.stop_stream()\n",
131 | "stream.close()\n",
132 | "# terminate pyaudio object\n",
133 | "p.terminate()\n",
134 | "# save audio file\n",
135 | "# open the file in 'write bytes' mode\n",
136 | "wf = wave.open(filename, \"wb\")\n",
137 | "# set the channels\n",
138 | "wf.setnchannels(channels)\n",
139 | "# set the sample format\n",
140 | "wf.setsampwidth(p.get_sample_size(FORMAT))\n",
141 | "# set the sample rate\n",
142 | "wf.setframerate(sample_rate)\n",
143 | "# write the frames as bytes\n",
144 | "wf.writeframes(b\"\".join(frames))\n",
145 | "# close the file\n",
146 | "wf.close()\n",
147 | "\n",
148 | "# ##import simpleaudio as sa\n",
149 | "# wave_obj = sa.WaveObject.from_wave_file(\"recorded.wav\")\n",
150 | "# play_obj = wave_obj.play()\n",
151 | "# play_obj.wait_done()\n",
152 | "# print(\"Complete\")\n"
153 | ]
154 | },
155 | {
156 | "cell_type": "markdown",
157 | "metadata": {},
158 | "source": [
159 | "# Extract text from file saved (in local)"
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 50,
165 | "metadata": {},
166 | "outputs": [
167 | {
168 | "name": "stdout",
169 | "output_type": "stream",
170 | "text": [
171 | "Done!\n",
172 | "hello this is Ashish\n"
173 | ]
174 | }
175 | ],
176 | "source": [
177 | "import speech_recognition as sr\n",
178 | "r = sr.Recognizer()\n",
179 | "audio ='recorded.wav'\n",
180 | "with sr.AudioFile(audio) as source:\n",
181 | " audio = r.record(source)\n",
182 | " print ('Done!')\n",
183 | "try:\n",
184 | " text = r.recognize_google(audio)\n",
185 | " print (text)\n",
186 | "except Exception as e:\n",
187 | " print ('e')"
188 | ]
189 | },
190 | {
191 | "cell_type": "markdown",
192 | "metadata": {},
193 | "source": [
194 | "# Play recorded sound"
195 | ]
196 | },
197 | {
198 | "cell_type": "code",
199 | "execution_count": 51,
200 | "metadata": {},
201 | "outputs": [
202 | {
203 | "name": "stdout",
204 | "output_type": "stream",
205 | "text": [
206 | "Complete\n"
207 | ]
208 | }
209 | ],
210 | "source": [
211 | "import simpleaudio as sa\n",
212 | "wave_obj = sa.WaveObject.from_wave_file(\"recorded.wav\")\n",
213 | "play_obj = wave_obj.play()\n",
214 | "play_obj.wait_done()\n",
215 | "print(\"Complete\")"
216 | ]
217 | },
218 | {
219 | "cell_type": "markdown",
220 | "metadata": {},
221 | "source": [
222 | "# extract text from sound (mic)"
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": 7,
228 | "metadata": {},
229 | "outputs": [
230 | {
231 | "name": "stdout",
232 | "output_type": "stream",
233 | "text": [
234 | "Microphone On:\n",
235 | "Done\n",
236 | "Microphone OFF : ITC Rashi\n"
237 | ]
238 | }
239 | ],
240 | "source": [
241 | "'''\n",
242 | "pip install pipwin\n",
243 | "pipwin install pyaudio\n",
244 | "'''\n",
245 | "import pyaudio\n",
246 | "import speech_recognition as sr\n",
247 | "import speech_recognition as sr\n",
248 | "r = sr.Recognizer()\n",
249 | "with sr.Microphone() as source:\n",
250 | " print(\"Microphone On:\") \n",
251 | " audio = r.listen(source)\n",
252 | " print(\"Done\")\n",
253 | " text = r.recognize_google(audio)\n",
254 | " print(\"Microphone OFF : {}\".format(text))"
255 | ]
256 | }
257 | ],
258 | "metadata": {
259 | "kernelspec": {
260 | "display_name": "Python 3",
261 | "language": "python",
262 | "name": "python3"
263 | },
264 | "language_info": {
265 | "codemirror_mode": {
266 | "name": "ipython",
267 | "version": 3
268 | },
269 | "file_extension": ".py",
270 | "mimetype": "text/x-python",
271 | "name": "python",
272 | "nbconvert_exporter": "python",
273 | "pygments_lexer": "ipython3",
274 | "version": "3.7.6"
275 | }
276 | },
277 | "nbformat": 4,
278 | "nbformat_minor": 4
279 | }
280 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # ML-AI-Python-Codes
2 |
3 | ## About the Project
4 | This repository contains a collection of Python programs covering various domains such as Machine Learning, Natural Language Processing (NLP), Cryptography, Blockchain, Image Processing, and Data Science. It includes implementations of key algorithms, AI models, trading strategies, and database integrations.
5 |
6 | ## Features
7 | - **Machine Learning & AI:** Face recognition, speech recognition, and neural network applications.
8 | - **Natural Language Processing (NLP):** Implementations using Spacy and NLTK.
9 | - **Cryptography & Blockchain:** Bitcoin mining and cryptographic solutions.
10 | - **Data Science & Analytics:** Stock market analysis, data extraction, and processing.
11 | - **Web Development & APIs:** Flask API development and database connections.
12 | - **Sorting & Algorithms:** Various sorting techniques and the Levenshtein Distance algorithm.
13 |
14 | ## Files and Notebooks
15 |
16 | | File Name | Description |
17 | |-----------|-------------|
18 | | **All sorting techniques.ipynb** | Implementation of various sorting algorithms |
19 | | **Bitcoin mining Cryptography Blockchain.ipynb** | Cryptography and Bitcoin mining concepts |
20 | | **Black hole number 4 - Cosmic number solution.ipynb** | Solution for the Black Hole Number 4 problem |
21 | | **Connect mysql to python.ipynb** | Connecting MySQL database with Python |
22 | | **Data extraction Python libraries.ipynb** | Data extraction methods using Python |
23 | | **Distance calculation between two Geographic location locations.ipynb** | Geographic distance calculation using Python |
24 | | **Django Noes.ipynb** | Notes and filter changes in Django |
25 | | **Download Hisorical data of stocks.ipynb** | Download historical stock market data using Zerodha API |
26 | | **Easy OCR convert image to text.ipynb** | Extract text from images using Optical Character Recognition (OCR) |
27 | | **Face Recognition Machine Learning.ipynb** | Machine Learning algorithms for face recognition |
28 | | **Image Processing.ipynb** | Image processing techniques using Python |
29 | | **Levenshtein Distance Algorithm.ipynb** | Algorithm for word autocompletion and autocorrection |
30 | | **Mutable immutable concept.ipynb** | Explanation of mutable and immutable data types in Python |
31 | | **NLP - Spacy Custom Entity Ruler.ipynb** | Custom entity recognition using Spacy NLP library |
32 | | **Quick Python.ipynb** | General Python scripts and utilities |
33 | | **Solutions.ipynb** | Miscellaneous Python solutions |
34 | | **Speech Recognition Libraries.ipynb** | Speech recognition techniques and libraries |
35 | | **Technical Indicator's of Stock market.ipynb** | Stock market technical indicators |
36 | | **Zerodha Live Trading.ipynb** | Live trading on NSE/BSE using Zerodha API |
37 | | **pandu.ipynb** | Repository cleanup and maintenance |
38 | | **simple api on flask.ipynb** | Creating a simple API using Flask |
39 | | **titanic_data.csv** | Dataset used for pandas testing |
40 |
41 | ## Technologies Used
42 | - **Python**
43 | - **Machine Learning (Scikit-Learn, TensorFlow, Keras)**
44 | - **Natural Language Processing (Spacy, NLTK)**
45 | - **Flask & API Development**
46 | - **Cryptography & Blockchain**
47 | - **Database Integration (MySQL, SQLite)**
48 | - **Stock Market Analysis (Zerodha API)**
49 | - **Image Processing (OpenCV, PIL)**
50 |
51 | ## Setup Instructions
52 | 1. Install required dependencies:
53 | ```sh
54 | pip install numpy pandas scikit-learn nltk spacy tensorflow flask mysql-connector-python
55 | ```
56 | 2. Clone the repository and navigate to the project folder.
57 | 3. Run the required Jupyter Notebook or Python script.
58 |
59 | ## License
60 | This project is open-source and available for learning and research purposes.
61 |
62 | ## Author
63 | Developed to provide a comprehensive collection of ML, AI, and Python-based solutions.
64 |
65 | Feel free to explore and contribute! 🚀
66 |
67 |
--------------------------------------------------------------------------------
/simple api on flask.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "nbformat": 4,
3 | "nbformat_minor": 0,
4 | "metadata": {
5 | "colab": {
6 | "name": "simple api on flask.ipynb",
7 | "private_outputs": true,
8 | "provenance": [],
9 | "collapsed_sections": [],
10 | "authorship_tag": "ABX9TyPvTj5HiilW/Vs4ep57WTNn",
11 | "include_colab_link": true
12 | },
13 | "kernelspec": {
14 | "name": "python3",
15 | "display_name": "Python 3"
16 | }
17 | },
18 | "cells": [
19 | {
20 | "cell_type": "markdown",
21 | "metadata": {
22 | "id": "view-in-github",
23 | "colab_type": "text"
24 | },
25 | "source": [
26 | "
"
27 | ]
28 | },
29 | {
30 | "cell_type": "code",
31 | "metadata": {
32 | "id": "oSzQAwJMWHiZ"
33 | },
34 | "source": [
35 | "import os\n",
36 | "import secrets\n",
37 | "\n",
38 | "from string import punctuation\n",
39 | "from flask import Flask, request, jsonify\n",
40 | "from flask import Flask, request, send_from_directory\n",
41 | "\n",
42 | "# Project imported\n",
43 | "\n",
44 | "\n",
45 | "app = Flask(__name__)\n",
46 | "\n",
47 | "# checking folder\n",
48 | "# to save files in a folder for processing\n",
49 | "\n",
50 | "files = os.listdir()\n",
51 | "check = os.path.isdir('files')\n",
52 | "if check:\n",
53 | " print(\"yes files folder exists\")\n",
54 | "else:\n",
55 | " os.mkdir(\"files\")\n",
56 | " print(\"No folder for saving pdf found creating new files folder\")\n",
57 | "# to save files in a folder for processing\n",
58 | "\n",
59 | "\n",
60 | "\n",
61 | "@app.route('/', methods=['GET', 'POST'])\n",
62 | "def bsa_main():\n",
63 | " # get file from request\n",
64 | " # for single file\n",
65 | " file = request.files['file']\n",
66 | " \n",
67 | " # saving file to local system\n",
68 | " file = request.files['file']\n",
69 | " name = secrets.token_hex(64) + '.pdf'\n",
70 | " UPLOAD_FOLDER = os.path.join(os.getcwd(), 'files')\n",
71 | " file.save(os.path.join(UPLOAD_FOLDER, name))\n",
72 | " path_of_file = os.path.join(UPLOAD_FOLDER, name)\n",
73 | " print(\"file location is:-\",path_of_file, \"File name is:-\",name)\n",
74 | "\n",
75 | "\n",
76 | " # for multiple files\n",
77 | " file_multiple = flask.request.files.getlist(\"file\")\n",
78 | " print(file_multiple)\n",
79 | "\n",
80 | "\n",
81 | " # GEt data from Form \n",
82 | " title_str = request.form['titlestring']\n",
83 | " if title_str:\n",
84 | " title_str=title_str.title()\n",
85 | " return {\"Title string\":title_str}\n",
86 | " else:\n",
87 | " return {\"Title string\":\"None\"}\n",
88 | "\n",
89 | "\n",
90 | "if __name__ == '__main__':\n",
91 | " #app.run(host='0.0.0.0',debug=True,port=5000)\n",
92 | " app.run()\n",
93 | "\n"
94 | ],
95 | "execution_count": null,
96 | "outputs": []
97 | }
98 | ]
99 | }
--------------------------------------------------------------------------------