{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Average Accuracy (MDM=True) across all groups with n_neighbors=1: 85.69%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=2: 86.04%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=3: 85.85%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=4: 85.88%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=5: 85.84%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=6: 85.81%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=7: 85.84%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=8: 85.86%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=9: 85.84%\n",
      "Average Accuracy (MDM=True) across all groups with n_neighbors=10: 85.91%\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 53\u001b[0m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;66;03m# Compute Word2Vec vectors for the train and test data\u001b[39;00m\n\u001b[1;32m     52\u001b[0m train_all_vectors \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([compute_sentence_vector(desc, model, vector_size) \u001b[38;5;28;01mfor\u001b[39;00m desc \u001b[38;5;129;01min\u001b[39;00m train_all_csv[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtag_description\u001b[39m\u001b[38;5;124m'\u001b[39m]])\n\u001b[0;32m---> 53\u001b[0m test_vectors \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([compute_sentence_vector(desc, model, vector_size) \u001b[38;5;28;01mfor\u001b[39;00m desc \u001b[38;5;129;01min\u001b[39;00m test_csv[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtag_description\u001b[39m\u001b[38;5;124m'\u001b[39m]])\n\u001b[1;32m     55\u001b[0m \u001b[38;5;66;03m# KNN에서 코사인 거리를 이용\u001b[39;00m\n\u001b[1;32m     56\u001b[0m knn \u001b[38;5;241m=\u001b[39m NearestNeighbors(n_neighbors\u001b[38;5;241m=\u001b[39mn, metric\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124meuclidean\u001b[39m\u001b[38;5;124m'\u001b[39m, n_jobs\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n",
      "Cell \u001b[0;32mIn[1], line 53\u001b[0m, in \u001b[0;36m<listcomp>\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m     51\u001b[0m \u001b[38;5;66;03m# Compute Word2Vec vectors for the train and test data\u001b[39;00m\n\u001b[1;32m     52\u001b[0m train_all_vectors \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([compute_sentence_vector(desc, model, vector_size) \u001b[38;5;28;01mfor\u001b[39;00m desc \u001b[38;5;129;01min\u001b[39;00m train_all_csv[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtag_description\u001b[39m\u001b[38;5;124m'\u001b[39m]])\n\u001b[0;32m---> 53\u001b[0m test_vectors \u001b[38;5;241m=\u001b[39m np\u001b[38;5;241m.\u001b[39marray([\u001b[43mcompute_sentence_vector\u001b[49m\u001b[43m(\u001b[49m\u001b[43mdesc\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmodel\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvector_size\u001b[49m\u001b[43m)\u001b[49m \u001b[38;5;28;01mfor\u001b[39;00m desc \u001b[38;5;129;01min\u001b[39;00m test_csv[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtag_description\u001b[39m\u001b[38;5;124m'\u001b[39m]])\n\u001b[1;32m     55\u001b[0m \u001b[38;5;66;03m# KNN에서 코사인 거리를 이용\u001b[39;00m\n\u001b[1;32m     56\u001b[0m knn \u001b[38;5;241m=\u001b[39m NearestNeighbors(n_neighbors\u001b[38;5;241m=\u001b[39mn, metric\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124meuclidean\u001b[39m\u001b[38;5;124m'\u001b[39m, n_jobs\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m)\n",
      "Cell \u001b[0;32mIn[1], line 12\u001b[0m, in \u001b[0;36mcompute_sentence_vector\u001b[0;34m(sentence, model, vector_size)\u001b[0m\n\u001b[1;32m     10\u001b[0m word_vectors \u001b[38;5;241m=\u001b[39m [model\u001b[38;5;241m.\u001b[39mwv[word] \u001b[38;5;28;01mfor\u001b[39;00m word \u001b[38;5;129;01min\u001b[39;00m words \u001b[38;5;28;01mif\u001b[39;00m word \u001b[38;5;129;01min\u001b[39;00m model\u001b[38;5;241m.\u001b[39mwv]\n\u001b[1;32m     11\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(word_vectors) \u001b[38;5;241m>\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 12\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mnp\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmean\u001b[49m\u001b[43m(\u001b[49m\u001b[43mword_vectors\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m     13\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m     14\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m np\u001b[38;5;241m.\u001b[39mzeros(vector_size)\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/numpy/core/fromnumeric.py:3504\u001b[0m, in \u001b[0;36mmean\u001b[0;34m(a, axis, dtype, out, keepdims, where)\u001b[0m\n\u001b[1;32m   3501\u001b[0m     \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m   3502\u001b[0m         \u001b[38;5;28;01mreturn\u001b[39;00m mean(axis\u001b[38;5;241m=\u001b[39maxis, dtype\u001b[38;5;241m=\u001b[39mdtype, out\u001b[38;5;241m=\u001b[39mout, \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mkwargs)\n\u001b[0;32m-> 3504\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_methods\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_mean\u001b[49m\u001b[43m(\u001b[49m\u001b[43ma\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m   3505\u001b[0m \u001b[43m                      \u001b[49m\u001b[43mout\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/anaconda3/envs/torch/lib/python3.10/site-packages/numpy/core/_methods.py:118\u001b[0m, in \u001b[0;36m_mean\u001b[0;34m(a, axis, dtype, out, keepdims, where)\u001b[0m\n\u001b[1;32m    115\u001b[0m         dtype \u001b[38;5;241m=\u001b[39m mu\u001b[38;5;241m.\u001b[39mdtype(\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mf4\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m    116\u001b[0m         is_float16_result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m\n\u001b[0;32m--> 118\u001b[0m ret \u001b[38;5;241m=\u001b[39m \u001b[43mumr_sum\u001b[49m\u001b[43m(\u001b[49m\u001b[43marr\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43maxis\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mout\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mkeepdims\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mwhere\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mwhere\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    119\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(ret, mu\u001b[38;5;241m.\u001b[39mndarray):\n\u001b[1;32m    120\u001b[0m     \u001b[38;5;28;01mwith\u001b[39;00m _no_nep50_warning():\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from gensim.models import Word2Vec\n",
    "from sklearn.neighbors import NearestNeighbors\n",
    "import os\n",
    "\n",
    "# Function to compute the average Word2Vec vector for a sentence\n",
    "def compute_sentence_vector(sentence, model, vector_size):\n",
    "    words = sentence.split()\n",
    "    word_vectors = [model.wv[word] for word in words if word in model.wv]\n",
    "    if len(word_vectors) > 0:\n",
    "        return np.mean(word_vectors, axis=0)\n",
    "    else:\n",
    "        return np.zeros(vector_size)\n",
    "\n",
    "# Initialize variables to store overall accuracy results\n",
    "average_accuracies = []\n",
    "\n",
    "# Loop through n_neighbors values from 1 to 52\n",
    "for n in range(1, 53):\n",
    "    accuracies = []  # Store accuracy for each group\n",
    "\n",
    "    # Loop through group numbers from 1 to 5\n",
    "    for group_number in range(1, 6):\n",
    "        train_all_path = f'../../data_preprocess/dataset/{group_number}/train_all.csv'\n",
    "        test_path = f'../../translation/0.result/{group_number}/test_p.csv'\n",
    "\n",
    "        if not os.path.exists(test_path):\n",
    "            print(f\"Test file for Group {group_number} does not exist. Skipping...\")\n",
    "            continue\n",
    "\n",
    "        # Load the train_all and test CSVs\n",
    "        train_all_csv = pd.read_csv(train_all_path, low_memory=False)\n",
    "        test_csv = pd.read_csv(test_path, low_memory=False)\n",
    "\n",
    "        train_all_csv['tag_description'] = train_all_csv['tag_description'].fillna('')\n",
    "        test_csv['tag_description'] = test_csv['tag_description'].fillna('')\n",
    "\n",
    "        test_csv['c_thing'] = ''\n",
    "        test_csv['c_property'] = ''\n",
    "        test_csv['c_score'] = ''\n",
    "        test_csv['c_duplicate'] = 0\n",
    "\n",
    "        combined_tag_descriptions = train_all_csv['tag_description'].tolist() + test_csv['tag_description'].tolist()\n",
    "\n",
    "        # Train Word2Vec model on combined descriptions\n",
    "        sentences = [desc.split() for desc in combined_tag_descriptions]\n",
    "        vector_size = 200  # You can set the vector size as needed\n",
    "        model = Word2Vec(sentences, vector_size=vector_size, window=3, min_count=1, workers=-1)\n",
    "\n",
    "        # Compute Word2Vec vectors for the train and test data\n",
    "        train_all_vectors = np.array([compute_sentence_vector(desc, model, vector_size) for desc in train_all_csv['tag_description']])\n",
    "        test_vectors = np.array([compute_sentence_vector(desc, model, vector_size) for desc in test_csv['tag_description']])\n",
    "\n",
    "        # KNN에서 코사인 거리를 이용\n",
    "        knn = NearestNeighbors(n_neighbors=n, metric='euclidean', n_jobs=-1)\n",
    "        knn.fit(train_all_vectors)\n",
    "\n",
    "        distances, indices = knn.kneighbors(test_vectors)\n",
    "\n",
    "        predicted_things = []\n",
    "        predicted_properties = []\n",
    "        predicted_scores = []\n",
    "\n",
    "        for i in range(len(test_csv)):\n",
    "            neighbor_index = indices[i][0]\n",
    "            distance = distances[i][0]\n",
    "\n",
    "            neighbor_thing = train_all_csv.iloc[neighbor_index]['thing']\n",
    "            neighbor_property = train_all_csv.iloc[neighbor_index]['property']\n",
    "\n",
    "            predicted_things.append(neighbor_thing)\n",
    "            predicted_properties.append(neighbor_property)\n",
    "\n",
    "            # 거리 기반으로 유사도 점수 계산\n",
    "            predicted_score = 1 - distance\n",
    "            predicted_scores.append(predicted_score)\n",
    "\n",
    "        test_csv['c_thing'] = predicted_things\n",
    "        test_csv['c_property'] = predicted_properties\n",
    "        test_csv['c_score'] = predicted_scores\n",
    "\n",
    "        test_csv['cthing_correct'] = test_csv['thing'] == test_csv['c_thing']\n",
    "        test_csv['cproperty_correct'] = test_csv['property'] == test_csv['c_property']\n",
    "        test_csv['ctp_correct'] = test_csv['cthing_correct'] & test_csv['cproperty_correct']\n",
    "\n",
    "        mdm_true_count = len(test_csv[test_csv['MDM'] == True])\n",
    "        accuracy = (test_csv['ctp_correct'].sum() / mdm_true_count) * 100\n",
    "        accuracies.append(accuracy)\n",
    "\n",
    "    # Calculate the average accuracy for the current n_neighbors value\n",
    "    average_accuracy = sum(accuracies) / len(accuracies)\n",
    "    average_accuracies.append(average_accuracy)\n",
    "    print(f\"Average Accuracy (MDM=True) across all groups with n_neighbors={n}: {average_accuracy:.2f}%\")\n",
    "\n",
    "# Print overall results for all n_neighbors values\n",
    "print(\"\\nFinal Results:\")\n",
    "for n, avg_accuracy in zip(range(1, 53), average_accuracies):\n",
    "    print(f\"n_neighbors={n}, Average Accuracy: {avg_accuracy:.2f}%\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "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.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
