Example #1
0
def correctSentence(req):
    response = {}
    userId = int(req.GET.get('userId'))
    orgsentences = req.GET.get('orgsentences')
    sentences = orgsentences.split('\n')
    # sentences.append(orgsentences)
    print('++++')
    _, wordList = WordManager.getWordByUserID(userId)
    wordList = [word.word for word in wordList]
    print(wordList, '-----')
    print(sentences, '-----')
    # 改错
    correctList, notes, dics, corrcnt = predict.predict_for_sentence(
        sentences, wordList)
    corrects = "\n".join(correctList)
    types = [str(k) for k in dics.keys() if len(dics[k]) > 0]
    # 添加改错记录, 加上对应标签
    if userId == 0:
        response['error_counts'] = corrcnt
        response['msg'] = 'succeed'
        response['err_num'] = 0
        response['correctSentenceList'] = correctList
        response['correctDetail'] = dics
        print(orgsentences)
        print(corrects)
        print(response,
              "=====correct sentences here but user not login=========")
        return JsonResponse(response)
    err, _, infodic = SenManager.addSentences(userId, orgsentences, corrects,
                                              types)
    if err == 'succeed':
        for t in types:
            # 更新错误类型数
            if infodic[t] == 'old':
                err = TypeManager.updateType(t, userId, len(dics[t]))
            if err != 'succeed':
                response['msg'] = 'error'
                response['err_num'] = 1
                return JsonResponse(response)
        for word in wordList:
            for l in sentences:
                if word in l:
                    print('ok')
                    err = WordManager.updateWord(word, userId, 1)
        # response['notes'] = json.dumps(notes)
        response['error_counts'] = corrcnt
        response['msg'] = 'succeed'
        response['err_num'] = 0
        response['correctSentenceList'] = correctList
        response['correctDetail'] = dics
        print(response, "=====correct sentences here=========")
        return JsonResponse(response)
    else:
        response['msg'] = 'error'
        response['err_num'] = 1
        return JsonResponse(response)
Example #2
0
def delWord(req):
    response = {}
    curword = req.GET.get('wordName')
    userID = req.GET.get('userID')
    err, wordList = WordManager.getWordByUserID(userID)
    wordId = [word.id for word in wordList if word.word == curword][0]
    info, _ = WordManager.delWord(wordId)
    if info == 'succeed':
        response['msg'] = 'succeed'
        response['err_num'] = 0
    else:
        response['msg'] = 'error'
        response['err_num'] = 1
    print(response, "=====delete word here")
    return JsonResponse(response)
Example #3
0
def getUserVocab(req):
    response = {}
    userId = req.GET.get('userId')
    info, vocab = WordManager.getWordByUserID(userId)
    if info == 'succeed':
        response['msg'] = 'succeed'
        response['err_num'] = 0
        response['list'] = json.loads(serializers.serialize("json", vocab))
    else:
        response['msg'] = 'error'
        response['err_num'] = 1
    print(response, "=====get all words here")
    return JsonResponse(response)
Example #4
0
def getMostUserWords(req):
    response = {}
    userId = req.GET.get('userId')
    info, wordList = WordManager.getMostUsedWord(userId)
    if info == 'succeed':
        response['msg'] = 'succeed'
        response['err_num'] = 0
        response['list'] = json.loads(serializers.serialize("json", wordList))
    else:
        response['msg'] = 'error'
        response['err_num'] = 1
    print(response, "=====get words rank here")
    return JsonResponse(response)
Example #5
0
def searchWord(req):
    response = {}
    userId = req.POST.get('userId')
    word = req.POST.get('word')
    info, words = WordManager.searchWord(userId, word)
    if info == 'succeed':
        response['msg'] = 'succeed'
        response['err_num'] = 0
        response['list'] = json.loads(serializers.serialize("json", words))
    else:
        response['msg'] = 'error'
        response['err_num'] = 1
    print(response, "=====search words here")
    return JsonResponse(response)
Example #6
0
def addWord(req):
    response = {}
    userId = req.GET.get('userId')
    word = req.GET.get('word')
    word = word.split('->')[0].strip()
    err, _ = WordManager.addWords(userId, word)
    if err == 'succeed':
        response['msg'] = 'succeed'
        response['err_num'] = 0
    else:
        response['msg'] = 'error'
        response['err_num'] = 1
    print(response, "=====add words here")
    return JsonResponse(response)
Example #7
0
    def tests(self):
        err, user1 = UserManager.createUser("tttt", "t1")

        err, word = WordManager.addWords(self.user.id, 'fottball')
        self.assertEqual(err, 'succeed')
        err, word1 = WordManager.addWords(self.user.id, 'facebook')
        err = WordManager.updateWord(word1.word, self.user.id, 5)
        self.assertEqual(err, 'succeed')

        err, words = WordManager.searchWord(self.user.id, "f")
        self.assertEqual(err, 'succeed')
        for w in words:
            print('aaa', w.word, w.use_counts)

        err, words = WordManager.getWordByUserID(self.user.id)
        for w in words:
            print(w.word, w.use_counts)
        self.assertEqual(err, 'succeed')
        print("========")
        err, word = WordManager.delWord(word1.id)
        self.assertEqual(err, 'succeed')


        err, word = WordManager.addWords(user1.id, 'iphone')
        err, word = WordManager.addWords(user1.id, 'huawei')
        err = WordManager.updateWord(word.word, user1.id, 10)

        err, words = WordManager.getMostUsedWord(user1.id)
        self.assertEqual(err, 'succeed')
        for word in words:
            print(word.word, word.use_counts)

        err, word22 = WordManager.delWord(word1.id)
        self.assertEqual(err, 'succeed')
        print(word22.is_delete)
        err, w = WordManager.searchWord(self.user.id, word22.word)
        self.assertNotEqual(err, 'succeed')
        err, w = WordManager.addWords(self.user.id, word22.word)
        self.assertEqual(w.id, word22.id)
        print(w.is_delete, w.id, word22.id, word22.is_delete)
        err, words = WordManager.getWordByUserID(user1.id)
        self.assertEqual(err, 'succeed')
        for w in words:
            print(w.word, w.use_counts)