Add spell check module to backend

This commit is contained in:
Ramon Wenger 2019-12-16 14:24:50 +01:00 committed by Ramon Wenger
parent 17e061892a
commit bdda817533
5 changed files with 176 additions and 4 deletions

View File

@ -16,6 +16,7 @@ from objectives.mutations import ObjectiveMutations
from objectives.schema import ObjectivesQuery
from portfolio.mutations import PortfolioMutations
from portfolio.schema import PortfolioQuery
from spellcheck.mutations import SpellCheckMutations
from surveys.schema import SurveysQuery
from surveys.mutations import SurveyMutations
from rooms.mutations import RoomMutations
@ -26,8 +27,7 @@ from registration.mutations_public import RegistrationMutations
class Query(UsersQuery, AllUsersQuery, ModuleRoomsQuery, RoomsQuery, ObjectivesQuery, BookQuery, AssignmentsQuery,
StudentSubmissionQuery, BasicKnowledgeQuery, PortfolioQuery, SurveysQuery,
graphene.ObjectType):
StudentSubmissionQuery, BasicKnowledgeQuery, PortfolioQuery, SurveysQuery, graphene.ObjectType):
node = relay.Node.Field()
if settings.DEBUG:
@ -35,8 +35,8 @@ class Query(UsersQuery, AllUsersQuery, ModuleRoomsQuery, RoomsQuery, ObjectivesQ
class Mutation(BookMutations, RoomMutations, AssignmentMutations, ObjectiveMutations, CoreMutations, PortfolioMutations,
ProfileMutations, SurveyMutations, NoteMutations, RegistrationMutations, graphene.ObjectType):
ProfileMutations, SurveyMutations, NoteMutations, RegistrationMutations, SpellCheckMutations,
graphene.ObjectType):
if settings.DEBUG:
debug = graphene.Field(DjangoDebug, name='__debug')

View File

View File

@ -0,0 +1,51 @@
import re
import requests
# from spellcheck.client import TaskbaseClient
# client = TaskbaseClient('info@iterativ.ch', 'myverysafepassword1234', 'https://dev-iterativ.taskbase.com')
# client.spellcheck('aOciP9H7tNu7pLsR4ohllk', 'Dies ist ein Sats mit filen Felern')
class TaskbaseClient:
# def __init__(self, resource_url):
# # resource url should be in the form https://username:password@baseurl
# pattern = re.compile(r'(\w+)://(\w+):(\w+)@([a-zA-Z0-9.]+)')
# scheme, username, password, url = pattern.match(resource_url).groups()
# self.username = username
# self.password = password
# self.token = None
# self.base_url = '{}://{}'.format(scheme, url)
def __init__(self, username, password, base_url):
self.username = username
self.password = password
self.base_url = base_url
self.token = None
def login(self):
payload = {
'username': self.username,
'password': self.password
}
response = requests.post('{}/api/login'.format(self.base_url), json=payload)
data = response.json()
self.token = data['accessToken']
def spellcheck(self, task, text):
if self.token is None:
self.login()
payload = {
"taskId": task,
"input": {
"text": text,
"type": "SPELL_CHECK"
}
}
headers = {'Authorization': 'Bearer {}'.format(self.token), 'Content-Type': 'application/json'}
response = requests.post('{}/api/grade'.format(self.base_url), json=payload, headers=headers)
if response.status_code == 200:
return response.content
else:
raise Exception('Something went wrong')

View File

@ -0,0 +1,121 @@
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()

View File