Add sentence offset to spell check results

Without that property, the offset always just starts at the current
sentence, not the whole user input
This commit is contained in:
Ramon Wenger 2020-02-03 15:04:52 +01:00
parent 5a58641183
commit ec1d68dd51
3 changed files with 20 additions and 7 deletions

View File

@ -16,17 +16,18 @@
let parts = []; let parts = [];
let index = 0; let index = 0;
[...this.corrections] // no side effects, as sort changes the source array [...this.corrections] // no side effects, as sort changes the source array
.sort((e1, e2) => e1.offset - e2.offset) .sort((e1, e2) => (e1.offset + e1.sentenceOffset) - (e2.offset + e2.sentenceOffset))
.forEach(current => { .forEach(current => {
let realOffset = current.offset + current.sentenceOffset;
parts.push({ parts.push({
correct: true, correct: true,
text: this.text.substring(index, current.offset) text: this.text.substring(index, realOffset)
}); });
parts.push({ parts.push({
correct: false, correct: false,
text: this.text.substring(current.offset, current.offset + current.length) text: this.text.substring(realOffset, realOffset + current.length)
}); });
index = current.offset + current.length index = realOffset + current.length
}); });
parts.push({ parts.push({
correct: true, correct: true,

View File

@ -4,6 +4,7 @@ mutation SpellCheck($input: SpellCheckInput!) {
results { results {
sentence sentence
offset offset
sentenceOffset
length length
affected affected
corrected corrected

View File

@ -15,11 +15,17 @@ from assignments.models import Assignment
from spellcheck.client import TaskbaseClient from spellcheck.client import TaskbaseClient
def sentence_offset_to_snake_case(result):
result['sentence_offset'] = result['sentenceOffset']
return result
class SpellCheckStepNode(graphene.ObjectType): class SpellCheckStepNode(graphene.ObjectType):
# id = graphene.String() # id = graphene.String()
# part = graphene.Field(SpellCheckPartNode) # part = graphene.Field(SpellCheckPartNode)
sentence = graphene.String() sentence = graphene.String()
offset = graphene.Int() offset = graphene.Int()
sentence_offset = graphene.Int()
length = graphene.Int() length = graphene.Int()
affected = graphene.String() affected = graphene.String()
corrected = graphene.String() corrected = graphene.String()
@ -45,13 +51,18 @@ class SpellCheck(relay.ClientIDMutation):
if assignment.taskbase_id is None or assignment.taskbase_id == '': if assignment.taskbase_id is None or assignment.taskbase_id == '':
# we need to use another user (with more privileges) here, because why not # we need to use another user (with more privileges) here, because why not
superclient = TaskbaseClient(settings.TASKBASE_SUPERUSER, settings.TASKBASE_SUPERPASSWORD, settings.TASKBASE_BASEURL) superclient = TaskbaseClient(settings.TASKBASE_SUPERUSER, settings.TASKBASE_SUPERPASSWORD,
settings.TASKBASE_BASEURL)
superclient.register_assignment(assignment) superclient.register_assignment(assignment)
data = json.loads(client.spellcheck(assignment.taskbase_id, text)) data = json.loads(client.spellcheck(assignment.taskbase_id, text))
# the property is called sentenceOffset, but graphene expects the property to be called sentence_offset.
return cls(correct=data['correct'] == 'CORRECT', results=map(lambda x: x['part'], data['steps'])) # we convert it manually here. fixme: is there a better way to declare this in the SpellCheckStepNode?
transformed_results = list(map(lambda x: x['part'], data['steps']))
return cls(correct=data['correct'] == 'CORRECT', results=[
sentence_offset_to_snake_case(result) for result in transformed_results
])
class SpellCheckMutations: class SpellCheckMutations: