-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.py
67 lines (52 loc) · 1.89 KB
/
sample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import randfacts
import random
import nltk
from nltk.tokenize import word_tokenize
from nltk.tag import pos_tag
from nltk.corpus import stopwords
# Download the necessary resources for nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('stopwords')
def get_random_fact():
return randfacts.get_fact(only_unsafe=False)
def extract_nouns_and_numbers(fact):
# Tokenize the fact into words
tokens = word_tokenize(fact)
# POS tagging to identify parts of speech
tagged = pos_tag(tokens)
# Define stopwords
stop_words = set(stopwords.words('english'))
# Extract nouns and numbers, ignoring stopwords
extracted = [word for word, pos in tagged if (pos in ['NN', 'NNS', 'NNP', 'NNPS'] or word.isdigit()) and word.lower() not in stop_words]
return extracted
def extract_answer(fact):
# Extract nouns and numbers
words = extract_nouns_and_numbers(fact)
# Select a random word or number from the list
if words:
answer = random.choice(words)
return answer
return ""
def create_question(fact, answer):
if answer:
question = fact.replace(answer, "___", 1)
return question
return False
def generate_quiz(num_questions=10):
questions = []
answers = []
while len(questions) < num_questions:
fact = get_random_fact()
answer = extract_answer(fact)
question = create_question(fact, answer)
if question:
questions.append(question)
answers.append(answer)
return questions, answers
# Generate quiz with 10 questions
quiz_questions, quiz_answers = generate_quiz(10)
# Print the questions and answers for verification
for i, (q, a) in enumerate(zip(quiz_questions, quiz_answers), start=1):
print(f"Q{i}: {q}")
print(f"A{i}: {a}")