{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"!pip install rapidfuzz","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:14:48.867189Z","iopub.execute_input":"2021-10-08T16:14:48.867510Z","iopub.status.idle":"2021-10-08T16:15:00.002835Z","shell.execute_reply.started":"2021-10-08T16:14:48.867431Z","shell.execute_reply":"2021-10-08T16:15:00.001869Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# copy & edit from : https://www.kaggle.com/thedrcat/wiki-image-caption-eda-and-baseline-new-data\nimport pandas as pd\nimport numpy as np\nimport base64\nfrom PIL import Image\nimport io\nfrom rapidfuzz import process, fuzz\nfrom urllib.parse import unquote","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:15:00.004756Z","iopub.execute_input":"2021-10-08T16:15:00.004968Z","iopub.status.idle":"2021-10-08T16:15:00.020339Z","shell.execute_reply.started":"2021-10-08T16:15:00.004942Z","shell.execute_reply":"2021-10-08T16:15:00.019472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Take a look on submission ","metadata":{}},{"cell_type":"code","source":"sub = pd.read_csv('../input/wikipedia-image-caption/sample_submission.csv')\nsub.head(15)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:15:00.021648Z","iopub.execute_input":"2021-10-08T16:15:00.021863Z","iopub.status.idle":"2021-10-08T16:15:00.571969Z","shell.execute_reply.started":"2021-10-08T16:15:00.021829Z","shell.execute_reply":"2021-10-08T16:15:00.571080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### There are id, caption_title_and_reference_description, in sample submission  \n#### and every id has 5 caption","metadata":{}},{"cell_type":"markdown","source":"#### test.tsv contain id, image_url","metadata":{}},{"cell_type":"code","source":"test = pd.read_csv('../input/wikipedia-image-caption/test.tsv',sep='\\t')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:15:00.573831Z","iopub.execute_input":"2021-10-08T16:15:00.574312Z","iopub.status.idle":"2021-10-08T16:15:00.809628Z","shell.execute_reply.started":"2021-10-08T16:15:00.574263Z","shell.execute_reply":"2021-10-08T16:15:00.808875Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    print(test.image_url.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:15:49.187584Z","iopub.execute_input":"2021-10-08T16:15:49.187865Z","iopub.status.idle":"2021-10-08T16:15:49.196518Z","shell.execute_reply.started":"2021-10-08T16:15:49.187838Z","shell.execute_reply":"2021-10-08T16:15:49.195897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_caption_list = pd.read_csv('../input/wikipedia-image-caption/test_caption_list.csv')\ntest_caption_list.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:16:07.937208Z","iopub.execute_input":"2021-10-08T16:16:07.937780Z","iopub.status.idle":"2021-10-08T16:16:08.332496Z","shell.execute_reply.started":"2021-10-08T16:16:07.937746Z","shell.execute_reply":"2021-10-08T16:16:08.331685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = pd.read_csv('../input/wikipedia-image-caption/image_data_test/image_pixels/test_image_pixels_part-00000.csv',sep='\\t',header=None)\ntest_image.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:17:00.787014Z","iopub.execute_input":"2021-10-08T16:17:00.787286Z","iopub.status.idle":"2021-10-08T16:17:09.126778Z","shell.execute_reply.started":"2021-10-08T16:17:00.787260Z","shell.execute_reply":"2021-10-08T16:17:09.125891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image64_encode = base64.b64decode(test_image[1].iloc[2])\nimg = Image.open(io.BytesIO(image64_encode))\nimg","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:18:54.334897Z","iopub.execute_input":"2021-10-08T16:18:54.335214Z","iopub.status.idle":"2021-10-08T16:18:54.402900Z","shell.execute_reply.started":"2021-10-08T16:18:54.335184Z","shell.execute_reply":"2021-10-08T16:18:54.402066Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import datatable as dt\ntrain = dt.fread('../input/wikipedia-image-caption/train-00000-of-00005.tsv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:18:57.296199Z","iopub.execute_input":"2021-10-08T16:18:57.296807Z","iopub.status.idle":"2021-10-08T16:20:07.080028Z","shell.execute_reply.started":"2021-10-08T16:18:57.296767Z","shell.execute_reply":"2021-10-08T16:20:07.079174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### The page_url have contain some information for caption_title_and_reference_description  \n#### test data also have url. Maybe it can do something.","metadata":{}},{"cell_type":"code","source":"def url_target_process(s):\n    s = s.rsplit('/',1)[1]\n    s = unquote(s)\n    s = s.replace('_',' ')\n    s = s + '[SEP]'\n    return s","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.081596Z","iopub.execute_input":"2021-10-08T16:20:07.081836Z","iopub.status.idle":"2021-10-08T16:20:07.087354Z","shell.execute_reply.started":"2021-10-08T16:20:07.081788Z","shell.execute_reply":"2021-10-08T16:20:07.086457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### from url to target in training data  ","metadata":{}},{"cell_type":"code","source":"for i in range(5):\n    print(f'target: {train[i,-1]}')\n    print(f'prediction: {url_target_process(train[i,1])}')\n    print()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.088816Z","iopub.execute_input":"2021-10-08T16:20:07.089061Z","iopub.status.idle":"2021-10-08T16:20:07.102926Z","shell.execute_reply.started":"2021-10-08T16:20:07.089034Z","shell.execute_reply":"2021-10-08T16:20:07.102125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Seems work so apply on test data on make submission","metadata":{}},{"cell_type":"code","source":"test['prediction'] = test['image_url'].apply(url_target_process)\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.105297Z","iopub.execute_input":"2021-10-08T16:20:07.105546Z","iopub.status.idle":"2021-10-08T16:20:07.513090Z","shell.execute_reply.started":"2021-10-08T16:20:07.105521Z","shell.execute_reply":"2021-10-08T16:20:07.512236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Caption = test_caption_list.caption_title_and_reference_description.values.tolist()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.514049Z","iopub.execute_input":"2021-10-08T16:20:07.514235Z","iopub.status.idle":"2021-10-08T16:20:07.520703Z","shell.execute_reply.started":"2021-10-08T16:20:07.514213Z","shell.execute_reply":"2021-10-08T16:20:07.519733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    pp = test.prediction[i]\n    print(pp)\n    res = process.extract(pp, Caption, scorer=fuzz.ratio, processor=None, limit=4)\n    print('closet caption:')\n    for j in res:\n        print(j[0])\n    print('*'*40)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.521734Z","iopub.execute_input":"2021-10-08T16:20:07.521943Z","iopub.status.idle":"2021-10-08T16:20:07.633135Z","shell.execute_reply.started":"2021-10-08T16:20:07.521920Z","shell.execute_reply":"2021-10-08T16:20:07.632273Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predict_caption(m):\n    res = process.extract(m, Caption, scorer=fuzz.ratio, processor=None, limit=5)\n    res = [x[0] for x in res]\n    return res","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.642412Z","iopub.execute_input":"2021-10-08T16:20:07.642780Z","iopub.status.idle":"2021-10-08T16:20:07.654324Z","shell.execute_reply.started":"2021-10-08T16:20:07.642751Z","shell.execute_reply":"2021-10-08T16:20:07.653442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.auto import tqdm\ntqdm.pandas()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T16:20:07.655330Z","iopub.execute_input":"2021-10-08T16:20:07.655550Z","iopub.status.idle":"2021-10-08T16:20:07.757068Z","shell.execute_reply.started":"2021-10-08T16:20:07.655523Z","shell.execute_reply":"2021-10-08T16:20:07.756420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['caption_title_and_reference_description'] = test['prediction'].progress_apply(predict_caption)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T09:47:53.898116Z","iopub.execute_input":"2021-10-08T09:47:53.898993Z","iopub.status.idle":"2021-10-08T09:49:34.926111Z","shell.execute_reply.started":"2021-10-08T09:47:53.898951Z","shell.execute_reply":"2021-10-08T09:49:34.924908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"del sub","metadata":{"execution":{"iopub.status.busy":"2021-10-08T09:15:54.686233Z","iopub.execute_input":"2021-10-08T09:15:54.686585Z","iopub.status.idle":"2021-10-08T09:15:54.692074Z","shell.execute_reply.started":"2021-10-08T09:15:54.686553Z","shell.execute_reply":"2021-10-08T09:15:54.691372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nsub = test[['id','caption_title_and_reference_description']]\nsub = sub.explode('caption_title_and_reference_description')\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-08T09:15:57.773448Z","iopub.execute_input":"2021-10-08T09:15:57.773888Z","iopub.status.idle":"2021-10-08T09:15:57.985362Z","shell.execute_reply.started":"2021-10-08T09:15:57.773858Z","shell.execute_reply":"2021-10-08T09:15:57.984724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-08T09:16:20.036328Z","iopub.execute_input":"2021-10-08T09:16:20.03677Z","iopub.status.idle":"2021-10-08T09:16:21.699655Z","shell.execute_reply.started":"2021-10-08T09:16:20.036741Z","shell.execute_reply":"2021-10-08T09:16:21.698936Z"},"trusted":true},"execution_count":null,"outputs":[]}]}