Update app.py
Browse files
app.py
CHANGED
|
@@ -102,24 +102,41 @@ class Agent1:
|
|
| 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'):
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
def apply_context(self, query: str) -> str:
|
| 114 |
words = word_tokenize(query.lower())
|
| 115 |
|
| 116 |
-
# Check if the query is short or
|
| 117 |
-
if len(words) <= 5 or
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
return query
|
| 125 |
|
|
|
|
| 102 |
|
| 103 |
def update_context(self, query: str):
|
| 104 |
tokens = nltk.pos_tag(word_tokenize(query))
|
| 105 |
+
important_phrases = []
|
| 106 |
+
current_phrase = []
|
| 107 |
+
|
| 108 |
for word, tag in tokens:
|
| 109 |
+
if tag.startswith('NN') or tag.startswith('JJ') or tag == 'NNP':
|
| 110 |
+
current_phrase.append(word)
|
| 111 |
+
else:
|
| 112 |
+
if current_phrase:
|
| 113 |
+
important_phrases.append(' '.join(current_phrase))
|
| 114 |
+
current_phrase = []
|
| 115 |
+
|
| 116 |
+
if current_phrase:
|
| 117 |
+
important_phrases.append(' '.join(current_phrase))
|
| 118 |
+
|
| 119 |
+
if important_phrases:
|
| 120 |
+
self.context['main_topic'] = important_phrases[0] # Use the first important phrase as main topic
|
| 121 |
+
self.context['related_topics'] = important_phrases[1:] # Store other phrases as related topics
|
| 122 |
|
| 123 |
def apply_context(self, query: str) -> str:
|
| 124 |
words = word_tokenize(query.lower())
|
| 125 |
|
| 126 |
+
# Check if the query is short, contains pronouns, or doesn't contain the main topic
|
| 127 |
+
if (len(words) <= 5 or
|
| 128 |
+
any(word in self.pronouns for word in words) or
|
| 129 |
+
(self.context.get('main_topic') and self.context['main_topic'].lower() not in query.lower())):
|
| 130 |
+
|
| 131 |
+
# Apply main topic context
|
| 132 |
+
if 'main_topic' in self.context:
|
| 133 |
+
query = f"{self.context['main_topic']} {query}"
|
| 134 |
+
|
| 135 |
+
# Apply related topics if query is very short
|
| 136 |
+
if len(words) <= 3 and 'related_topics' in self.context:
|
| 137 |
+
for topic in self.context['related_topics']:
|
| 138 |
+
if topic.lower() not in query.lower():
|
| 139 |
+
query += f" {topic}"
|
| 140 |
|
| 141 |
return query
|
| 142 |
|