Spaces:
Runtime error
Runtime error
Commit
·
5a44472
1
Parent(s):
8266f12
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import random
|
3 |
+
import spacy
|
4 |
+
import requests
|
5 |
+
from bs4 import BeautifulSoup
|
6 |
+
import re
|
7 |
+
import spacy
|
8 |
+
import language_tool_python
|
9 |
+
|
10 |
+
# Initialize LanguageTool
|
11 |
+
tool = language_tool_python.LanguageToolPublicAPI('en-US')
|
12 |
+
|
13 |
+
# Define the Streamlit app
|
14 |
+
st.title("NLP Testing and Scoring App")
|
15 |
+
|
16 |
+
# Web scraping and text cleaning
|
17 |
+
entity = "Canada"
|
18 |
+
prefix = "https://wiki.kidzsearch.com/wiki/"
|
19 |
+
page = requests.get(f'{prefix}{entity}')
|
20 |
+
res = BeautifulSoup(page.content, 'html.parser')
|
21 |
+
|
22 |
+
text = [i.get_text() for i in res.find_all('p')]
|
23 |
+
|
24 |
+
cleaned_text = ' '.join(text)
|
25 |
+
cleaned_text = re.sub(r'[^a-zA-Z0-9.,]', ' ', cleaned_text)
|
26 |
+
paragraphs = [p.strip() for p in re.split(r'\n', cleaned_text) if p.strip()]
|
27 |
+
|
28 |
+
# Process text using SpaCy
|
29 |
+
nlp = spacy.load("en_core_web_sm")
|
30 |
+
doc = nlp(cleaned_text)
|
31 |
+
|
32 |
+
sentences = [sent.text for sent in doc.sents]
|
33 |
+
|
34 |
+
# Combine sentences into paragraphs
|
35 |
+
paragraphs = [f"{sentences[i]} {sentences[i + 1]}" if i + 1 < len(sentences) else sentences[i] for i in range(0, len(sentences), 2)]
|
36 |
+
|
37 |
+
class SubjectiveTest:
|
38 |
+
|
39 |
+
def __init__(self, data, noOfQues):
|
40 |
+
self.summary = data
|
41 |
+
self.noOfQues = noOfQues
|
42 |
+
self.nlp = spacy.load("en_core_web_sm")
|
43 |
+
|
44 |
+
def adjust_question_pattern(self, entity_label, topic_placeholder=True):
|
45 |
+
question_patterns = {
|
46 |
+
"PERSON": ["Who is {entity}?", "Tell me about {entity}", "Explain {entity}", "What do you know about {entity}"],
|
47 |
+
"ORG": ["What is {entity}?", "Tell me about {entity}", "Explain {entity}", "What do you know about {entity}"],
|
48 |
+
"GPE": ["Tell me about {entity}", "Explain {entity}", "What do you know about {entity}", "Describe {entity}", "Where is {entity}"],
|
49 |
+
"MONEY": ["How much is {entity}?", "Tell me the value of {entity}", "Explain the amount of {entity}"],
|
50 |
+
"DATE": ["Why was {entity} important?", "Explain what happened on {entity}"],
|
51 |
+
# Add more entity-label to question-pattern mappings as needed
|
52 |
+
}
|
53 |
+
|
54 |
+
if topic_placeholder:
|
55 |
+
for key in question_patterns:
|
56 |
+
question_patterns[key] = [pattern + " {topic}" for pattern in question_patterns[key]]
|
57 |
+
|
58 |
+
return question_patterns.get(entity_label, ["Explain {entity} {topic}"])
|
59 |
+
|
60 |
+
def generate_test(self, topic=None):
|
61 |
+
doc = self.nlp(self.summary)
|
62 |
+
question_answer_dict = dict()
|
63 |
+
|
64 |
+
for sentence in doc.sents:
|
65 |
+
for ent in sentence.ents:
|
66 |
+
entity_label = ent.label_
|
67 |
+
entity_text = ent.text
|
68 |
+
question_patterns = self.adjust_question_pattern(entity_label, topic is not None)
|
69 |
+
for pattern in question_patterns:
|
70 |
+
question = pattern.format(entity=entity_text, topic=topic)
|
71 |
+
if entity_label in question_answer_dict:
|
72 |
+
question_answer_dict[entity_label].append(question)
|
73 |
+
else:
|
74 |
+
question_answer_dict[entity_label] = [question]
|
75 |
+
|
76 |
+
questions = []
|
77 |
+
|
78 |
+
for entity_label, entity_questions in question_answer_dict.items():
|
79 |
+
entity_questions = entity_questions[:self.noOfQues]
|
80 |
+
questions.extend(entity_questions)
|
81 |
+
|
82 |
+
return questions
|
83 |
+
|
84 |
+
# Example usage
|
85 |
+
data = ' '.join(paragraphs)
|
86 |
+
noOfQues = 5
|
87 |
+
|
88 |
+
subjective_generator = SubjectiveTest(data, noOfQues)
|
89 |
+
question_list = subjective_generator.generate_test("")
|
90 |
+
questions = []
|
91 |
+
for i, question in enumerate(question_list):
|
92 |
+
if "Explain" not in question and len(tool.check(question)) == 0 and grammar_sense(question) == "Make Sense":
|
93 |
+
questions.append(f"Question: {question}")
|
94 |
+
|
95 |
+
scores = []
|
96 |
+
|
97 |
+
# Now, add a Streamlit UI for input and displaying scores
|
98 |
+
st.subheader("Answer the following questions:")
|
99 |
+
user_responses = {}
|
100 |
+
for i, question in enumerate(questions):
|
101 |
+
response = st.text_area(f"Question {i + 1}", key=f"response_{i}")
|
102 |
+
user_responses[f"response_{i}"] = response
|
103 |
+
|
104 |
+
if st.button("Calculate Scores"):
|
105 |
+
for i in questions:
|
106 |
+
res = user_responses[f"response_{i}"]
|
107 |
+
# Simulate the scoring process, replace with your actual scoring logic
|
108 |
+
score = random.randint(0, 100)
|
109 |
+
scores.append(score)
|
110 |
+
|
111 |
+
x = 0
|
112 |
+
new_scores = []
|
113 |
+
for i in scores:
|
114 |
+
if i == 'N/A':
|
115 |
+
scores.pop(x)
|
116 |
+
scores.append(85)
|
117 |
+
x = x + 1
|
118 |
+
else:
|
119 |
+
x = x + 1
|
120 |
+
|
121 |
+
def calculate_average(numbers):
|
122 |
+
if not numbers:
|
123 |
+
return 0 # Return 0 for an empty list to avoid division by zero.
|
124 |
+
|
125 |
+
total = sum(numbers)
|
126 |
+
average = total / len(numbers)
|
127 |
+
return average
|
128 |
+
|
129 |
+
st.subheader(f'Your average score is {calculate_average(scores)}')
|