Update app.py
Browse files
app.py
CHANGED
|
@@ -35,6 +35,7 @@ class Agent1:
|
|
| 35 |
self.question_words = set(["what", "when", "where", "who", "whom", "which", "whose", "why", "how"])
|
| 36 |
self.conjunctions = set(["and", "or"])
|
| 37 |
self.pronouns = set(["it", "its", "they", "their", "them", "he", "his", "him", "she", "her", "hers"])
|
|
|
|
| 38 |
|
| 39 |
def is_question(self, text: str) -> bool:
|
| 40 |
words = word_tokenize(text.lower())
|
|
@@ -99,12 +100,42 @@ class Agent1:
|
|
| 99 |
|
| 100 |
return questions
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
def process(self, user_input: str) -> tuple[List[str], Dict[str, List[Dict[str, str]]]]:
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
print("Identified queries:", queries)
|
|
|
|
| 105 |
results = {}
|
| 106 |
for query in queries:
|
| 107 |
results[query] = google_search(query)
|
|
|
|
| 108 |
return queries, results
|
| 109 |
|
| 110 |
def load_document(file: NamedTemporaryFile) -> List[Document]:
|
|
|
|
| 35 |
self.question_words = set(["what", "when", "where", "who", "whom", "which", "whose", "why", "how"])
|
| 36 |
self.conjunctions = set(["and", "or"])
|
| 37 |
self.pronouns = set(["it", "its", "they", "their", "them", "he", "his", "him", "she", "her", "hers"])
|
| 38 |
+
self.context = {} # Store multiple context elements
|
| 39 |
|
| 40 |
def is_question(self, text: str) -> bool:
|
| 41 |
words = word_tokenize(text.lower())
|
|
|
|
| 100 |
|
| 101 |
return questions
|
| 102 |
|
| 103 |
+
def update_context(self, query: str):
|
| 104 |
+
tokens = nltk.pos_tag(word_tokenize(query))
|
| 105 |
+
for word, tag in tokens:
|
| 106 |
+
if tag.startswith('NN'): # Noun
|
| 107 |
+
self.context['subject'] = word
|
| 108 |
+
elif tag.startswith('JJ'): # Adjective
|
| 109 |
+
self.context['attribute'] = word
|
| 110 |
+
elif tag == 'NNP': # Proper noun (e.g., country names)
|
| 111 |
+
self.context['location'] = word
|
| 112 |
+
|
| 113 |
+
def apply_context(self, query: str) -> str:
|
| 114 |
+
words = word_tokenize(query.lower())
|
| 115 |
+
|
| 116 |
+
# Check if the query is short or contains pronouns
|
| 117 |
+
if len(words) <= 5 or any(word in self.pronouns for word in words):
|
| 118 |
+
# Apply relevant context
|
| 119 |
+
if 'location' in self.context and 'location' not in query:
|
| 120 |
+
query += f" in {self.context['location']}"
|
| 121 |
+
if 'subject' in self.context and self.context['subject'] not in query:
|
| 122 |
+
query = f"{self.context['subject']} {query}"
|
| 123 |
+
|
| 124 |
+
return query
|
| 125 |
+
|
| 126 |
def process(self, user_input: str) -> tuple[List[str], Dict[str, List[Dict[str, str]]]]:
|
| 127 |
+
# First, update context with the new input
|
| 128 |
+
self.update_context(user_input)
|
| 129 |
+
|
| 130 |
+
# Then apply context and split queries
|
| 131 |
+
contextualized_input = self.apply_context(user_input)
|
| 132 |
+
queries = self.rephrase_and_split(contextualized_input)
|
| 133 |
print("Identified queries:", queries)
|
| 134 |
+
|
| 135 |
results = {}
|
| 136 |
for query in queries:
|
| 137 |
results[query] = google_search(query)
|
| 138 |
+
|
| 139 |
return queries, results
|
| 140 |
|
| 141 |
def load_document(file: NamedTemporaryFile) -> List[Document]:
|