Spaces:
Running
Running
Update app.py
Browse files
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
|
46 |
tokens = nltk.pos_tag(word_tokenize(sentence))
|
47 |
-
|
48 |
-
current_phrase = []
|
49 |
for word, tag in tokens:
|
50 |
if tag.startswith('NN'):
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
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
|
64 |
-
|
65 |
|
66 |
-
if not
|
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] =
|
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
|