Shreyas094 commited on
Commit
7849340
·
verified ·
1 Parent(s): 53b9156

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -13
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'): # 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
 
 
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