{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ .,;'0123456789\"" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import unicodedata\n", "import string\n", "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\n", "\n", "all_letters = string.ascii_letters + \" .,;'0123456789\"\n", "n_letters = len(all_letters)\n", "\n", "all_letters" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Build Category dictionary, category and list of product name, shor description" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from elasticsearch import Elasticsearch\n", "client = Elasticsearch(\"http://172.17.212.35:9200\")\n", "resp = client.search(index=\"retromotion-indexer_development_products\",\n", " body={\"_source\":[\"descriptions\",\"descriptionsSource\",\"nameSource\",\"shortDescriptionSource\",\"categoriesSource\"],\n", " \"query\": {\"match_all\": {}}})" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "df_eng = pd.DataFrame(columns=['name','sdesc','category','catlevel0','catlevel1','catlevel2','catlevel3','catlevel4','catlevel5'])" ] }, { "cell_type": "code", "execution_count": 5, "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": 6, "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": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | name | \n", "sdesc | \n", "category | \n", "catlevel0 | \n", "catlevel1 | \n", "catlevel2 | \n", "catlevel3 | \n", "catlevel4 | \n", "catlevel5 | \n", "
|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "denso dtm82363 thermostat coolant | \n", "thermostat coolant | \n", "thermostat | \n", "sparepart | \n", "cooling system | \n", "thermostat | \n", "NaN | \n", "NaN | \n", "NaN | \n", "
| 1 | \n", "zf 1043 010 297 automatic transmission | \n", "automatic transmission | \n", "automatic transmission | \n", "sparepart | \n", "transmission | \n", "automatic transmission | \n", "automatic transmission | \n", "NaN | \n", "NaN | \n", "
| 2 | \n", "zf 1060 040 003 automatic transmission | \n", "automatic transmission | \n", "automatic transmission | \n", "sparepart | \n", "transmission | \n", "automatic transmission | \n", "automatic transmission | \n", "NaN | \n", "NaN | \n", "
| 3 | \n", "vaico v10 0131 camshaft | \n", "camshaft | \n", "camshaft | \n", "sparepart | \n", "engine | \n", "engine timing | \n", "camshaft | \n", "NaN | \n", "NaN | \n", "
| 4 | \n", "vaico v26 0302 track control arm | \n", "track control arm | \n", "track control arm | \n", "sparepart | \n", "powertrain suspension | \n", "wheel suspension | \n", "track control arm | \n", "NaN | \n", "NaN | \n", "