Beispiel #1
0
def spellCheck(text):
    #choose the dictionary
    d = enchant.Dict("en_US")

    #Store number of misspelt words
    numIncorrect = 0

    sentences = paragraph.paragraph_to_sentences(text)

    #Store misspelt words in dictionary
    misspelt = []

    #split text into words
    wordList = re.findall(r"\w+", text)

    #checking misspelt words
    for index, word in enumerate(wordList):
        if d.check(word) == False:
            current_length = 0
            sentence = ""
            j = 0
            while current_length < index:
                current_length += paragraph.getWordCount(sentences[j])
                j += 1
            misspelt.append({
                "index": index,
                "word": word,
                "sentence": sentences[j - 1],
                "sentence_index": j - 1,
                "suggested_word": suggest_word(d, word)
            })
            numIncorrect += 1

    return numIncorrect, misspelt
async def get_essay_comment(
    order_id: int,
    current_account: schemas.Account = Depends(get_current_account),
    db: Session = Depends(get_db)):
    db_order = db.query(
        models.Order).filter(models.Order.order_id == order_id).first()
    #If the order not in Database, return error
    if not db_order:
        raise HTTPException(status_code=404, detail="Order not found")

    #check if the order is paid
    if db_order.status_id in [0, 1]:
        raise HTTPException(status_code=405, detail="Order has been paid yet!")

    #Check permission
    if current_account.role_id != 0 and current_account.user_id not in [
            db_order.student_id, db_order.teacher_id
    ]:
        raise HTTPException(status_code=403)

    db_essay = db_order.essay

    db_essay_comment_list = db.query(models.EssayComment).filter(
        models.EssayComment.essay_id == db_essay.essay_id).order_by(
            models.EssayComment.sentence_index).all()
    essay = db_essay.content.replace("\n", " ")
    sentences = paragraph.paragraph_to_sentences(essay)
    print(sentences)

    if len(db_essay_comment_list) == 0:
        for index, sentence in enumerate(sentences):
            db_essay_comment = models.EssayComment(
                essay_id=db_essay.essay_id,
                sentence_index=index,
            )
            db.add(db_essay_comment)
            db.commit()
            db.refresh(db_essay_comment)

    db_essay_comment_list = db.query(models.EssayComment).filter(
        models.EssayComment.essay_id == db_essay.essay_id).order_by(
            models.EssayComment.sentence_index).all()

    essay_comments = []
    for index, db_essay_comment in enumerate(db_essay_comment_list):
        essay_comments.append(
            schemas.EssayComment(
                sentence_index=db_essay_comment.sentence_index,
                sentence=sentences[index],
                comment=db_essay_comment.comment))

    essay_comment_response = schemas.EssayCommentResponse(
        essay_id=db_essay.essay_id,
        title=db_essay.title,
        content=db_essay.content,
        type_id=db_essay.type_id,
        essay_comments=essay_comments)

    return essay_comment_response
Beispiel #3
0
def spellCheckAdvance(text):
    #Store number of misspelt words
    numIncorrect = 0

    sentences = paragraph.paragraph_to_sentences(text)

    #Store misspelt words in dictionary
    misspelt = []

    current_length = 0

    for sentence_index, sentence in enumerate(sentences):
        matches = tool.check(sentence)
        for rules in matches:
            if len(rules.replacements) > 0:
                numIncorrect += 1
                mistake = sentence[rules.offset:rules.errorLength +
                                   rules.offset]
                correction = rules.replacements[0]
                misspelt.append({
                    "index": rules.offset + current_length,
                    "word": mistake,
                    "sentence": sentence,
                    "sentence_index": sentence_index,
                    "suggested_word": correction
                })
        current_length += len(sentence)
    """
        my_mistakes = []
        my_corrections = []
        start_positions = []
        end_positions = []
    
    for rules in matches:
        if len(rules.replacements)>0:
            start_positions.append(rules.offset)
            end_positions.append(rules.errorLength+rules.offset)
            my_mistakes.append(text[rules.offset:rules.errorLength+rules.offset])
            my_corrections.append(rules.replacements[0])
        
        
    my_new_text = list(text)
    
    
    for m in range(len(start_positions)):
        for i in range(len(text)):
            my_new_text[start_positions[m]] = my_corrections[m]
            if (i>start_positions[m] and i<end_positions[m]):
                my_new_text[i]=""
    """
    return numIncorrect, misspelt