Shreyas094 commited on
Commit
0bd56d6
·
verified ·
1 Parent(s): 2982f30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -42,39 +42,33 @@ class Agent1:
42
  text.strip().endswith('?') or
43
  any(word in self.question_words for word in words))
44
 
45
- def find_noun_phrases(self, sentence):
46
  tokens = nltk.pos_tag(word_tokenize(sentence))
47
- noun_phrases = []
48
- current_phrase = []
49
  for word, tag in tokens:
50
  if tag.startswith('NN'):
51
- current_phrase.append(word)
52
- elif current_phrase:
53
- noun_phrases.append(' '.join(current_phrase))
54
- current_phrase = []
55
- if current_phrase:
56
- noun_phrases.append(' '.join(current_phrase))
57
- return noun_phrases
58
 
59
  def replace_pronoun(self, questions: List[str]) -> List[str]:
60
  if len(questions) < 2:
61
  return questions
62
 
63
- # Find noun phrases in the first question
64
- noun_phrases = self.find_noun_phrases(questions[0])
65
 
66
- if not noun_phrases:
67
  return questions
68
 
69
- # Use the last noun phrase as the antecedent
70
- antecedent = noun_phrases[-1]
71
-
72
  # Replace pronouns in subsequent questions
73
  for i in range(1, len(questions)):
74
  words = word_tokenize(questions[i])
75
  for j, word in enumerate(words):
76
  if word.lower() in self.pronouns:
77
- words[j] = antecedent
78
  questions[i] = ' '.join(words)
79
 
80
  return questions
 
42
  text.strip().endswith('?') or
43
  any(word in self.question_words for word in words))
44
 
45
+ def find_subject(self, sentence):
46
  tokens = nltk.pos_tag(word_tokenize(sentence))
47
+ subject = None
 
48
  for word, tag in tokens:
49
  if tag.startswith('NN'):
50
+ subject = word
51
+ break
52
+ if tag == 'IN': # Stop at preposition
53
+ break
54
+ return subject
 
 
55
 
56
  def replace_pronoun(self, questions: List[str]) -> List[str]:
57
  if len(questions) < 2:
58
  return questions
59
 
60
+ # Find the subject in the first question
61
+ subject = self.find_subject(questions[0])
62
 
63
+ if not subject:
64
  return questions
65
 
 
 
 
66
  # Replace pronouns in subsequent questions
67
  for i in range(1, len(questions)):
68
  words = word_tokenize(questions[i])
69
  for j, word in enumerate(words):
70
  if word.lower() in self.pronouns:
71
+ words[j] = subject
72
  questions[i] = ' '.join(words)
73
 
74
  return questions