{ "cells": [ { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "15865.52s - pydevd: Sending message related to process being replaced timed-out after 5 seconds\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Defaulting to user installation because normal site-packages is not writeable\n", "Collecting scikit-learn\n", " Downloading scikit_learn-1.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (9.6 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m9.6/9.6 MB\u001b[0m \u001b[31m3.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hRequirement already satisfied: numpy>=1.17.3 in /home/shoney/.local/lib/python3.10/site-packages (from scikit-learn) (1.24.3)\n", "Collecting scipy>=1.3.2\n", " Downloading scipy-1.10.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (34.4 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m34.4/34.4 MB\u001b[0m \u001b[31m6.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n", "\u001b[?25hCollecting joblib>=1.1.1\n", " Downloading joblib-1.2.0-py3-none-any.whl (297 kB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m298.0/298.0 KB\u001b[0m \u001b[31m6.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n", "\u001b[?25hCollecting threadpoolctl>=2.0.0\n", " Downloading threadpoolctl-3.1.0-py3-none-any.whl (14 kB)\n", "Installing collected packages: threadpoolctl, scipy, joblib, scikit-learn\n", "Successfully installed joblib-1.2.0 scikit-learn-1.2.2 scipy-1.10.1 threadpoolctl-3.1.0\n" ] } ], "source": [ "!python3 -m pip install scikit-learn" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from elasticsearch import Elasticsearch\n", "client = Elasticsearch(\"http://localhost:9200\")\n", "client.info()\n", "client.ping()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "resp = client.search(index=\"retromotion-indexer_development_products\",\n", " body={\"_source\":[\"descriptions\",\"descriptionsSource\",\"nameSource\",\"shortDescriptionSource\",\"categoriesSource\"],\n", " \"query\": {\"match_all\": {}}})\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Got 22329 Hits:\n" ] } ], "source": [ "print(\"Got %d Hits:\" % resp['hits']['total'])" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "ename": "ModuleNotFoundError", "evalue": "No module named 'seaborn'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[55], line 7\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39msklearn\u001b[39;00m\n\u001b[1;32m 6\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mmatplotlib\u001b[39;00m\u001b[39m.\u001b[39;00m\u001b[39mpyplot\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39mplt\u001b[39;00m\n\u001b[0;32m----> 7\u001b[0m \u001b[39mimport\u001b[39;00m \u001b[39mseaborn\u001b[39;00m \u001b[39mas\u001b[39;00m \u001b[39msns\u001b[39;00m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'seaborn'" ] } ], "source": [ "import pandas as pd\n", "import re\n", "import numpy as np\n", "\n", "import sklearn\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "df_eng = pd.DataFrame(columns=['name','sdesc','category','catlevel0','catlevel1','catlevel2','catlevel3','catlevel4','catlevel5'])\n", "df_de = pd.DataFrame(columns=['name','sdesc','desc','category','categorypath'])" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namesdesccategorycatlevel0catlevel1catlevel2catlevel3catlevel4catlevel5
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [name, sdesc, category, catlevel0, catlevel1, catlevel2, catlevel3, catlevel4, catlevel5]\n", "Index: []" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_eng.head()" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "def cleanStr(text):\n", " \n", " # remove numbers\n", " # no_number_string = re.sub(r'\\d+','',lower_string)\n", "\n", " # remove all punctuation except words and space\n", " # text = re.sub(r'[^\\w\\s]','', lower_string)\n", "\n", " # clean = re.compile('<.*?>')\n", "\n", " # text = re.sub(clean,'',text)\n", " text = re.sub('<[^>]*>', '', text)\n", " emoticons = re.findall('(?::|;|=)(?:-)?(?:\\)|\\(|D|P)', text)\n", " text = (re.sub('[\\W]+', ' ', text.lower()) + ' ' .join(emoticons).replace('-', ''))\n", "\n", " return text\n" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "def readHtml(html):\n", " dftable = pd.read_html(html)\n", " return dftable" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [], "source": [ "name_en=[]\n", "name_de=[]\n", "\n", "sdesc_en=[]\n", "sdesc_de=[]\n", "\n", "desc_en=[]\n", "desc_de=[]\n", "\n", "cat_en=[]\n", "cat_de=[]\n", "\n", "catpath_en=[]\n", "catpath_de=[]\n", "\n", "for hit in resp['hits']['hits']:\n", " list_row =[]\n", " for name in hit['_source']['nameSource']:\n", " if (name[\"language\"]==\"en\"):\n", " list_row.append(cleanStr(name[\"value\"]))\n", " else:\n", " name_de.append(name[\"value\"])\n", " \n", " for sdesc in hit['_source']['shortDescriptionSource']:\n", " if (sdesc[\"language\"]==\"en\"):\n", " list_row.append(cleanStr(sdesc[\"value\"]))\n", " else:\n", " sdesc_de.append(sdesc[\"value\"])\n", "\n", " # for desc in hit['_source']['descriptionsSource']:\n", " # if (desc[\"language\"]==\"en\"):\n", " # desc_en.append(readHtml(desc[\"value\"]))\n", " # else:\n", " # desc_de.append(desc[\"value\"])\n", " \n", " for cats in hit['_source']['categoriesSource']:\n", " if (cats[\"language\"]==\"en\"):\n", " list_row.append(cleanStr(cats[\"label\"]))\n", " else:\n", " desc_de.append(cats[\"label\"])\n", "\n", " if (cats[\"language\"]==\"en\"):\n", " categories =cats[\"path\"].split('/')\n", " i =0\n", " catls=[]\n", " for category in categories:\n", "\n", " list_row.append(cleanStr(category))\n", " i= i+1\n", " \n", " else:\n", " catpath_de.append(cats[\"path\"])\n", " \n", " # new_row = pd.DataFrame({'name':_name, 'sdesc':_sdesc, 'category':_cat_en }, index=[0])\n", "\n", " # pd.concat([new_row,df_eng.loc[:]]).reset_index(drop=True)\n", "\n", " for i in range (0,9-len(list_row)):\n", " list_row.append(np.nan)\n", "\n", " df_eng.loc[len(df_eng)] = list_row\n", "\n" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "# df_en = pd.DataFrame({'name':name_en,\n", "# 'sdesc':sdesc_en,\n", "# # 'desc':desc_en,\n", "# 'category':cat_en,\n", "# 'categorypath':catpath_en})\n", "\n", "\n", "# df_de = pd.DataFrame({'name':name_de,\n", "# 'sdesc':sdesc_de,\n", "# 'desc':desc_de,\n", "# 'category':cat_de,\n", "# 'categorypath':catpath_de})" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
namesdesccategorycatlevel0catlevel1catlevel2catlevel3catlevel4catlevel5
0denso dtm82363 thermostat coolantthermostat coolantthermostatSparePartCooling SystemThermostatnanana
1zf 1043 010 297 automatic transmissionautomatic transmissionautomatic transmissionSparePartTransmissionAutomatic TransmissionAutomatic Transmissionnana
2zf 1060 040 003 automatic transmissionautomatic transmissionautomatic transmissionSparePartTransmissionAutomatic TransmissionAutomatic Transmissionnana
3vaico v10 0131 camshaftcamshaftcamshaftSparePartEngineEngine TimingCamshaftnana
4vaico v26 0302 track control armtrack control armtrack control armSparePartPowertrain & SuspensionWheel SuspensionTrack Control Armnana
\n", "
" ], "text/plain": [ " name sdesc \\\n", "0 denso dtm82363 thermostat coolant thermostat coolant \n", "1 zf 1043 010 297 automatic transmission automatic transmission \n", "2 zf 1060 040 003 automatic transmission automatic transmission \n", "3 vaico v10 0131 camshaft camshaft \n", "4 vaico v26 0302 track control arm track control arm \n", "\n", " category catlevel0 catlevel1 \\\n", "0 thermostat SparePart Cooling System \n", "1 automatic transmission SparePart Transmission \n", "2 automatic transmission SparePart Transmission \n", "3 camshaft SparePart Engine \n", "4 track control arm SparePart Powertrain & Suspension \n", "\n", " catlevel2 catlevel3 catlevel4 catlevel5 \n", "0 Thermostat na na na \n", "1 Automatic Transmission Automatic Transmission na na \n", "2 Automatic Transmission Automatic Transmission na na \n", "3 Engine Timing Camshaft na na \n", "4 Wheel Suspension Track Control Arm na na " ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_eng.head()" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "name 0\n", "sdesc 0\n", "category 0\n", "catlevel0 0\n", "catlevel1 0\n", "catlevel2 0\n", "catlevel3 6\n", "catlevel4 30\n", "catlevel5 30\n", "dtype: int64" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_eng.isna().sum()" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(40, 9)" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_eng.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.6" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }