1+ import os
12from sentence_transformers import SentenceTransformer , util
23
4+ MODEL_NAME = 'all-MiniLM-L6-v2'
5+ MODEL_FOLDER = 'model'
6+
37def load_file (file_path ):
48 with open (file_path , 'r' , encoding = 'utf-8' ) as file :
59 return [line .strip () for line in file if line .strip ()]
610
11+ def load_or_download_model ():
12+ model_path = os .path .join (MODEL_FOLDER , MODEL_NAME )
13+ if os .path .exists (model_path ):
14+ print (f"Loading model from { model_path } " )
15+ return SentenceTransformer (model_path )
16+ else :
17+ print (f"Downloading model { MODEL_NAME } " )
18+ model = SentenceTransformer (MODEL_NAME )
19+ os .makedirs (MODEL_FOLDER , exist_ok = True )
20+ model .save (model_path )
21+ print (f"Model saved to { model_path } " )
22+ return model
23+
724def find_similar_sentences (query , file_path , top_n = 5 ):
825 # Load the pre-trained model
9- model = SentenceTransformer ( 'all-MiniLM-L6-v2' )
26+ model = load_or_download_model ( )
1027
1128 # Load and encode the sentences from the file
1229 sentences = load_file (file_path )
@@ -23,14 +40,27 @@ def find_similar_sentences(query, file_path, top_n=5):
2340
2441 return top_results
2542
43+ def validate_file_path (file_path ):
44+ if not file_path .endswith ('.txt' ):
45+ file_path += '.txt'
46+ if not os .path .exists (file_path ):
47+ raise FileNotFoundError (f"The file '{ file_path } ' does not exist." )
48+ return file_path
49+
2650def main ():
2751 print ("Welcome to the Sentence Similarity Search Tool!" )
2852
2953 # Get user input for query
3054 query = input ("Enter your query: " )
3155
32- # Get user input for file path
33- file_path = input ("Enter the path to your text file: " )
56+ # Get user input for file path and validate it
57+ while True :
58+ file_path = input ("Enter the path to your text file without extension: " )
59+ try :
60+ file_path = validate_file_path (file_path )
61+ break
62+ except FileNotFoundError as e :
63+ print (f"Error: { str (e )} Please try again." )
3464
3565 try :
3666 results = find_similar_sentences (query , file_path )
@@ -39,8 +69,6 @@ def main():
3969 for sentence , score in results :
4070 print (f"Similarity: { score :.4f} " )
4171 print (f"Sentence: { sentence } \n " )
42- except FileNotFoundError :
43- print (f"Error: The file '{ file_path } ' was not found. Please check the file path and try again." )
4472 except Exception as e :
4573 print (f"An error occurred: { str (e )} " )
4674
0 commit comments