Spaces:
Sleeping
Sleeping
Create question_parser.py
Browse files- question_parser.py +39 -0
question_parser.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class QuestionType:
|
| 5 |
+
PERCENT = "percent"
|
| 6 |
+
RATIO = "ratio"
|
| 7 |
+
ALGEBRA = "algebra"
|
| 8 |
+
STATISTICS = "statistics"
|
| 9 |
+
PROBABILITY = "probability"
|
| 10 |
+
GEOMETRY = "geometry"
|
| 11 |
+
ARITHMETIC = "arithmetic"
|
| 12 |
+
UNKNOWN = "unknown"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def detect_question_type(question_text: str) -> str:
|
| 16 |
+
q = question_text.lower()
|
| 17 |
+
|
| 18 |
+
if "%" in q or "percent" in q or "increase" in q or "decrease" in q:
|
| 19 |
+
return QuestionType.PERCENT
|
| 20 |
+
|
| 21 |
+
if "ratio" in q or ":" in q or "proportion" in q:
|
| 22 |
+
return QuestionType.RATIO
|
| 23 |
+
|
| 24 |
+
if re.search(r"\bmean\b|\bmedian\b|\bmode\b|\bstandard deviation\b", q):
|
| 25 |
+
return QuestionType.STATISTICS
|
| 26 |
+
|
| 27 |
+
if "probability" in q or "chance" in q or "random" in q:
|
| 28 |
+
return QuestionType.PROBABILITY
|
| 29 |
+
|
| 30 |
+
if "triangle" in q or "circle" in q or "radius" in q or "area" in q:
|
| 31 |
+
return QuestionType.GEOMETRY
|
| 32 |
+
|
| 33 |
+
if re.search(r"[a-z]\s*=", q):
|
| 34 |
+
return QuestionType.ALGEBRA
|
| 35 |
+
|
| 36 |
+
if re.search(r"\d", q):
|
| 37 |
+
return QuestionType.ARITHMETIC
|
| 38 |
+
|
| 39 |
+
return QuestionType.UNKNOWN
|