import os import pandas as pd import requests import random import numpy as np from…
- User
import os import pandas as pd import requests import random import numpy as np from tqdm.auto import tqdm from sentence_transformers import SentenceTransformer, util import torch import textwrap
Read the CSV file
csv_path = "IPL2020 Commentary Data.csv" df = pd.read_csv(csv_path)
Inspect the data to check the columns
print("DataFrame columns:", df.columns)
Preprocess the text data
def text_formatter(text: str) -> str: """Performs minor formatting on text.""" if isinstance(text, str): return text.replace("\n", " ").strip() return ""
df['formatted_text'] = df['long_text'].apply(text_formatter)
Verify that the formatted_text column has been created
if 'formatted_text' not in df.columns: raise KeyError("Column 'formatted_text' was not created in the DataFrame.")
Check the first few rows to confirm the formatting
print("First few rows after formatting:\n", df[['formatted_text']].head())
Define a function to split text into chunks
def split_list(input_list: list, slice_size: int) -> list[list[str]]: """Splits the input_list into sublists of size slice_size (or as close as possible).""" return [input_list[i:i + slice_size] for i in range(0, len(input_list), slice_size)]
Split the commentary into chunks of sentences
num_sentence_chunk_size = 10
Add sentence chunks to the dataframe
df['sentence_chunks'] = df['formatted_text'].apply(lambda x: split_list(x.split('. '), num_sentence_chunk_size)) df['num_chunks'] = df['sentence_chunks'].apply(len)
Check available columns before expanding rows
print("Available columns before expanding rows:", df.columns)
Ensure that the necessary columns 'over' and 'ball' exist, if not, handle the missing columns appropriately
if 'over' not in df.columns: df['over'] = df.get('over', np.nan)
Expand the dataframe to have each chunk in a separate row
expanded_rows = []
for _, row in tqdm(df.iterrows(), total=len(df)): for sentence_chunk in row['sentence_chunks']: chunk_dict = { "over": row["over"], "sentence_chunk": " ".join(sentence_chunk), "chunk_char_count": len(" ".join(sentence_chunk)), "chunk_word_count": len(" ".join(sentence_chunk).split(" ")), "chunk_token_count": len(" ".join(sentence_chunk)) / 4 } expanded_rows.append(chunk_dict)
df_chunks = pd.DataFrame(expanded_rows) print(df_chunks.head())
Load the embedding model
embedding_model = SentenceTransformer(model_name_or_path="all-mpnet-base-v2", device="cuda" if torch.cuda.is_available() else "cpu")
Create embeddings for the sentence chunks
df_chunks['embedding'] = df_chunks['sentence_chunk'].apply(lambda x: embedding_model.encode(x, convert_to_tensor=True))
- Assistant
- User
Query: excellent batting performance
RuntimeError Traceback (most recent call last) in <cell line: 1>() ----> 1 perform_similarity_search("excellent batting performance")
in perform_similarity_search(query, top_k) 9 10 # Get the top-k results ---> 11 top_results = torch.topk(dot_scores, k=top_k) 12 13 # Display the results
RuntimeError: selected index k out of range
- Assistant
The
RuntimeError: selected index k out of rangeindicates that the value oftop_kexceeds the number of available similarity scores, meaning that our embeddings tensor might be empty or doesn't have enough entries to select from.