{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "da65bfc9-6748-4fa6-94dc-86019000fc61",
   "metadata": {},
   "source": [
    "# Smart Sentence Finder"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5805d5a4-c4e0-4611-ba89-7f1492aae6b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/sd205521/anaconda3/envs/rapids-23.12/lib/python3.10/site-packages/sentence_transformers/cross_encoder/CrossEncoder.py:11: TqdmExperimentalWarning: Using `tqdm.autonotebook.tqdm` in notebook mode. Use `tqdm.tqdm` instead to force console mode (e.g. in jupyter console)\n",
      "  from tqdm.autonotebook import tqdm, trange\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import torch.nn.functional as F\n",
    "import numpy as np\n",
    "import pysbd\n",
    "import os\n",
    "import re\n",
    "from pathlib import Path\n",
    "\n",
    "from sentence_transformers import SentenceTransformer\n",
    "from tqdm.notebook import tqdm\n",
    "from concurrent.futures import ThreadPoolExecutor, as_completed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e32a9e59-9f5d-4a8d-9518-335c30b20da5",
   "metadata": {},
   "outputs": [],
   "source": [
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be007efb-bdf4-4852-81eb-8c7649e1eaf1",
   "metadata": {},
   "source": [
    "## Functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0164031d-84c8-4aba-844e-1fc74b5155eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def process(model_name, input_query, sentences, n=5, device=device):\n",
    "    # Create a tqdm progress bar with the total number of steps\n",
    "    progress_bar = tqdm(total=5, desc=\"Starting\")\n",
    "\n",
    "    # Load the model and set it to the device\n",
    "    model = SentenceTransformer(model_name).to(device)\n",
    "    progress_bar.update(1)\n",
    "    progress_bar.set_description(\"Model loaded\")\n",
    "\n",
    "    # Encode the input query\n",
    "    input_embedding = model.encode(input_query, convert_to_tensor=True).to(device)\n",
    "    progress_bar.update(1)\n",
    "    progress_bar.set_description(\"Input query encoded\")\n",
    "\n",
    "    # Filter sentences and encode them\n",
    "    filtered_sentences = [sentence for sentence in sentences if len(sentence.split()) > 5]\n",
    "    embeddings = model.encode(filtered_sentences, convert_to_tensor=True).to(device)\n",
    "    progress_bar.update(1)\n",
    "    progress_bar.set_description(\"Sentences encoded\")\n",
    "\n",
    "    # Compute cosine similarity scores\n",
    "    scores = F.cosine_similarity(embeddings, input_embedding.unsqueeze(0), dim=1)\n",
    "    progress_bar.update(1)\n",
    "    progress_bar.set_description(\"Cosine similarity calculated\")\n",
    "\n",
    "    # Move scores to CPU for further processing\n",
    "    scores = scores.cpu()\n",
    "\n",
    "    # Sort and select top n sentences\n",
    "    top_sentences = sorted(zip(filtered_sentences, scores), key=lambda x: x[1], reverse=True)[:n]\n",
    "    progress_bar.update(1)\n",
    "    progress_bar.set_description(\"Sentences sorted and selected\")\n",
    "\n",
    "    # Print top sentences\n",
    "    print(f\"Model name is: {model_name}.\\n\")\n",
    "    print(f\"Input query is: {input_query}\\n\")\n",
    "    for i, (sentence, score) in enumerate(top_sentences):\n",
    "        print(f\"Ranking: {i+1} | Score: {score:.4f}\\nSentence: {sentence}\\n\")\n",
    "    \n",
    "    progress_bar.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "129be43a-24e3-4a48-839a-45394d12b995",
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_by_character_count(text, chars_per_chunk):\n",
    "    total_chars = len(text)\n",
    "    chunks = []\n",
    "    start = 0\n",
    "\n",
    "    while start < total_chars:\n",
    "        # Set the initial end position\n",
    "        end = min(start + chars_per_chunk, total_chars)\n",
    "\n",
    "        # Search backwards for a period\n",
    "        if end < total_chars:\n",
    "            while end > start and text[end-1] != '.':\n",
    "                end -= 1\n",
    "\n",
    "            # If no period is found in the chunk, extend to the next period\n",
    "            if end == start:\n",
    "                while end < total_chars and text[end-1] != '.':\n",
    "                    end += 1\n",
    "\n",
    "        chunk = text[start:end].strip()\n",
    "        chunks.append(chunk)\n",
    "        start = end\n",
    "\n",
    "    return chunks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7d46c96c-beb0-43e8-856f-ea85de2d927f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def process_chunk(chunk):\n",
    "    seg = pysbd.Segmenter(language=\"en\", clean=False)\n",
    "    return seg.segment(chunk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f05051c0-7903-49a6-b442-e30f07a186bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def clean_sentences(sentences):\n",
    "    cleaned_sentences = []\n",
    "    modification_count = 0\n",
    "\n",
    "    for sentence in tqdm(sentences):\n",
    "        # Remove leading and trailing spaces\n",
    "        trimmed_sentence = sentence.strip()\n",
    "        # Replace multiple spaces with a single space\n",
    "        cleaned_sentence = re.sub(r'\\s+', ' ', trimmed_sentence)\n",
    "\n",
    "        if sentence != cleaned_sentence:\n",
    "            modification_count += 1\n",
    "\n",
    "        cleaned_sentences.append(cleaned_sentence)\n",
    "\n",
    "    print(f\"{modification_count} sentences cleaned.\")\n",
    "    \n",
    "    return cleaned_sentences"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c581888-dacb-4f0f-815c-8e45d66dacb6",
   "metadata": {},
   "source": [
    "## Paths"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "d24e0394-48a3-4423-8d98-078d8e37fdac",
   "metadata": {},
   "outputs": [],
   "source": [
    "base_dir = Path.cwd()\n",
    "data_dir = base_dir / 'data'\n",
    "\n",
    "alice_in_wonderland_path = data_dir / 'alice_in_wonderland.txt'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2050efc5-73b6-48d5-8aa2-50f1a5ed6826",
   "metadata": {},
   "source": [
    "## Input Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3f0e90db-7596-4d4b-a02d-0a4e0ef2a814",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(alice_in_wonderland_path, 'r', encoding='utf-8') as file:\n",
    "    input_text = file.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "beb18b84-8347-4897-9817-cb46b30da6bc",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_query = \"She wonders about things.\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "443acad8-9de5-42be-b98c-b48c7152ae42",
   "metadata": {},
   "source": [
    "## Processing Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "beb83fd3-7231-404d-893c-931f43b04857",
   "metadata": {},
   "outputs": [],
   "source": [
    "input_text = input_text.replace(\"\\n\", \" \").replace(\"-\", \" \").replace(\"_\", \" \")\n",
    "word_count = len(input_text.split())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "bda38f28-8212-43db-a982-7df6b3cffe97",
   "metadata": {},
   "outputs": [],
   "source": [
    "chars_per_chunk = 10000  # Adjust the number of characters per chunk\n",
    "chunks = split_by_character_count(input_text, chars_per_chunk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6702e9f3-100c-4adb-a3fa-44066fe08422",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/17 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "if len(input_text) > chars_per_chunk * 2:\n",
    "    num_threads = os.cpu_count()\n",
    "\n",
    "    with ThreadPoolExecutor(max_workers=num_threads) as executor:\n",
    "        # Create futures for processing each chunk\n",
    "        futures = [executor.submit(process_chunk, chunk) for chunk in chunks]\n",
    "\n",
    "        # Collect all sentences using list comprehension\n",
    "        sentences = [sentence for future in tqdm(as_completed(futures), total=len(futures), leave=False) for sentence in future.result()]\n",
    "\n",
    "else:\n",
    "    sentences = process_chunk(input_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "bc5c23e0-f64a-4dab-aab4-c7bad349eb47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e097000603c04681bb4671a85a6824ee",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/929 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "913 sentences cleaned.\n"
     ]
    }
   ],
   "source": [
    "sentences = clean_sentences(sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fab53473-1429-4771-811a-19c9bf86e5c9",
   "metadata": {},
   "source": [
    "## Models"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75a908e0-219b-41c6-8b4e-4fb220ab799b",
   "metadata": {},
   "source": [
    "### all-mpnet-base-v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bf996c48-7a72-4653-a03b-acaead160602",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'sentence-transformers/all-mpnet-base-v2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9bb8a931-650a-482a-a1be-d700c399e811",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9e84f51fd5034dbc857115405dc6da79",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/sd205521/anaconda3/envs/rapids-23.12/lib/python3.10/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: sentence-transformers/all-mpnet-base-v2.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.5630\n",
      "Sentence: First, she tried to look down and make out what she was coming to, but it was too dark to see anything; then she looked at the sides of the well, and noticed that they were filled with cupboards and book shelves; here and there she saw maps and pictures hung upon pegs.\n",
      "\n",
      "Ranking: 2 | Score: 0.5617\n",
      "Sentence: Alice asked in a tone of great curiosity.\n",
      "\n",
      "Ranking: 3 | Score: 0.5466\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n",
      "Ranking: 4 | Score: 0.5302\n",
      "Sentence: How she longed to get out of that dark hall, and wander about among those beds of bright flowers and those cool fountains, but she could not even get her head through the doorway; “and even if my head would go through,” thought poor Alice, “it would be of very little use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I could, if I only knew how to begin.”\n",
      "\n",
      "Ranking: 5 | Score: 0.5151\n",
      "Sentence: “Thinking again?” the Duchess asked, with another dig of her sharp little chin.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8e4de852-3611-4fe1-b66e-db5cdbf235b1",
   "metadata": {},
   "source": [
    "### bge-large-en-v1.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "2372398e-4be6-47d9-ac04-72c5dece4a12",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'BAAI/bge-large-en-v1.5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6ce86ad9-7f8b-4578-86a0-78378a61ddcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "503639b9378a401bb665841036eb4fa6",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: BAAI/bge-large-en-v1.5.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.7519\n",
      "Sentence: “Does the boots and shoes!” she repeated in a wondering tone.\n",
      "\n",
      "Ranking: 2 | Score: 0.7257\n",
      "Sentence: Alice asked in a tone of great curiosity.\n",
      "\n",
      "Ranking: 3 | Score: 0.6796\n",
      "Sentence: While she was looking at the place where it had been, it suddenly appeared again.\n",
      "\n",
      "Ranking: 4 | Score: 0.6774\n",
      "Sentence: “How can I have done that?” she thought.\n",
      "\n",
      "Ranking: 5 | Score: 0.6752\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b86b1a8-7039-4e5d-874c-4f1d9b3c50d4",
   "metadata": {},
   "source": [
    "### bge-small-en-v1.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "3559254c-4cc7-46e0-8bca-77f47daa4c54",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'BAAI/bge-small-en-v1.5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "76c70923-e10d-4b21-97e4-1d861cc52620",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "97f3d0508e61462c82623637c43d5594",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: BAAI/bge-small-en-v1.5.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.7542\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n",
      "Ranking: 2 | Score: 0.7517\n",
      "Sentence: “Does the boots and shoes!” she repeated in a wondering tone.\n",
      "\n",
      "Ranking: 3 | Score: 0.7491\n",
      "Sentence: Alice asked in a tone of great curiosity.\n",
      "\n",
      "Ranking: 4 | Score: 0.7149\n",
      "Sentence: “But perhaps he can’t help it,” she said to herself; “his eyes are so very nearly at the top of his head. But at any rate he might answer questions.-How am I to get in?” she repeated, aloud.\n",
      "\n",
      "Ranking: 5 | Score: 0.7122\n",
      "Sentence: “Is that the reason so many tea things are put out here?” she asked.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4658088-14fc-47b1-a506-e1eb9a9b8284",
   "metadata": {},
   "source": [
    "### paraphrase-multilingual-MiniLM-L12-v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a3c30b6b-989b-4ec9-abd9-83df0c2f1857",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d32531de-f289-494f-9997-73b312ec2396",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "a75e046a1e1a46d8860e5f1e60e12e19",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.5765\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n",
      "Ranking: 2 | Score: 0.5761\n",
      "Sentence: Alice asked in a tone of great curiosity.\n",
      "\n",
      "Ranking: 3 | Score: 0.5397\n",
      "Sentence: “I should like to hear her try and repeat something now.\n",
      "\n",
      "Ranking: 4 | Score: 0.5268\n",
      "Sentence: “Thinking again?” the Duchess asked, with another dig of her sharp little chin.\n",
      "\n",
      "Ranking: 5 | Score: 0.5226\n",
      "Sentence: Lastly, she pictured to herself how this same little sister of hers would, in the after time, be herself a grown woman; and how she would keep, through all her riper years, the simple and loving heart of her childhood: and how she would gather about her other little children, and make their eyes bright and eager with many a strange tale, perhaps even with the dream of Wonderland of long ago: and how she would feel with all their simple sorrows, and find a pleasure in all their simple joys, remembering her own child life, and the happy summer days.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "240a8ea5-4b69-4683-b39c-3ef95e8d313e",
   "metadata": {},
   "source": [
    "### all-distilroberta-v1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "2ab2266d-c81b-4f96-bfb3-c877a7c972ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'sentence-transformers/all-distilroberta-v1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0fd6973f-2aa4-4529-a185-07633c72ea5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "997d2f3ba32e4b419ad2901649d3650f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: sentence-transformers/all-distilroberta-v1.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.5484\n",
      "Sentence: Alice asked in a tone of great curiosity.\n",
      "\n",
      "Ranking: 2 | Score: 0.5468\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n",
      "Ranking: 3 | Score: 0.4857\n",
      "Sentence: How she longed to get out of that dark hall, and wander about among those beds of bright flowers and those cool fountains, but she could not even get her head through the doorway; “and even if my head would go through,” thought poor Alice, “it would be of very little use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I could, if I only knew how to begin.”\n",
      "\n",
      "Ranking: 4 | Score: 0.4744\n",
      "Sentence: “What can all that green stuff be?” said Alice.\n",
      "\n",
      "Ranking: 5 | Score: 0.4597\n",
      "Sentence: “How can you learn lessons in here? Why, there’s hardly room for you , and no room at all for any lesson books!” And so she went on, taking first one side and then the other, and making quite a conversation of it altogether; but after a few minutes she heard a voice outside, and stopped to listen.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd5af838-f211-46a0-bd3d-767b4c37161f",
   "metadata": {},
   "source": [
    "### paraphrase-distilroberta-base-v1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "6d0044fe-e9f2-45ca-802c-6515eebbb8f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'sentence-transformers/paraphrase-distilroberta-base-v1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "a375261e-0b3e-4be3-bbc5-cdcf07a39adb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "0e5703b4e2bb490bad8bbd2eee63be9c",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: sentence-transformers/paraphrase-distilroberta-base-v1.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.4377\n",
      "Sentence: “I’m sure I’m not Ada,” she said, “for her hair goes in such long ringlets, and mine doesn’t go in ringlets at all; and I’m sure I can’t be Mabel, for I know all sorts of things, and she, oh! she knows such a very little! Besides, she’s she, and I’m I, and-oh dear, how puzzling it all is! I’ll try if I know all the things I used to know. Let me see: four times five is twelve, and four times six is thirteen, and four times seven is-oh dear! I shall never get to twenty at that rate! However, the Multiplication Table doesn’t signify: let’s try Geography. London is the capital of Paris, and Paris is the capital of Rome, and Rome-no, that’s all wrong, I’m certain! I must have been changed for Mabel! I’ll try and say ‘ How doth the little -’” and she crossed her hands on her lap as if she were saying lessons, and began to repeat it, but her voice sounded hoarse and strange, and the words did not come the same as they used to do:- “How doth the little crocodile Improve his shining tail, And pour the waters of the Nile On every golden scale! “How cheerfully he seems to grin, How neatly spread his claws, And welcome little fishes in With gently smiling jaws!” “I’m sure those are not the right words,” said poor Alice, and her eyes filled with tears again as she went on, “I must be Mabel after all, and I shall have to go and live in that poky little house, and have next to no toys to play with, and oh! ever so many lessons to learn! No, I’ve made up my mind about it; if I’m Mabel, I’ll stay down here! It’ll be no use their putting their heads down and saying ‘Come up again, dear!’ I shall only look up and say ‘Who am I then? Tell me that first, and then, if I like being that person, I’ll come up: if not, I’ll stay down here till I’m somebody else’-but, oh dear!” cried Alice, with a sudden burst of tears, “I do wish they would put their heads down! I am so very tired of being all alone here!” As she said this she looked down at her hands, and was surprised to see that she had put on one of the Rabbit’s little white kid gloves while she was talking.\n",
      "\n",
      "Ranking: 2 | Score: 0.4351\n",
      "Sentence: “Does the boots and shoes!” she repeated in a wondering tone.\n",
      "\n",
      "Ranking: 3 | Score: 0.3910\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n",
      "Ranking: 4 | Score: 0.3788\n",
      "Sentence: (she couldn’t guess of what sort it was)\n",
      "\n",
      "Ranking: 5 | Score: 0.3776\n",
      "Sentence: “I mean, what makes them so shiny?” Alice looked down at them, and considered a little before she gave her answer.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e21b384b-2429-4a1c-b879-dd2971d0028e",
   "metadata": {},
   "source": [
    "### distiluse-base-multilingual-cased-v2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "63494df6-55a9-48c7-8faf-a9f733aa979c",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'sentence-transformers/distiluse-base-multilingual-cased-v2'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "6c5dfa01-5d19-426f-b9cf-2ccbeecd5cf8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "b88ab9952a614f76bbe74975a2e0d964",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: sentence-transformers/distiluse-base-multilingual-cased-v2.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.5158\n",
      "Sentence: Alice asked in a tone of great curiosity.\n",
      "\n",
      "Ranking: 2 | Score: 0.4163\n",
      "Sentence: She felt very curious to know what it was all about, and crept a little way out of the wood to listen.\n",
      "\n",
      "Ranking: 3 | Score: 0.3528\n",
      "Sentence: “What a funny watch!” she remarked.\n",
      "\n",
      "Ranking: 4 | Score: 0.3270\n",
      "Sentence: “Does the boots and shoes!” she repeated in a wondering tone.\n",
      "\n",
      "Ranking: 5 | Score: 0.3072\n",
      "Sentence: “Is that the reason so many tea things are put out here?” she asked.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "47897b4c-9093-48f9-9f9d-ce337f599a1d",
   "metadata": {},
   "source": [
    "### msmarco-distilbert-cos-v5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "24c76fcd-d862-428d-8453-61f13974bb09",
   "metadata": {},
   "outputs": [],
   "source": [
    "model_name = 'sentence-transformers/msmarco-distilbert-cos-v5'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "00bc4190-b425-43b8-9adc-612a793066e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "65dbbcbcdb6a4939a9103b90ebb9abb8",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Starting:   0%|          | 0/5 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model name is: sentence-transformers/msmarco-distilbert-cos-v5.\n",
      "\n",
      "Input query is: She wonders about things.\n",
      "\n",
      "Ranking: 1 | Score: 0.4943\n",
      "Sentence: Alice said; but was dreadfully puzzled by the whole thing, and longed to change the subject.\n",
      "\n",
      "Ranking: 2 | Score: 0.4883\n",
      "Sentence: “I can see you’re trying to invent something!” “I-I’m a little girl,” said Alice, rather doubtfully, as she remembered the number of changes she had gone through that day.\n",
      "\n",
      "Ranking: 3 | Score: 0.4587\n",
      "Sentence: “Why, she ,” said the Gryphon.\n",
      "\n",
      "Ranking: 4 | Score: 0.4543\n",
      "Sentence: (she couldn’t guess of what sort it was)\n",
      "\n",
      "Ranking: 5 | Score: 0.4460\n",
      "Sentence: How she longed to get out of that dark hall, and wander about among those beds of bright flowers and those cool fountains, but she could not even get her head through the doorway; “and even if my head would go through,” thought poor Alice, “it would be of very little use without my shoulders. Oh, how I wish I could shut up like a telescope! I think I could, if I only knew how to begin.”\n",
      "\n"
     ]
    }
   ],
   "source": [
    "process(model_name, input_query, sentences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3ec6ef3-8b54-4308-b4d8-a67ef5f2882f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
