afkdark commited on
Commit
0340678
·
verified ·
1 Parent(s): 5918661

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -26
app.py CHANGED
@@ -68,23 +68,30 @@ def get_named_entities(text):
68
  return []
69
 
70
  def generate_question_from_sentence(sentence):
71
- """Generate a question from a sentence with robust error handling."""
72
  try:
73
  # Remove punctuation at the end
74
  sentence = re.sub(r'[.!?]$', '', sentence)
75
 
76
- # Check for common patterns that can be turned into questions
77
- if re.search(r'\bis\s|\bwas\s|\bwere\s|\bare\s', sentence):
78
- # Convert statements with "is", "was", "were", "are" into yes/no questions
79
- match = re.search(r'^(.*?)\s(is|was|were|are)\s(.*?)$', sentence, re.IGNORECASE)
80
- if match:
81
- return f"{match.group(2).capitalize()} {match.group(1)} {match.group(3)}?"
82
 
83
- # Check for sentences with dates or years
 
 
 
 
 
84
  if re.search(r'\b(in|on|during)\s\d{4}\b|\b(January|February|March|April|May|June|July|August|September|October|November|December)\b', sentence, re.IGNORECASE):
85
  return f"When did {sentence.lower()}?"
86
 
87
- # Try to get named entities, but don't fail if NER isn't working
 
 
 
 
88
  entities = get_named_entities(sentence)
89
 
90
  # If there are named entities, ask about them
@@ -96,31 +103,47 @@ def generate_question_from_sentence(sentence):
96
  return f"Where is {entity}?"
97
  elif entity_type == 'ORGANIZATION':
98
  return f"What is {entity}?"
 
 
99
 
100
- # Check for sentences with "because", "due to", "as a result"
101
- if re.search(r'\bbecause\b|\bdue to\b|\bas a result\b', sentence, re.IGNORECASE):
102
- return f"Why {sentence.lower()}?"
103
 
104
- # Simplified approach without relying on POS tagging
105
  words = sentence.split()
106
-
107
- # Very simple subject extraction (first word)
108
- if words:
109
- subject = words[0]
110
- if subject.lower() in ['i', 'you', 'we', 'they', 'he', 'she', 'it', 'this', 'that']:
111
- return f"What did {subject.lower()} do?"
 
 
 
 
112
  else:
113
- return f"What about {subject}?"
 
 
 
 
 
 
 
114
 
115
- # Very generic fallback
116
  question_starters = [
117
- "What is described in",
118
- "What is mentioned about",
119
- "Can you explain",
120
- "Could you elaborate on"
 
 
121
  ]
122
 
123
- return f"{random.choice(question_starters)} the statement: '{sentence}'?"
 
124
  except Exception as e:
125
  print(f"Question generation failed: {str(e)}")
126
  return f"What can you tell me about: '{sentence}'?"
 
68
  return []
69
 
70
  def generate_question_from_sentence(sentence):
71
+ """Generate a question from a sentence with improved question formation."""
72
  try:
73
  # Remove punctuation at the end
74
  sentence = re.sub(r'[.!?]$', '', sentence)
75
 
76
+ # Convert statements with be-verbs into yes/no questions
77
+ be_verb_pattern = re.search(r'^(.*?)\s(is|was|were|are|am)\s(.*?)$', sentence, re.IGNORECASE)
78
+ if be_verb_pattern:
79
+ return f"{be_verb_pattern.group(2).capitalize()} {be_verb_pattern.group(1)} {be_verb_pattern.group(3)}?"
 
 
80
 
81
+ # Check for modal verbs
82
+ modal_pattern = re.search(r'^(.*?)\s(can|could|will|would|should|may|might)\s(.*?)$', sentence, re.IGNORECASE)
83
+ if modal_pattern:
84
+ return f"{modal_pattern.group(2).capitalize()} {modal_pattern.group(1)} {modal_pattern.group(3)}?"
85
+
86
+ # Check for sentences with temporal markers
87
  if re.search(r'\b(in|on|during)\s\d{4}\b|\b(January|February|March|April|May|June|July|August|September|October|November|December)\b', sentence, re.IGNORECASE):
88
  return f"When did {sentence.lower()}?"
89
 
90
+ # Check for causal relationships
91
+ if re.search(r'\bbecause\b|\bdue to\b|\bas a result\b|\btherefore\b|\bhence\b', sentence, re.IGNORECASE):
92
+ return f"Why {sentence.lower()}?"
93
+
94
+ # Try to get named entities
95
  entities = get_named_entities(sentence)
96
 
97
  # If there are named entities, ask about them
 
103
  return f"Where is {entity}?"
104
  elif entity_type == 'ORGANIZATION':
105
  return f"What is {entity}?"
106
+ else:
107
+ return f"Can you tell me more about {entity}?"
108
 
109
+ # Check for quantifiable content
110
+ if re.search(r'\b(many|number of|several|few|multiple)\b', sentence, re.IGNORECASE):
111
+ return f"How many are mentioned in the statement: '{sentence}'?"
112
 
113
+ # Look for action verbs to create "what" questions
114
  words = sentence.split()
115
+ if len(words) >= 3:
116
+ # Basic subject-verb detection
117
+ potential_subject = words[0]
118
+ potential_verb = words[1]
119
+
120
+ # Common pronouns and determiners
121
+ pronouns = ['i', 'you', 'we', 'they', 'he', 'she', 'it', 'this', 'that', 'these', 'those', 'a', 'an', 'the']
122
+
123
+ if potential_subject.lower() in pronouns:
124
+ return f"What did {potential_subject.lower()} {' '.join(words[1:])}?"
125
  else:
126
+ # Try to identify the main topic
127
+ # First, remove common stop words
128
+ stop_words = ['a', 'an', 'the', 'and', 'but', 'or', 'for', 'nor', 'on', 'at', 'to', 'from', 'by']
129
+ content_words = [word for word in words if word.lower() not in stop_words]
130
+
131
+ if content_words:
132
+ main_topic = content_words[0]
133
+ return f"What is significant about {main_topic}?"
134
 
135
+ # More varied generic fallbacks
136
  question_starters = [
137
+ "What is important about",
138
+ "How would you describe",
139
+ "What are the key aspects of",
140
+ "What's notable regarding",
141
+ "How does the text characterize",
142
+ "What insights can be drawn from"
143
  ]
144
 
145
+ return f"{random.choice(question_starters)} this: '{sentence}'?"
146
+
147
  except Exception as e:
148
  print(f"Question generation failed: {str(e)}")
149
  return f"What can you tell me about: '{sentence}'?"