122 lines
3.9 KiB
Python
122 lines
3.9 KiB
Python
import json
|
|
|
|
import graphene
|
|
from django.conf import settings
|
|
from graphene import relay
|
|
|
|
# class SpellCheckPartNode(graphene.ObjectType):
|
|
# sentence = graphene.String()
|
|
# offset = graphene.Int()
|
|
# length = graphene.Int()
|
|
# affected = graphene.String()
|
|
# corrected = graphene.String()
|
|
from spellcheck.client import TaskbaseClient
|
|
|
|
|
|
class SpellCheckStepNode(graphene.ObjectType):
|
|
# id = graphene.String()
|
|
# part = graphene.Field(SpellCheckPartNode)
|
|
sentence = graphene.String()
|
|
offset = graphene.Int()
|
|
length = graphene.Int()
|
|
affected = graphene.String()
|
|
corrected = graphene.String()
|
|
|
|
# def resolve_sentence(self, *args, **kwargs):
|
|
# print(args)
|
|
# print(kwargs)
|
|
# print(self)
|
|
# return self.part.sentence
|
|
|
|
# def resolve_offset(self):
|
|
# return self.part['offset']
|
|
#
|
|
# def resolve_length(self):
|
|
# return self.part['length']
|
|
#
|
|
# def resolve_affected(self):
|
|
# return self.part['affected']
|
|
#
|
|
# def resolve_corrected(self):
|
|
# return self.part['corrected']
|
|
|
|
|
|
class SpellCheck(relay.ClientIDMutation):
|
|
class Input:
|
|
text = graphene.String(required=True)
|
|
assignment = graphene.ID(required=True)
|
|
|
|
results = graphene.List(SpellCheckStepNode)
|
|
correct = graphene.Boolean()
|
|
|
|
@classmethod
|
|
def mutate_and_get_payload(cls, root, info, **kwargs):
|
|
user = info.context.user
|
|
text = kwargs.get('text')
|
|
assignment = kwargs.get('assignment')
|
|
|
|
client = TaskbaseClient(settings.TASKBASE_USER, settings.TASKBASE_PASSWORD, settings.TASKBASE_BASEURL)
|
|
|
|
data = json.loads(client.spellcheck('aOciP9H7tNu7pLsR4ohllk', text))
|
|
|
|
# data = {
|
|
# "correct": "WRONG",
|
|
# "solution": "",
|
|
# "steps": [
|
|
# {
|
|
# "id": 755,
|
|
# "part": {
|
|
# "sentence": "Das ist ein text mit Felern",
|
|
# "offset": 12,
|
|
# "length": 4,
|
|
# "affected": "text",
|
|
# "corrected": "Text",
|
|
# "mistakeType": "SPELLING",
|
|
# "mistakeSubtype": "CAPITALIZATION",
|
|
# "partType": "com.taskbase.lap.server.services.step.tokenizer.SpellCheckPart"
|
|
# },
|
|
# "partHash": "802479c1bf90c4ad0f998d550d34c18d502c948b3d72ae2d571808fb24823055",
|
|
# "count": 2
|
|
# },
|
|
# {
|
|
# "id": 759,
|
|
# "part": {
|
|
# "sentence": "Das ist ein text mit Felern",
|
|
# "offset": 21,
|
|
# "length": 6,
|
|
# "affected": "Felern",
|
|
# "corrected": "Feiern",
|
|
# "mistakeType": "SPELLING",
|
|
# "mistakeSubtype": "TYPO",
|
|
# "partType": "com.taskbase.lap.server.services.step.tokenizer.SpellCheckPart"
|
|
# },
|
|
# "partHash": "b6128347caec0cd0d81afd00f03e26bd71785ff12f35e892796d584adfbb18bf",
|
|
# "count": 2
|
|
# }
|
|
# ]
|
|
# }
|
|
|
|
return cls(correct=data['correct'] == 'CORRECT', results=map(lambda x: x['part'], data['steps']))
|
|
# user = info.context.user
|
|
# module_id = kwargs.get('module')
|
|
# bookmarked = kwargs.get('bookmarked')
|
|
#
|
|
# module = get_object(Module, module_id)
|
|
#
|
|
# if bookmarked:
|
|
# ModuleBookmark.objects.create(
|
|
# module=module,
|
|
# user=user
|
|
# )
|
|
# else:
|
|
# ModuleBookmark.objects.get(
|
|
# module=module,
|
|
# user=user
|
|
# ).delete()
|
|
#
|
|
# return cls(success=True)
|
|
|
|
|
|
class SpellCheckMutations:
|
|
spell_check = SpellCheck.Field()
|