Skip to content

Commit

Permalink
Move hint parsing from front-end to back-end (#149)
Browse files Browse the repository at this point in the history
* Completed and tested infer_hint_type in parser

* Removed inferHintType from Quiz index.js

* Add hint_type back to Quiz index

* Comments removed

* Removed another useless comment

* Remove comment

* Revert print statements and remove another useless comment
  • Loading branch information
stevenelleman authored and normangilmore committed Sep 22, 2017
1 parent a67133d commit dcaf940
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
19 changes: 2 additions & 17 deletions app/components/Quiz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,6 @@ import { introJs } from 'intro.js/intro.js';
import { abridgeText,
getAnswerAnnotations } from 'components/Quiz/contextWords';

// note, will also capture 'whom' and 'whose'
const re_hinttype = /^(WHERE|WHO|HOW MANY|WHEN)/i;

function inferHintType(question_text) {
// data.nlp_hint_types.py: 'WHERE', 'WHO', 'HOW MANY', 'WHEN', 'NONE'
let match = question_text.match(re_hinttype);
if (match) {
return match[1].toUpperCase();
};
return 'NONE';
};

function eqSet(as, bs) {
if (as.size !== bs.size) return false;
for (var a of as) if (!bs.has(a)) return false;
Expand Down Expand Up @@ -291,7 +279,7 @@ export class Quiz extends Component {
: <div></div>;
return (
<div key={question.id}>
<Question question={question} />
<Question question={question} />
{ buttons }
</div>
);
Expand Down Expand Up @@ -370,10 +358,7 @@ export class Quiz extends Component {
const question_id = this.props.question_id;
let hint_type = 'NONE'; // data.nlp_hint_types.py: 'WHO', 'HOW MANY', 'WHEN', 'NONE'
if (questionDB && questionDB[question_id]) {
// DB not yet populated with this field
// hint_type = questionDB[question_id].hint_type;
// Use the prior technique of using the first word to set the hint type.
hint_type = inferHintType(questionDB[question_id].question_text);
hint_type = questionDB[question_id].hint_type;
};
if (this.props.displayHintSelectControl) {
hint_type = this.props.displayHintType;
Expand Down
20 changes: 15 additions & 5 deletions data/parse_schema.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import argparse
from collections import namedtuple
import re

TITLE_ID = 'title:'
INSTRUCTIONS_ID = 'instructions:'
Expand All @@ -15,7 +16,7 @@
'dt' : 'DATE',
'tm' : 'TIME'}

Dependency = namedtuple('Dependency',
Dependency = namedtuple('Dependency',
['topic', 'question', 'answer', 'next_question'])

def load_defaults(output):
Expand Down Expand Up @@ -63,7 +64,7 @@ def parse_glossary(glossary_entry, output):
output['glossary'][term.strip()] = definition.strip()

def parse_dependency(dependency, output):

splitted_dependency = dependency.split(', ')
source_phrase = splitted_dependency[0]
target_phrase = splitted_dependency[1].split(' ')[1]
Expand All @@ -82,6 +83,12 @@ def parse_dependency(dependency, output):
source_answer_id,
target_question))

def infer_hint_type(question):
match = re.search("WHERE|WHO|HOW MANY|WHEN", question, re.IGNORECASE)
if match:
return match.group(0).upper()
else:
return 'NONE';

def parse_question_entry(entry_id, data, output):
type_bits = entry_id.split('.')
Expand All @@ -90,7 +97,7 @@ def parse_question_entry(entry_id, data, output):
try:
topics_id = int(type_bits[0])
except ValueError:
return
return
topic_id = type_bits[0]
if 'topics' not in output:
output['topics'] = []
Expand All @@ -104,13 +111,16 @@ def parse_question_entry(entry_id, data, output):
question_id = type_bits[1]
topic = [t for t in output['topics'] if t['id'] == topic_id][0]
question_type, question_text = data.split(None, 1)
hint_type = infer_hint_type(question_text)
if question_type in QUESTION_TYPES:
question_type = QUESTION_TYPES[question_type]
topic['questions'].append({
'question_number': question_id,
'question_text': question_text,
'question_type': question_type,
'answers': [],
'hint_type': hint_type,

})
else:
topic_id, question_id, answer_id = type_bits
Expand All @@ -134,8 +144,8 @@ def print_dependencies(output):
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('filename', nargs=1)
args = arg_parser.parse_args()

output = parse_schema(args.filename[0])
print_data(output)
# print_dependencies(output)

# print_dependencies(output)

0 comments on commit dcaf940

Please sign in to comment.