Spaces:
Sleeping
Sleeping
Delete simple_question_generator.py
Browse files- simple_question_generator.py +0 -76
simple_question_generator.py
DELETED
|
@@ -1,76 +0,0 @@
|
|
| 1 |
-
import re
|
| 2 |
-
import random
|
| 3 |
-
|
| 4 |
-
class SimpleQuestionGenerator:
|
| 5 |
-
def __init__(self):
|
| 6 |
-
self.question_words = [
|
| 7 |
-
'What', 'Why', 'How', 'When', 'Where', 'Who', 'Which', 'Describe', 'Explain'
|
| 8 |
-
]
|
| 9 |
-
self.auxiliary_verbs = ['is', 'are', 'was', 'were', 'do', 'does', 'did', 'have', 'has', 'had']
|
| 10 |
-
|
| 11 |
-
def generate_question(self, sentence):
|
| 12 |
-
"""Generate a simple question from a given sentence."""
|
| 13 |
-
if not sentence.strip():
|
| 14 |
-
return ""
|
| 15 |
-
|
| 16 |
-
# Clean the sentence
|
| 17 |
-
sentence = sentence.strip().strip('.').strip()
|
| 18 |
-
|
| 19 |
-
# Simple question patterns
|
| 20 |
-
patterns = [
|
| 21 |
-
(r'^(.*?) is (.*?)[.,;]?$', 'What is {}?'),
|
| 22 |
-
(r'^(.*?) are (.*?)[.,;]?$', 'What are {}?'),
|
| 23 |
-
(r'^(.*?) was (.*?)[.,;]?$', 'What was {}?'),
|
| 24 |
-
(r'^(.*?) were (.*?)[.,;]?$', 'What were {}?'),
|
| 25 |
-
(r'^(.*?) can be (.*?)[.,;]?$', 'How can {} be {}?'),
|
| 26 |
-
(r'^(.*?) has (.*?)[.,;]?$', 'What has {}?'),
|
| 27 |
-
(r'^(.*?) have (.*?)[.,;]?$', 'What have {}?'),
|
| 28 |
-
]
|
| 29 |
-
|
| 30 |
-
# Try to match patterns
|
| 31 |
-
for pattern, template in patterns:
|
| 32 |
-
match = re.match(pattern, sentence, re.IGNORECASE)
|
| 33 |
-
if match:
|
| 34 |
-
return template.format(match.group(1)).capitalize()
|
| 35 |
-
|
| 36 |
-
# Default question if no pattern matches
|
| 37 |
-
return f"What is the main point about: {sentence[:50]}...?"
|
| 38 |
-
|
| 39 |
-
def generate_questions(self, text, num_questions=5):
|
| 40 |
-
"""Generate questions from the given text."""
|
| 41 |
-
# Simple sentence splitting
|
| 42 |
-
sentences = re.split(r'(?<=[.!?])\s+', text)
|
| 43 |
-
questions = []
|
| 44 |
-
|
| 45 |
-
for sentence in sentences:
|
| 46 |
-
if len(questions) >= num_questions:
|
| 47 |
-
break
|
| 48 |
-
|
| 49 |
-
question = self.generate_question(sentence)
|
| 50 |
-
if question and question not in questions:
|
| 51 |
-
questions.append(question)
|
| 52 |
-
|
| 53 |
-
return questions
|
| 54 |
-
|
| 55 |
-
# Example usage
|
| 56 |
-
if __name__ == "__main__":
|
| 57 |
-
# Create a question generator
|
| 58 |
-
qg = SimpleQuestionGenerator()
|
| 59 |
-
|
| 60 |
-
# Sample text
|
| 61 |
-
sample_text = """
|
| 62 |
-
Machine learning is a branch of artificial intelligence.
|
| 63 |
-
It focuses on building systems that learn from data.
|
| 64 |
-
These systems can improve their performance over time.
|
| 65 |
-
There are three main types of machine learning: supervised, unsupervised, and reinforcement learning.
|
| 66 |
-
Supervised learning uses labeled data to train models.
|
| 67 |
-
Unsupervised learning finds patterns in unlabeled data.
|
| 68 |
-
Reinforcement learning involves training an agent to make decisions through rewards.
|
| 69 |
-
"""
|
| 70 |
-
|
| 71 |
-
# Generate and print questions
|
| 72 |
-
print("Generating questions...\n")
|
| 73 |
-
questions = qg.generate_questions(sample_text, 5)
|
| 74 |
-
|
| 75 |
-
for i, question in enumerate(questions, 1):
|
| 76 |
-
print(f"{i}. {question}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|