Decoder2704/python-chatbot-jay
0
1{2 "cells": [3 {4 "cell_type": "code",5 "execution_count": 1,6 "id": "499c780e",7 "metadata": {},8 "outputs": [],9 "source": [10 "# Load data preprocessing libs\n",11 "import pandas as pd\n",12 "import numpy as np\n",13 "from datetime import datetime,date\n",14 "\n",15 "import re\n",16 "from bs4 import BeautifulSoup\n",17 "\n",18 "# Load vectorizer and similarity measure\n",19 "from sklearn.feature_extraction.text import TfidfVectorizer\n",20 "from sklearn.metrics.pairwise import cosine_similarity\n",21 "\n",22 "import tkinter\n",23 "import threading\n",24 "from tkinter import *\n",25 "from tkinter import messagebox\n",26 "import speech_recognition as speech\n",27 "# import import_ipynb\n",28 "# from mailing import mail"29 ]30 },31 {32 "cell_type": "code",33 "execution_count": 2,34 "id": "9310b1f3",35 "metadata": {},36 "outputs": [],37 "source": [38 "q = pd.read_csv(\"Dataset/Questions.csv\",encoding='latin-1')\n",39 "an = pd.read_csv(\"Dataset/AnswersV2.csv\",encoding='latin-1')"40 ]41 },42 {43 "cell_type": "code",44 "execution_count": 3,45 "id": "cd50d5b2",46 "metadata": {},47 "outputs": [48 {49 "data": {50 "text/html": [51 "<div>\n",52 "<style scoped>\n",53 " .dataframe tbody tr th:only-of-type {\n",54 " vertical-align: middle;\n",55 " }\n",56 "\n",57 " .dataframe tbody tr th {\n",58 " vertical-align: top;\n",59 " }\n",60 "\n",61 " .dataframe thead th {\n",62 " text-align: right;\n",63 " }\n",64 "</style>\n",65 "<table border=\"1\" class=\"dataframe\">\n",66 " <thead>\n",67 " <tr style=\"text-align: right;\">\n",68 " <th></th>\n",69 " <th>AId</th>\n",70 " <th>OwnerUserId</th>\n",71 " <th>CreationDate</th>\n",72 " <th>QId</th>\n",73 " <th>Score</th>\n",74 " <th>Body</th>\n",75 " <th>latest_score</th>\n",76 " <th>alternate</th>\n",77 " <th>date</th>\n",78 " </tr>\n",79 " </thead>\n",80 " <tbody>\n",81 " <tr>\n",82 " <th>0</th>\n",83 " <td>497</td>\n",84 " <td>50.0</td>\n",85 " <td>2008-08-02T16:56:53Z</td>\n",86 " <td>469</td>\n",87 " <td>4</td>\n",88 " <td><p>open up a terminal (Applications-&gt;Utilit...</td>\n",89 " <td>4</td>\n",90 " <td>4</td>\n",91 " <td>2022-07-07</td>\n",92 " </tr>\n",93 " <tr>\n",94 " <th>1</th>\n",95 " <td>518</td>\n",96 " <td>153.0</td>\n",97 " <td>2008-08-02T17:42:28Z</td>\n",98 " <td>469</td>\n",99 " <td>2</td>\n",100 " <td><p>I haven't been able to find anything that d...</td>\n",101 " <td>2</td>\n",102 " <td>2</td>\n",103 " <td>2022-07-07</td>\n",104 " </tr>\n",105 " <tr>\n",106 " <th>2</th>\n",107 " <td>536</td>\n",108 " <td>161.0</td>\n",109 " <td>2008-08-02T18:49:07Z</td>\n",110 " <td>502</td>\n",111 " <td>9</td>\n",112 " <td><p>You can use ImageMagick's convert utility f...</td>\n",113 " <td>9</td>\n",114 " <td>9</td>\n",115 " <td>2022-07-07</td>\n",116 " </tr>\n",117 " <tr>\n",118 " <th>3</th>\n",119 " <td>538</td>\n",120 " <td>156.0</td>\n",121 " <td>2008-08-02T18:56:56Z</td>\n",122 " <td>535</td>\n",123 " <td>23</td>\n",124 " <td><p>One possibility is Hudson. It's written in...</td>\n",125 " <td>23</td>\n",126 " <td>23</td>\n",127 " <td>2022-07-07</td>\n",128 " </tr>\n",129 " <tr>\n",130 " <th>4</th>\n",131 " <td>541</td>\n",132 " <td>157.0</td>\n",133 " <td>2008-08-02T19:06:40Z</td>\n",134 " <td>535</td>\n",135 " <td>20</td>\n",136 " <td><p>We run <a href=\"http://buildbot.net/trac\">B...</td>\n",137 " <td>20</td>\n",138 " <td>20</td>\n",139 " <td>2022-07-07</td>\n",140 " </tr>\n",141 " </tbody>\n",142 "</table>\n",143 "</div>"144 ],145 "text/plain": [146 " AId OwnerUserId CreationDate QId Score \\\n",147 "0 497 50.0 2008-08-02T16:56:53Z 469 4 \n",148 "1 518 153.0 2008-08-02T17:42:28Z 469 2 \n",149 "2 536 161.0 2008-08-02T18:49:07Z 502 9 \n",150 "3 538 156.0 2008-08-02T18:56:56Z 535 23 \n",151 "4 541 157.0 2008-08-02T19:06:40Z 535 20 \n",152 "\n",153 " Body latest_score alternate \\\n",154 "0 <p>open up a terminal (Applications->Utilit... 4 4 \n",155 "1 <p>I haven't been able to find anything that d... 2 2 \n",156 "2 <p>You can use ImageMagick's convert utility f... 9 9 \n",157 "3 <p>One possibility is Hudson. It's written in... 23 23 \n",158 "4 <p>We run <a href=\"http://buildbot.net/trac\">B... 20 20 \n",159 "\n",160 " date \n",161 "0 2022-07-07 \n",162 "1 2022-07-07 \n",163 "2 2022-07-07 \n",164 "3 2022-07-07 \n",165 "4 2022-07-07 "166 ]167 },168 "execution_count": 3,169 "metadata": {},170 "output_type": "execute_result"171 }172 ],173 "source": [174 "an.head()"175 ]176 },177 {178 "cell_type": "code",179 "execution_count": 4,180 "id": "e6294c92",181 "metadata": {},182 "outputs": [183 {184 "data": {185 "text/html": [186 "<div>\n",187 "<style scoped>\n",188 " .dataframe tbody tr th:only-of-type {\n",189 " vertical-align: middle;\n",190 " }\n",191 "\n",192 " .dataframe tbody tr th {\n",193 " vertical-align: top;\n",194 " }\n",195 "\n",196 " .dataframe thead th {\n",197 " text-align: right;\n",198 " }\n",199 "</style>\n",200 "<table border=\"1\" class=\"dataframe\">\n",201 " <thead>\n",202 " <tr style=\"text-align: right;\">\n",203 " <th></th>\n",204 " <th>AId</th>\n",205 " <th>OwnerUserId</th>\n",206 " <th>CreationDate</th>\n",207 " <th>QId</th>\n",208 " <th>Score</th>\n",209 " <th>Body</th>\n",210 " <th>latest_score</th>\n",211 " <th>alternate</th>\n",212 " <th>date</th>\n",213 " </tr>\n",214 " </thead>\n",215 " <tbody>\n",216 " <tr>\n",217 " <th>0</th>\n",218 " <td>497</td>\n",219 " <td>50.0</td>\n",220 " <td>2008-08-02T16:56:53Z</td>\n",221 " <td>469</td>\n",222 " <td>4</td>\n",223 " <td><p>open up a terminal (Applications-&gt;Utilit...</td>\n",224 " <td>4</td>\n",225 " <td>4</td>\n",226 " <td>2022-07-07</td>\n",227 " </tr>\n",228 " <tr>\n",229 " <th>1</th>\n",230 " <td>518</td>\n",231 " <td>153.0</td>\n",232 " <td>2008-08-02T17:42:28Z</td>\n",233 " <td>469</td>\n",234 " <td>2</td>\n",235 " <td><p>I haven't been able to find anything that d...</td>\n",236 " <td>2</td>\n",237 " <td>2</td>\n",238 " <td>2022-07-07</td>\n",239 " </tr>\n",240 " <tr>\n",241 " <th>2</th>\n",242 " <td>536</td>\n",243 " <td>161.0</td>\n",244 " <td>2008-08-02T18:49:07Z</td>\n",245 " <td>502</td>\n",246 " <td>9</td>\n",247 " <td><p>You can use ImageMagick's convert utility f...</td>\n",248 " <td>9</td>\n",249 " <td>9</td>\n",250 " <td>2022-07-07</td>\n",251 " </tr>\n",252 " <tr>\n",253 " <th>3</th>\n",254 " <td>538</td>\n",255 " <td>156.0</td>\n",256 " <td>2008-08-02T18:56:56Z</td>\n",257 " <td>535</td>\n",258 " <td>23</td>\n",259 " <td><p>One possibility is Hudson. It's written in...</td>\n",260 " <td>23</td>\n",261 " <td>23</td>\n",262 " <td>2022-07-07</td>\n",263 " </tr>\n",264 " <tr>\n",265 " <th>4</th>\n",266 " <td>541</td>\n",267 " <td>157.0</td>\n",268 " <td>2008-08-02T19:06:40Z</td>\n",269 " <td>535</td>\n",270 " <td>20</td>\n",271 " <td><p>We run <a href=\"http://buildbot.net/trac\">B...</td>\n",272 " <td>20</td>\n",273 " <td>20</td>\n",274 " <td>2022-07-07</td>\n",275 " </tr>\n",276 " </tbody>\n",277 "</table>\n",278 "</div>"279 ],280 "text/plain": [281 " AId OwnerUserId CreationDate QId Score \\\n",282 "0 497 50.0 2008-08-02T16:56:53Z 469 4 \n",283 "1 518 153.0 2008-08-02T17:42:28Z 469 2 \n",284 "2 536 161.0 2008-08-02T18:49:07Z 502 9 \n",285 "3 538 156.0 2008-08-02T18:56:56Z 535 23 \n",286 "4 541 157.0 2008-08-02T19:06:40Z 535 20 \n",287 "\n",288 " Body latest_score alternate \\\n",289 "0 <p>open up a terminal (Applications->Utilit... 4 4 \n",290 "1 <p>I haven't been able to find anything that d... 2 2 \n",291 "2 <p>You can use ImageMagick's convert utility f... 9 9 \n",292 "3 <p>One possibility is Hudson. It's written in... 23 23 \n",293 "4 <p>We run <a href=\"http://buildbot.net/trac\">B... 20 20 \n",294 "\n",295 " date \n",296 "0 2022-07-07 \n",297 "1 2022-07-07 \n",298 "2 2022-07-07 \n",299 "3 2022-07-07 \n",300 "4 2022-07-07 "301 ]302 },303 "execution_count": 4,304 "metadata": {},305 "output_type": "execute_result"306 }307 ],308 "source": [309 "an.rename(columns={'ParentId':'QId'},inplace=True)\n",310 "an.head()"311 ]312 },313 {314 "cell_type": "code",315 "execution_count": 5,316 "id": "3c3a7943",317 "metadata": {},318 "outputs": [],319 "source": [320 "q.rename(columns={'Id':'QId'},inplace=True)\n",321 "an.rename(columns={'Id':'AId'},inplace=True)"322 ]323 },324 {325 "cell_type": "code",326 "execution_count": 6,327 "id": "fb0c57ac",328 "metadata": {},329 "outputs": [],330 "source": [331 "# an = an[an['Score']>5]\n",332 "# an.head()"333 ]334 },335 {336 "cell_type": "code",337 "execution_count": 7,338 "id": "2105797a",339 "metadata": {},340 "outputs": [341 {342 "data": {343 "text/html": [344 "<div>\n",345 "<style scoped>\n",346 " .dataframe tbody tr th:only-of-type {\n",347 " vertical-align: middle;\n",348 " }\n",349 "\n",350 " .dataframe tbody tr th {\n",351 " vertical-align: top;\n",352 " }\n",353 "\n",354 " .dataframe thead th {\n",355 " text-align: right;\n",356 " }\n",357 "</style>\n",358 "<table border=\"1\" class=\"dataframe\">\n",359 " <thead>\n",360 " <tr style=\"text-align: right;\">\n",361 " <th></th>\n",362 " <th>QId</th>\n",363 " <th>OwnerUserId</th>\n",364 " <th>CreationDate</th>\n",365 " <th>Score</th>\n",366 " <th>Title</th>\n",367 " <th>Body</th>\n",368 " </tr>\n",369 " </thead>\n",370 " <tbody>\n",371 " <tr>\n",372 " <th>0</th>\n",373 " <td>469</td>\n",374 " <td>147.0</td>\n",375 " <td>2008-08-02T15:11:16Z</td>\n",376 " <td>21</td>\n",377 " <td>How can I find the full path to a font from it...</td>\n",378 " <td><p>I am using the Photoshop's javascript API t...</td>\n",379 " </tr>\n",380 " <tr>\n",381 " <th>1</th>\n",382 " <td>502</td>\n",383 " <td>147.0</td>\n",384 " <td>2008-08-02T17:01:58Z</td>\n",385 " <td>27</td>\n",386 " <td>Get a preview JPEG of a PDF on Windows?</td>\n",387 " <td><p>I have a cross-platform (Python) applicatio...</td>\n",388 " </tr>\n",389 " <tr>\n",390 " <th>2</th>\n",391 " <td>535</td>\n",392 " <td>154.0</td>\n",393 " <td>2008-08-02T18:43:54Z</td>\n",394 " <td>40</td>\n",395 " <td>Continuous Integration System for a Python Cod...</td>\n",396 " <td><p>I'm starting work on a hobby project with a...</td>\n",397 " </tr>\n",398 " <tr>\n",399 " <th>3</th>\n",400 " <td>594</td>\n",401 " <td>116.0</td>\n",402 " <td>2008-08-03T01:15:08Z</td>\n",403 " <td>25</td>\n",404 " <td>cx_Oracle: How do I iterate over a result set?</td>\n",405 " <td><p>There are several ways to iterate over a re...</td>\n",406 " </tr>\n",407 " <tr>\n",408 " <th>4</th>\n",409 " <td>683</td>\n",410 " <td>199.0</td>\n",411 " <td>2008-08-03T13:19:16Z</td>\n",412 " <td>28</td>\n",413 " <td>Using 'in' to match an attribute of Python obj...</td>\n",414 " <td><p>I don't remember whether I was dreaming or ...</td>\n",415 " </tr>\n",416 " </tbody>\n",417 "</table>\n",418 "</div>"419 ],420 "text/plain": [421 " QId OwnerUserId CreationDate Score \\\n",422 "0 469 147.0 2008-08-02T15:11:16Z 21 \n",423 "1 502 147.0 2008-08-02T17:01:58Z 27 \n",424 "2 535 154.0 2008-08-02T18:43:54Z 40 \n",425 "3 594 116.0 2008-08-03T01:15:08Z 25 \n",426 "4 683 199.0 2008-08-03T13:19:16Z 28 \n",427 "\n",428 " Title \\\n",429 "0 How can I find the full path to a font from it... \n",430 "1 Get a preview JPEG of a PDF on Windows? \n",431 "2 Continuous Integration System for a Python Cod... \n",432 "3 cx_Oracle: How do I iterate over a result set? \n",433 "4 Using 'in' to match an attribute of Python obj... \n",434 "\n",435 " Body \n",436 "0 <p>I am using the Photoshop's javascript API t... \n",437 "1 <p>I have a cross-platform (Python) applicatio... \n",438 "2 <p>I'm starting work on a hobby project with a... \n",439 "3 <p>There are several ways to iterate over a re... \n",440 "4 <p>I don't remember whether I was dreaming or ... "441 ]442 },443 "execution_count": 7,444 "metadata": {},445 "output_type": "execute_result"446 }447 ],448 "source": [449 "q.head()"450 ]451 },452 {453 "cell_type": "code",454 "execution_count": 8,455 "id": "ea1f8452",456 "metadata": {},457 "outputs": [458 {459 "data": {460 "text/html": [461 "<div>\n",462 "<style scoped>\n",463 " .dataframe tbody tr th:only-of-type {\n",464 " vertical-align: middle;\n",465 " }\n",466 "\n",467 " .dataframe tbody tr th {\n",468 " vertical-align: top;\n",469 " }\n",470 "\n",471 " .dataframe thead th {\n",472 " text-align: right;\n",473 " }\n",474 "</style>\n",475 "<table border=\"1\" class=\"dataframe\">\n",476 " <thead>\n",477 " <tr style=\"text-align: right;\">\n",478 " <th></th>\n",479 " <th>QId</th>\n",480 " <th>OwnerUserId_x</th>\n",481 " <th>CreationDate_x</th>\n",482 " <th>Score_x</th>\n",483 " <th>Title</th>\n",484 " <th>Body_x</th>\n",485 " <th>AId</th>\n",486 " <th>OwnerUserId_y</th>\n",487 " <th>CreationDate_y</th>\n",488 " <th>Score_y</th>\n",489 " <th>Body_y</th>\n",490 " <th>latest_score</th>\n",491 " <th>alternate</th>\n",492 " <th>date</th>\n",493 " </tr>\n",494 " </thead>\n",495 " <tbody>\n",496 " <tr>\n",497 " <th>0</th>\n",498 " <td>469</td>\n",499 " <td>147.0</td>\n",500 " <td>2008-08-02T15:11:16Z</td>\n",501 " <td>21</td>\n",502 " <td>How can I find the full path to a font from it...</td>\n",503 " <td><p>I am using the Photoshop's javascript API t...</td>\n",504 " <td>497</td>\n",505 " <td>50.0</td>\n",506 " <td>2008-08-02T16:56:53Z</td>\n",507 " <td>4</td>\n",508 " <td><p>open up a terminal (Applications-&gt;Utilit...</td>\n",509 " <td>4</td>\n",510 " <td>4</td>\n",511 " <td>2022-07-07</td>\n",512 " </tr>\n",513 " <tr>\n",514 " <th>1</th>\n",515 " <td>469</td>\n",516 " <td>147.0</td>\n",517 " <td>2008-08-02T15:11:16Z</td>\n",518 " <td>21</td>\n",519 " <td>How can I find the full path to a font from it...</td>\n",520 " <td><p>I am using the Photoshop's javascript API t...</td>\n",521 " <td>518</td>\n",522 " <td>153.0</td>\n",523 " <td>2008-08-02T17:42:28Z</td>\n",524 " <td>2</td>\n",525 " <td><p>I haven't been able to find anything that d...</td>\n",526 " <td>2</td>\n",527 " <td>2</td>\n",528 " <td>2022-07-07</td>\n",529 " </tr>\n",530 " <tr>\n",531 " <th>2</th>\n",532 " <td>469</td>\n",533 " <td>147.0</td>\n",534 " <td>2008-08-02T15:11:16Z</td>\n",535 " <td>21</td>\n",536 " <td>How can I find the full path to a font from it...</td>\n",537 " <td><p>I am using the Photoshop's javascript API t...</td>\n",538 " <td>3040</td>\n",539 " <td>457.0</td>\n",540 " <td>2008-08-06T03:01:23Z</td>\n",541 " <td>12</td>\n",542 " <td><p>Unfortunately the only API that isn't depre...</td>\n",543 " <td>12</td>\n",544 " <td>12</td>\n",545 " <td>2022-07-07</td>\n",546 " </tr>\n",547 " <tr>\n",548 " <th>3</th>\n",549 " <td>469</td>\n",550 " <td>147.0</td>\n",551 " <td>2008-08-02T15:11:16Z</td>\n",552 " <td>21</td>\n",553 " <td>How can I find the full path to a font from it...</td>\n",554 " <td><p>I am using the Photoshop's javascript API t...</td>\n",555 " <td>195170</td>\n",556 " <td>745.0</td>\n",557 " <td>2008-10-12T07:02:40Z</td>\n",558 " <td>1</td>\n",559 " <td><p>There must be a method in Cocoa to get a li...</td>\n",560 " <td>1</td>\n",561 " <td>1</td>\n",562 " <td>2022-07-07</td>\n",563 " </tr>\n",564 " <tr>\n",565 " <th>4</th>\n",566 " <td>502</td>\n",567 " <td>147.0</td>\n",568 " <td>2008-08-02T17:01:58Z</td>\n",569 " <td>27</td>\n",570 " <td>Get a preview JPEG of a PDF on Windows?</td>\n",571 " <td><p>I have a cross-platform (Python) applicatio...</td>\n",572 " <td>536</td>\n",573 " <td>161.0</td>\n",574 " <td>2008-08-02T18:49:07Z</td>\n",575 " <td>9</td>\n",576 " <td><p>You can use ImageMagick's convert utility f...</td>\n",577 " <td>9</td>\n",578 " <td>9</td>\n",579 " <td>2022-07-07</td>\n",580 " </tr>\n",581 " </tbody>\n",582 "</table>\n",583 "</div>"584 ],585 "text/plain": [586 " QId OwnerUserId_x CreationDate_x Score_x \\\n",587 "0 469 147.0 2008-08-02T15:11:16Z 21 \n",588 "1 469 147.0 2008-08-02T15:11:16Z 21 \n",589 "2 469 147.0 2008-08-02T15:11:16Z 21 \n",590 "3 469 147.0 2008-08-02T15:11:16Z 21 \n",591 "4 502 147.0 2008-08-02T17:01:58Z 27 \n",592 "\n",593 " Title \\\n",594 "0 How can I find the full path to a font from it... \n",595 "1 How can I find the full path to a font from it... \n",596 "2 How can I find the full path to a font from it... \n",597 "3 How can I find the full path to a font from it... \n",598 "4 Get a preview JPEG of a PDF on Windows? \n",599 "\n",600 " Body_x AId OwnerUserId_y \\\n",601 "0 <p>I am using the Photoshop's javascript API t... 497 50.0 \n",602 "1 <p>I am using the Photoshop's javascript API t... 518 153.0 \n",603 "2 <p>I am using the Photoshop's javascript API t... 3040 457.0 \n",604 "3 <p>I am using the Photoshop's javascript API t... 195170 745.0 \n",605 "4 <p>I have a cross-platform (Python) applicatio... 536 161.0 \n",606 "\n",607 " CreationDate_y Score_y \\\n",608 "0 2008-08-02T16:56:53Z 4 \n",609 "1 2008-08-02T17:42:28Z 2 \n",610 "2 2008-08-06T03:01:23Z 12 \n",611 "3 2008-10-12T07:02:40Z 1 \n",612 "4 2008-08-02T18:49:07Z 9 \n",613 "\n",614 " Body_y latest_score alternate \\\n",615 "0 <p>open up a terminal (Applications->Utilit... 4 4 \n",616 "1 <p>I haven't been able to find anything that d... 2 2 \n",617 "2 <p>Unfortunately the only API that isn't depre... 12 12 \n",618 "3 <p>There must be a method in Cocoa to get a li... 1 1 \n",619 "4 <p>You can use ImageMagick's convert utility f... 9 9 \n",620 "\n",621 " date \n",622 "0 2022-07-07 \n",623 "1 2022-07-07 \n",624 "2 2022-07-07 \n",625 "3 2022-07-07 \n",626 "4 2022-07-07 "627 ]628 },629 "execution_count": 8,630 "metadata": {},631 "output_type": "execute_result"632 }633 ],634 "source": [635 "df = q.merge(an, on='QId')\n",636 "df.head()"637 ]638 },639 {640 "cell_type": "code",641 "execution_count": 9,642 "id": "56a5a410",643 "metadata": {},644 "outputs": [645 {646 "data": {647 "text/html": [648 "<div>\n",649 "<style scoped>\n",650 " .dataframe tbody tr th:only-of-type {\n",651 " vertical-align: middle;\n",652 " }\n",653 "\n",654 " .dataframe tbody tr th {\n",655 " vertical-align: top;\n",656 " }\n",657 "\n",658 " .dataframe thead th {\n",659 " text-align: right;\n",660 " }\n",661 "</style>\n",662 "<table border=\"1\" class=\"dataframe\">\n",663 " <thead>\n",664 " <tr style=\"text-align: right;\">\n",665 " <th></th>\n",666 " <th>QId</th>\n",667 " <th>Title</th>\n",668 " <th>Body_x</th>\n",669 " <th>AId</th>\n",670 " <th>Score_y</th>\n",671 " <th>Body_y</th>\n",672 " <th>latest_score</th>\n",673 " <th>alternate</th>\n",674 " <th>date</th>\n",675 " </tr>\n",676 " </thead>\n",677 " <tbody>\n",678 " <tr>\n",679 " <th>0</th>\n",680 " <td>469</td>\n",681 " <td>How can I find the full path to a font from it...</td>\n",682 " <td><p>I am using the Photoshop's javascript API t...</td>\n",683 " <td>497</td>\n",684 " <td>4</td>\n",685 " <td><p>open up a terminal (Applications-&gt;Utilit...</td>\n",686 " <td>4</td>\n",687 " <td>4</td>\n",688 " <td>2022-07-07</td>\n",689 " </tr>\n",690 " <tr>\n",691 " <th>1</th>\n",692 " <td>469</td>\n",693 " <td>How can I find the full path to a font from it...</td>\n",694 " <td><p>I am using the Photoshop's javascript API t...</td>\n",695 " <td>518</td>\n",696 " <td>2</td>\n",697 " <td><p>I haven't been able to find anything that d...</td>\n",698 " <td>2</td>\n",699 " <td>2</td>\n",700 " <td>2022-07-07</td>\n",701 " </tr>\n",702 " <tr>\n",703 " <th>2</th>\n",704 " <td>469</td>\n",705 " <td>How can I find the full path to a font from it...</td>\n",706 " <td><p>I am using the Photoshop's javascript API t...</td>\n",707 " <td>3040</td>\n",708 " <td>12</td>\n",709 " <td><p>Unfortunately the only API that isn't depre...</td>\n",710 " <td>12</td>\n",711 " <td>12</td>\n",712 " <td>2022-07-07</td>\n",713 " </tr>\n",714 " <tr>\n",715 " <th>3</th>\n",716 " <td>469</td>\n",717 " <td>How can I find the full path to a font from it...</td>\n",718 " <td><p>I am using the Photoshop's javascript API t...</td>\n",719 " <td>195170</td>\n",720 " <td>1</td>\n",721 " <td><p>There must be a method in Cocoa to get a li...</td>\n",722 " <td>1</td>\n",723 " <td>1</td>\n",724 " <td>2022-07-07</td>\n",725 " </tr>\n",726 " <tr>\n",727 " <th>4</th>\n",728 " <td>502</td>\n",729 " <td>Get a preview JPEG of a PDF on Windows?</td>\n",730 " <td><p>I have a cross-platform (Python) applicatio...</td>\n",731 " <td>536</td>\n",732 " <td>9</td>\n",733 " <td><p>You can use ImageMagick's convert utility f...</td>\n",734 " <td>9</td>\n",735 " <td>9</td>\n",736 " <td>2022-07-07</td>\n",737 " </tr>\n",738 " </tbody>\n",739 "</table>\n",740 "</div>"741 ],742 "text/plain": [743 " QId Title \\\n",744 "0 469 How can I find the full path to a font from it... \n",745 "1 469 How can I find the full path to a font from it... \n",746 "2 469 How can I find the full path to a font from it... \n",747 "3 469 How can I find the full path to a font from it... \n",748 "4 502 Get a preview JPEG of a PDF on Windows? \n",749 "\n",750 " Body_x AId Score_y \\\n",751 "0 <p>I am using the Photoshop's javascript API t... 497 4 \n",752 "1 <p>I am using the Photoshop's javascript API t... 518 2 \n",753 "2 <p>I am using the Photoshop's javascript API t... 3040 12 \n",754 "3 <p>I am using the Photoshop's javascript API t... 195170 1 \n",755 "4 <p>I have a cross-platform (Python) applicatio... 536 9 \n",756 "\n",757 " Body_y latest_score alternate \\\n",758 "0 <p>open up a terminal (Applications->Utilit... 4 4 \n",759 "1 <p>I haven't been able to find anything that d... 2 2 \n",760 "2 <p>Unfortunately the only API that isn't depre... 12 12 \n",761 "3 <p>There must be a method in Cocoa to get a li... 1 1 \n",762 "4 <p>You can use ImageMagick's convert utility f... 9 9 \n",763 "\n",764 " date \n",765 "0 2022-07-07 \n",766 "1 2022-07-07 \n",767 "2 2022-07-07 \n",768 "3 2022-07-07 \n",769 "4 2022-07-07 "770 ]771 },772 "execution_count": 9,773 "metadata": {},774 "output_type": "execute_result"775 }776 ],777 "source": [778 "df.drop(columns=['OwnerUserId_x','CreationDate_x','Score_x','OwnerUserId_y','CreationDate_y'],inplace=True)\n",779 "df.head()"780 ]781 },782 {783 "cell_type": "code",784 "execution_count": 10,785 "id": "15c46d77",786 "metadata": {},787 "outputs": [],788 "source": [789 "df.rename(columns={'Body_x':'Question','Body_y':'Answer','Score_y':'Score'},inplace=True)"790 ]791 },792 {793 "cell_type": "code",794 "execution_count": 11,795 "id": "b1fa97db",796 "metadata": {},797 "outputs": [798 {799 "data": {800 "text/html": [801 "<div>\n",802 "<style scoped>\n",803 " .dataframe tbody tr th:only-of-type {\n",804 " vertical-align: middle;\n",805 " }\n",806 "\n",807 " .dataframe tbody tr th {\n",808 " vertical-align: top;\n",809 " }\n",810 "\n",811 " .dataframe thead th {\n",812 " text-align: right;\n",813 " }\n",814 "</style>\n",815 "<table border=\"1\" class=\"dataframe\">\n",816 " <thead>\n",817 " <tr style=\"text-align: right;\">\n",818 " <th></th>\n",819 " <th>QId</th>\n",820 " <th>Title</th>\n",821 " <th>Question</th>\n",822 " <th>AId</th>\n",823 " <th>Score</th>\n",824 " <th>Answer</th>\n",825 " <th>latest_score</th>\n",826 " <th>alternate</th>\n",827 " <th>date</th>\n",828 " </tr>\n",829 " </thead>\n",830 " <tbody>\n",831 " <tr>\n",832 " <th>0</th>\n",833 " <td>469</td>\n",834 " <td>How can I find the full path to a font from it...</td>\n",835 " <td><p>I am using the Photoshop's javascript API t...</td>\n",836 " <td>497</td>\n",837 " <td>4</td>\n",838 " <td><p>open up a terminal (Applications-&gt;Utilit...</td>\n",839 " <td>4</td>\n",840 " <td>4</td>\n",841 " <td>2022-07-07</td>\n",842 " </tr>\n",843 " <tr>\n",844 " <th>1</th>\n",845 " <td>469</td>\n",846 " <td>How can I find the full path to a font from it...</td>\n",847 " <td><p>I am using the Photoshop's javascript API t...</td>\n",848 " <td>518</td>\n",849 " <td>2</td>\n",850 " <td><p>I haven't been able to find anything that d...</td>\n",851 " <td>2</td>\n",852 " <td>2</td>\n",853 " <td>2022-07-07</td>\n",854 " </tr>\n",855 " <tr>\n",856 " <th>2</th>\n",857 " <td>469</td>\n",858 " <td>How can I find the full path to a font from it...</td>\n",859 " <td><p>I am using the Photoshop's javascript API t...</td>\n",860 " <td>3040</td>\n",861 " <td>12</td>\n",862 " <td><p>Unfortunately the only API that isn't depre...</td>\n",863 " <td>12</td>\n",864 " <td>12</td>\n",865 " <td>2022-07-07</td>\n",866 " </tr>\n",867 " <tr>\n",868 " <th>3</th>\n",869 " <td>469</td>\n",870 " <td>How can I find the full path to a font from it...</td>\n",871 " <td><p>I am using the Photoshop's javascript API t...</td>\n",872 " <td>195170</td>\n",873 " <td>1</td>\n",874 " <td><p>There must be a method in Cocoa to get a li...</td>\n",875 " <td>1</td>\n",876 " <td>1</td>\n",877 " <td>2022-07-07</td>\n",878 " </tr>\n",879 " <tr>\n",880 " <th>4</th>\n",881 " <td>502</td>\n",882 " <td>Get a preview JPEG of a PDF on Windows?</td>\n",883 " <td><p>I have a cross-platform (Python) applicatio...</td>\n",884 " <td>536</td>\n",885 " <td>9</td>\n",886 " <td><p>You can use ImageMagick's convert utility f...</td>\n",887 " <td>9</td>\n",888 " <td>9</td>\n",889 " <td>2022-07-07</td>\n",890 " </tr>\n",891 " </tbody>\n",892 "</table>\n",893 "</div>"894 ],895 "text/plain": [896 " QId Title \\\n",897 "0 469 How can I find the full path to a font from it... \n",898 "1 469 How can I find the full path to a font from it... \n",899 "2 469 How can I find the full path to a font from it... \n",900 "3 469 How can I find the full path to a font from it... \n",901 "4 502 Get a preview JPEG of a PDF on Windows? \n",902 "\n",903 " Question AId Score \\\n",904 "0 <p>I am using the Photoshop's javascript API t... 497 4 \n",905 "1 <p>I am using the Photoshop's javascript API t... 518 2 \n",906 "2 <p>I am using the Photoshop's javascript API t... 3040 12 \n",907 "3 <p>I am using the Photoshop's javascript API t... 195170 1 \n",908 "4 <p>I have a cross-platform (Python) applicatio... 536 9 \n",909 "\n",910 " Answer latest_score alternate \\\n",911 "0 <p>open up a terminal (Applications->Utilit... 4 4 \n",912 "1 <p>I haven't been able to find anything that d... 2 2 \n",913 "2 <p>Unfortunately the only API that isn't depre... 12 12 \n",914 "3 <p>There must be a method in Cocoa to get a li... 1 1 \n",915 "4 <p>You can use ImageMagick's convert utility f... 9 9 \n",916 "\n",917 " date \n",918 "0 2022-07-07 \n",919 "1 2022-07-07 \n",920 "2 2022-07-07 \n",921 "3 2022-07-07 \n",922 "4 2022-07-07 "923 ]924 },925 "execution_count": 11,926 "metadata": {},927 "output_type": "execute_result"928 }929 ],930 "source": [931 "df.head()"932 ]933 },934 {935 "cell_type": "code",936 "execution_count": 12,937 "id": "7e2bb140",938 "metadata": {},939 "outputs": [],940 "source": [941 "df['Answer'] = df['Answer'].apply(lambda x:BeautifulSoup(x).get_text())\n",942 "df['Question'] = df['Question'].apply(lambda x:BeautifulSoup(x).get_text())"943 ]944 },945 {946 "cell_type": "code",947 "execution_count": 13,948 "id": "d3a5c40b",949 "metadata": {},950 "outputs": [951 {952 "data": {953 "text/html": [954 "<div>\n",955 "<style scoped>\n",956 " .dataframe tbody tr th:only-of-type {\n",957 " vertical-align: middle;\n",958 " }\n",959 "\n",960 " .dataframe tbody tr th {\n",961 " vertical-align: top;\n",962 " }\n",963 "\n",964 " .dataframe thead th {\n",965 " text-align: right;\n",966 " }\n",967 "</style>\n",968 "<table border=\"1\" class=\"dataframe\">\n",969 " <thead>\n",970 " <tr style=\"text-align: right;\">\n",971 " <th></th>\n",972 " <th>QId</th>\n",973 " <th>Title</th>\n",974 " <th>Question</th>\n",975 " <th>AId</th>\n",976 " <th>Score</th>\n",977 " <th>Answer</th>\n",978 " <th>latest_score</th>\n",979 " <th>alternate</th>\n",980 " <th>date</th>\n",981 " </tr>\n",982 " </thead>\n",983 " <tbody>\n",984 " <tr>\n",985 " <th>0</th>\n",986 " <td>469</td>\n",987 " <td>How can I find the full path to a font from it...</td>\n",988 " <td>I am using the Photoshop's javascript API to f...</td>\n",989 " <td>497</td>\n",990 " <td>4</td>\n",991 " <td>open up a terminal (Applications->Utilities->T...</td>\n",992 " <td>4</td>\n",993 " <td>4</td>\n",994 " <td>2022-07-07</td>\n",995 " </tr>\n",996 " <tr>\n",997 " <th>1</th>\n",998 " <td>469</td>\n",999 " <td>How can I find the full path to a font from it...</td>\n",1000 " <td>I am using the Photoshop's javascript API to f...</td>\n",1001 " <td>518</td>\n",1002 " <td>2</td>\n",1003 " <td>I haven't been able to find anything that does...</td>\n",1004 " <td>2</td>\n",1005 " <td>2</td>\n",1006 " <td>2022-07-07</td>\n",1007 " </tr>\n",1008 " <tr>\n",1009 " <th>2</th>\n",1010 " <td>469</td>\n",1011 " <td>How can I find the full path to a font from it...</td>\n",1012 " <td>I am using the Photoshop's javascript API to f...</td>\n",1013 " <td>3040</td>\n",1014 " <td>12</td>\n",1015 " <td>Unfortunately the only API that isn't deprecat...</td>\n",1016 " <td>12</td>\n",1017 " <td>12</td>\n",1018 " <td>2022-07-07</td>\n",1019 " </tr>\n",1020 " <tr>\n",1021 " <th>3</th>\n",1022 " <td>469</td>\n",1023 " <td>How can I find the full path to a font from it...</td>\n",1024 " <td>I am using the Photoshop's javascript API to f...</td>\n",1025 " <td>195170</td>\n",1026 " <td>1</td>\n",1027 " <td>There must be a method in Cocoa to get a list ...</td>\n",1028 " <td>1</td>\n",1029 " <td>1</td>\n",1030 " <td>2022-07-07</td>\n",1031 " </tr>\n",1032 " <tr>\n",1033 " <th>4</th>\n",1034 " <td>502</td>\n",1035 " <td>Get a preview JPEG of a PDF on Windows?</td>\n",1036 " <td>I have a cross-platform (Python) application w...</td>\n",1037 " <td>536</td>\n",1038 " <td>9</td>\n",1039 " <td>You can use ImageMagick's convert utility for ...</td>\n",1040 " <td>9</td>\n",1041 " <td>9</td>\n",1042 " <td>2022-07-07</td>\n",1043 " </tr>\n",1044 " </tbody>\n",1045 "</table>\n",1046 "</div>"1047 ],1048 "text/plain": [1049 " QId Title \\\n",1050 "0 469 How can I find the full path to a font from it... \n",1051 "1 469 How can I find the full path to a font from it... \n",1052 "2 469 How can I find the full path to a font from it... \n",1053 "3 469 How can I find the full path to a font from it... \n",1054 "4 502 Get a preview JPEG of a PDF on Windows? \n",1055 "\n",1056 " Question AId Score \\\n",1057 "0 I am using the Photoshop's javascript API to f... 497 4 \n",1058 "1 I am using the Photoshop's javascript API to f... 518 2 \n",1059 "2 I am using the Photoshop's javascript API to f... 3040 12 \n",1060 "3 I am using the Photoshop's javascript API to f... 195170 1 \n",1061 "4 I have a cross-platform (Python) application w... 536 9 \n",1062 "\n",1063 " Answer latest_score alternate \\\n",1064 "0 open up a terminal (Applications->Utilities->T... 4 4 \n",1065 "1 I haven't been able to find anything that does... 2 2 \n",1066 "2 Unfortunately the only API that isn't deprecat... 12 12 \n",1067 "3 There must be a method in Cocoa to get a list ... 1 1 \n",1068 "4 You can use ImageMagick's convert utility for ... 9 9 \n",1069 "\n",1070 " date \n",1071 "0 2022-07-07 \n",1072 "1 2022-07-07 \n",1073 "2 2022-07-07 \n",1074 "3 2022-07-07 \n",1075 "4 2022-07-07 "1076 ]1077 },1078 "execution_count": 13,1079 "metadata": {},1080 "output_type": "execute_result"1081 }1082 ],1083 "source": [1084 "df.head()"1085 ]1086 },1087 {1088 "cell_type": "code",1089 "execution_count": 14,1090 "id": "d01589ab",1091 "metadata": {},1092 "outputs": [1093 {1094 "data": {1095 "text/plain": [1096 "TfidfVectorizer()"1097 ]1098 },1099 "execution_count": 14,1100 "metadata": {},1101 "output_type": "execute_result"1102 }1103 ],1104 "source": [1105 "vectorizer = TfidfVectorizer()\n",1106 "vectorizer.fit(np.concatenate((df.Question, df.Answer)))"1107 ]1108 },1109 {1110 "cell_type": "code",1111 "execution_count": 15,1112 "id": "63b5737b",1113 "metadata": {},1114 "outputs": [],1115 "source": [1116 "Question_vectors = vectorizer.transform(df.Question)"1117 ]1118 },1119 {1120 "cell_type": "code",1121 "execution_count": 16,1122 "id": "d354b464",1123 "metadata": {},1124 "outputs": [],1125 "source": [1126 "threshold = 30\n",1127 "answer = []"1128 ]1129 },1130 {1131 "cell_type": "code",1132 "execution_count": 17,1133 "id": "366f46cc",1134 "metadata": {},1135 "outputs": [],1136 "source": [1137 "def getAnswerWithHighestScore(answers):\n",1138 " r = np.argmax(answers['latest_score']-answers['alternate'])\n",1139 " if (datetime.strptime(str(date.today()),\"%Y-%m-%d\")-datetime.strptime(answers['date'].iloc[0],\"%Y-%m-%d\")).days > threshold:\n",1140 " df[df['QId'] == answers['QId'].iloc[0]]['date'] = date.today()\n",1141 " df[df['QId'] == answers['QId'].iloc[0]]['alternate']=df[df['QId'] == answers['QId'].iloc[0]]['latest_score']\n",1142 " an[an['QId'] == answers['QId'].iloc[0]]['date'] = date.today()\n",1143 " an[an['QId'] == answers['QId'].iloc[0]]['alternate']=df[df['QId'] == answers['QId'].iloc[0]]['latest_score']\n",1144 " an.to_csv(\"Dataset/AnswersV2.csv\",index=False)\n",1145 " return [answers.iloc[r]['Answer'],answers.iloc[r]['AId'],answers.iloc[r]['alternate']]"1146 ]1147 },1148 {1149 "cell_type": "code",1150 "execution_count": 18,1151 "id": "737213ef",1152 "metadata": {},1153 "outputs": [],1154 "source": [1155 "def get_answer(row):\n",1156 " global answer\n",1157 " qid = df.iloc[row][0]\n",1158 " answers = df.loc[df['QId'] == qid]\n",1159 " answer = getAnswerWithHighestScore(answers)\n",1160 " return [answer[0],answer[2]]"1161 ]1162 },1163 {1164 "cell_type": "code",1165 "execution_count": 19,1166 "id": "93a13b89",1167 "metadata": {},1168 "outputs": [],1169 "source": [1170 "def chatbot_response(msg):\n",1171 " input_question =BeautifulSoup(msg).get_text()\n",1172 "\n",1173 " # Locate the closest question\n",1174 " input_question_vector = vectorizer.transform([input_question])\n",1175 "\n",1176 " # Compute similarities\n",1177 " similarities = cosine_similarity(input_question_vector, Question_vectors)\n",1178 "\n",1179 " # Find the closest question\n",1180 " closest = np.argmax(similarities, axis=1)\n",1181 " a = get_answer(closest[0])\n",1182 " return [a[0],a[1]]\n",1183 "# return df.Answer.iloc[closest].values[0]"1184 ]1185 },1186 {1187 "cell_type": "code",1188 "execution_count": 20,1189 "id": "85503221",1190 "metadata": {},1191 "outputs": [],1192 "source": [1193 "global check,upvoted,downvoted\n",1194 "check = True\n",1195 "upvoted = False\n",1196 "downvoted = False"1197 ]1198 },1199 {1200 "cell_type": "code",