Narayana02 commited on
Commit
83ea860
·
verified ·
1 Parent(s): 70fae8a

Update electronics_query.py

Browse files
Files changed (1) hide show
  1. electronics_query.py +52 -79
electronics_query.py CHANGED
@@ -1,7 +1,5 @@
1
- import os
2
- import json
3
  import pandas as pd
4
- from termcolor import colored
5
 
6
  # Load the electronics dataset
7
  def load_electronics_dataset():
@@ -11,84 +9,59 @@ def load_electronics_dataset():
11
  except Exception as e:
12
  raise Exception(f"Error loading electronics dataset: {e}")
13
 
14
- # Function to query the dataset
15
- def query_electronics(query, n_results=5):
16
- electronics_df = load_electronics_dataset()
17
-
18
- # Apply filters to the dataset based on query
19
- filtered_df = electronics_df[electronics_df.apply(lambda row: query.lower() in row['ProductName'].lower(), axis=1)]
20
-
21
- # Sort by relevance (example: based on some column like Rating or Price)
22
- sorted_df = filtered_df.sort_values(by='Rating', ascending=False)
23
-
24
- # Get top n_results
25
- top_results = sorted_df.head(n_results)
26
-
27
- return top_results
28
 
29
- # Function to save query results to a JSON file
30
- def save_results_to_json(results, query, n_results):
31
- # Create a dictionary to store the results
32
- json_results = {
33
- "query": query,
34
- "n_results": n_results,
35
- "results": []
36
- }
37
-
38
- # Iterate through the results and add them to the dictionary
39
- for i, row in results.iterrows():
40
- json_results["results"].append({
41
- "result_number": i + 1,
42
- "product_name": row['ProductName'],
43
- "category": row['Category'],
44
- "price": row['Price'],
45
- "rating": row['Rating'],
46
- "description": row['Description']
47
- })
48
-
49
- # Write the results to a JSON file
50
- with open("electronics_query_results.json", "w") as f:
51
- json.dump(json_results, f, indent=4)
52
 
53
- # Main function to run the query system
54
- def main():
55
- # Print welcome message
56
- print(colored("Welcome to the Electronics Query System!", "green"))
57
- print("You can ask questions about the electronics in the dataset.")
58
 
59
- # Main loop for user interaction
60
- while True:
61
- # Get user query
62
- query = input(colored("\nEnter your query (or 'quit' to exit): ", "yellow"))
63
-
64
- # Check if user wants to quit
65
- if query.lower() == 'quit':
66
- break
67
-
68
- # Get number of desired results
69
- n_results = int(input(colored("How many results would you like? ", "yellow")))
70
-
71
- # Query the dataset
72
- results = query_electronics(query, n_results)
73
-
74
- # Print the results
75
- print(colored("\nTop matching products:", "green"))
76
- for i, row in results.iterrows():
77
- print(colored(f"\nResult {i + 1}:", "blue"))
78
- print(f"Product: {row['ProductName']}")
79
- print(f"Category: {row['Category']}")
80
- print(f"Price: ${row['Price']}")
81
- print(f"Rating: {row['Rating']}")
82
- print(colored("Description:", "cyan"))
83
- print(row['Description'])
84
-
85
- # Save results to JSON file
86
- save_results_to_json(results, query, n_results)
87
- print(colored("\nResults have been saved to electronics_query_results.json", "green"))
88
 
89
- # Print goodbye message
90
- print(colored("Thank you for using the Electronics Query System!", "green"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- # Check if the script is being run directly
93
- if __name__ == "__main__":
94
- main()
 
 
 
1
  import pandas as pd
2
+ import random
3
 
4
  # Load the electronics dataset
5
  def load_electronics_dataset():
 
9
  except Exception as e:
10
  raise Exception(f"Error loading electronics dataset: {e}")
11
 
12
+ # Define different response styles
13
+ electronics_response_templates = [
14
+ lambda row: (
15
+ f"The {row['ProductName']} is a great choice if you're looking for a {row['Category']}. "
16
+ f"Priced at ${row['Price']}, customers appreciate its {row['Description']}. "
17
+ f"Would you like to know more about the {row['ProductName']}?"
18
+ ),
19
+ lambda row: (
20
+ f"Check out the {row['ProductName']}! It's available for ${row['Price']} and is known for its {row['Description']}. "
21
+ f"What do you think about this {row['Category']}?"
22
+ ),
23
+ # Add more templates as needed
24
+ ]
 
25
 
26
+ # Generate a dynamic response
27
+ def generate_electronics_response(row):
28
+ template = random.choice(electronics_response_templates)
29
+ return template(row)
30
+
31
+ # Extract filters from the query
32
+ def extract_electronics_filters(query):
33
+ filters = {}
34
+ query_lower = query.lower()
35
+
36
+ if 'best' in query_lower and 'rating' in query_lower:
37
+ filters['Rating'] = 'max'
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ if 'phones' in query_lower:
40
+ filters['Category'] = 'phone'
41
+ elif 'laptops' in query_lower:
42
+ filters['Category'] = 'laptop'
 
43
 
44
+ return filters
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
+ # Apply filters to the electronics DataFrame
47
+ def apply_electronics_filters(df, filters):
48
+ for key, value in filters.items():
49
+ if key == 'Rating' and value == 'max':
50
+ df = df[df['Rating'] == df['Rating'].max()]
51
+ elif key in df.columns and isinstance(value, str):
52
+ df = df[df[key].str.contains(value, case=False, na=False)]
53
+ return df
54
+
55
+ # Query electronics based on user input
56
+ def query_electronics(user_query, n_results=5):
57
+ electronics_df = load_electronics_dataset()
58
+ filtered_df = apply_electronics_filters(electronics_df, extract_electronics_filters(user_query))
59
+
60
+ # Check if 'Rating' column exists before sorting
61
+ if 'Rating' in filtered_df.columns:
62
+ sorted_df = filtered_df.sort_values(by='Rating', ascending=False)
63
+ else:
64
+ sorted_df = filtered_df # Skip sorting if 'Rating' is not available
65
 
66
+ # Return the top N results
67
+ return sorted_df.head(n_results)