{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python -m pip install seaborn" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!python -m pip install scikit-learn" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from elasticsearch import Elasticsearch\n", "client = Elasticsearch(\"http://localhost:9200\")\n", "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": [], "source": [ "\n" ] }, { "cell_type": "code", "execution_count": 4, "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": 5, "metadata": {}, "outputs": [], "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": 10, "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": 11, "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": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_eng.head()" ] }, { "cell_type": "code", "execution_count": 12, "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": 13, "metadata": {}, "outputs": [], "source": [ "def readHtml(html):\n", " dftable = pd.read_html(html)\n", " return dftable" ] }, { "cell_type": "code", "execution_count": 14, "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": null, "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": 15, "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 systemthermostatNaNNaNNaN
1zf 1043 010 297 automatic transmissionautomatic transmissionautomatic transmissionspareparttransmissionautomatic transmissionautomatic transmissionNaNNaN
2zf 1060 040 003 automatic transmissionautomatic transmissionautomatic transmissionspareparttransmissionautomatic transmissionautomatic transmissionNaNNaN
3vaico v10 0131 camshaftcamshaftcamshaftsparepartengineengine timingcamshaftNaNNaN
4vaico v26 0302 track control armtrack control armtrack control armsparepartpowertrain suspensionwheel suspensiontrack control armNaNNaN
\n", "
" ], "text/plain": [ " name ... catlevel5\n", "0 denso dtm82363 thermostat coolant ... NaN\n", "1 zf 1043 010 297 automatic transmission ... NaN\n", "2 zf 1060 040 003 automatic transmission ... NaN\n", "3 vaico v10 0131 camshaft ... NaN\n", "4 vaico v26 0302 track control arm ... NaN\n", "\n", "[5 rows x 9 columns]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_eng.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng.isna().sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng = df_eng.dropna()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng.describe()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "One hot encoding " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng = pd.get_dummies(df_eng,columns=[\"category\"])\n", "df_eng.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_eng.shape" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sklearn.model_selection import train_test_split\n", "\n" ] } ], "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.9.7" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }