Besides teachers and parents, it can also mark homework

Recently, a story about a parent dropping out of a social group became a hot search on a blog. In the story, the contradictions between teachers and parents are concentrated on correcting homework. As for which is right and which is wrong, it is left to the masses to evaluate. As a technical worker, I suddenly wondered whether I could let the machine assist teachers in correcting homework in the future. Sounds like an idea for world peace!

After an adjustable (search) research (cable), on the English composition correcting, is there really some mature scheme can be used, and the learning cost is low, such as youdao ZhiYun English composition correcting service, only need to read the document according to the rules of development and application, can get the detailed grade as a result, the composition can be pictures, also can be words, Grades can range from primary school to IELTS and TOEFL.

With a lot of excitement, I quickly developed a simple demo, which I shared below.

Preparations for calling the API

First of all, you need to create an instance, create an application, bind the application and instance on the personal page of Youdao Wisdom Cloud, and obtain the ID and key of the application. For details about the process of individual registration and application creation, see the development process of a batch file translation shared in the article

It should be noted here that the essay grading is in the form of image and text, which call different apis, so you need to create two instances.

Detailed introduction of the development process

The following describes the specific code development process.

English composition correction is divided into two apis, corresponding to two forms of composition, image recognition and text input respectively. In both cases, the signature generated by the content to be corrected and the timestamp is posted to the API interface, which then returns the correction result.

Parameters required for image recognition API input are as follows:

The field name type meaning mandatory note
q text Image base64. The size does not exceed 5MB True Pictures of base64
langType text Currently, only English is supported False en
appKey text Application ID (Application ID) True Can be inApplication managementTo view
salt text Random string True hfa12lak56ja9gjl
sign text Signature information: SHA256 (appKey+ Input +salt+ key) True xxxxx
grade text Composition grade. Grade supports lists. By default, only sentences are evaluated regardless of grade false default
title text The thesis title false 0
modelContent text User provided sample content false 0
goodExpression text Good user-provided expression false 0
needTypo text Whether typo is required, (true/false), the default is true false true
signType text Signature type true v3
limitedWords text Essay word limit false 1000

Text input API input parameters are as follows:

The field name type meaning mandatory note
q text The corrected text. The value contains a maximum of 5000 characters True text
langType text Currently, only English is supported False en
appKey text Application ID (Application ID) True Can be inApplication managementTo view
salt text Random string True hfa12lak56ja9gjl
sign text Signature information: SHA256 (appKey+ Input +salt+ key) True xxxxx
grade text Composition grade. Grade supports lists. By default, only sentences are evaluated regardless of grade false default
title text The thesis title false 0
modelContent text User provided sample content false 0
goodExpression text Good user-provided expression false 0
needTypo text Error correction, default true (true/false) false true
signType text Signature type true v3
limitedWords text Essay word limit false 1000
curtime text Current UTC timestamp in seconds true The time stamp

It is better to transmit limitedWords, so that scoring is more accurate. The signature generation algorithm is as follows:

  • SignType =v3, sha256(application ID+input+salt+curtime+ key), recommended

Sha256 signature calculation method: SHA256 (application ID+ Input +salt+ current UTC timestamp + key).

Input is calculated as follows: Input = the first 10 characters of multiple Q’s + multiple Q’s + The last 10 characters of multiple Q’s (when the length of multiple Q’s is greater than 20) or input= the string of multiple Q’s (when the length of multiple Q’s is less than or equal to 20).

In interface input parameters, grade is of the following types:

level code
Evaluate the sentence regardless of grade default
Primary school elementary
Junior high school junior
High school high
Level 4 cet4
Level 6 cet6
take part in the postgraduate entrance exams graduate
The toefl toefl
GRE gre
The ielts ielts

The Demo development:

This demo is developed using Python3, including maindow. Py, correctclass.py, homeworkcorrect. py three files, respectively for the demo interface, interface logic processing and English composition correction interface call method encapsulation.

  1. Interface part:

    The UI part is relatively simple, with the main functions of selecting composition files to be corrected, selecting the storage path of correction results, and selecting the type of correction. The layout code is as follows:

    root=tk.Tk()
    root.title(" youdao correct writing test")
    frm = tk.Frame(root)
    frm.grid(padx='50', pady='50')
    
    # Article selection
    btn_get_file = tk.Button(frm, text='Select the assignment to be marked (picture or text)', command=get_files)
    btn_get_file.grid(row=0, column=0, ipadx='3', ipady='3', padx='10', pady='20')
    text1 = tk.Text(frm, width='40', height='10')
    text1.grid(row=0, column=1)
    
    # Result path selection
    btn_get_result_path=tk.Button(frm,text='Select the path to store the correction results',command=set_result_path)
    btn_get_result_path.grid(row=1,column=0)
    text2=tk.Text(frm,width='40', height='2')
    text2.grid(row=1,column=1)
    
    # Level selection
    label=tk.Label(frm,text='Select grade:')
    label.grid(row=3,column=0)
    combox=ttk.Combobox(frm,textvariable=tk.StringVar(),width=38)
    combox["value"]=select_type_dict
    combox.current(0)
    combox.bind("<<ComboboxSelected>>",get_grade_type)
    combox.grid(row=3,column=1)
    
    Start correcting
    btn_sure=tk.Button(frm,text="Grade",command=correct_files)
    btn_sure.grid(row=4,column=1)
    
    root.mainloop()
    Copy the code

    Start bTN_sure binding event correct_files() to initiate the correction and open the result storage path after completion:

    def correct_files() :
        correct.start_correct()
        os.system('start '+correct.result_path)
    Copy the code
  2. correctclass.py

    Here mainly with the LOGIC of UI, analysis of file type, select the appropriate interface to correct the composition.

    First define a class Correct:

    class Correct() :
        def __init__(self,file_paths,grade,result_path) :	
            self.file_paths=file_paths		Path of file to be corrected
            self.grade =grade				# Grade
            self.result_path=result_path	# result path
    Copy the code

    The get_correct_result() method determines which wrapper method to call based on the file type, processes the return value, and stores the correction result to the file system.

        def get_correct_result(self,file_path) :
            file_type=file_path.split(".") [1]
            if file_type=="txt":
                print(file_path)
                result=connect_context(file_path,self.grade)
                self.save_result(file_path,result)
            elif file_type=="png" or file_type=="jpg" or file_type=="jepg" :
                result=connect_pic(file_path,self.grade)
                self.save_result(file_path,result)
    Copy the code

    The save_result() method saves results:

        def save_result(self,file_path,result) :
            result_file_name=os.path.basename(file_path).split('. ') [0] +'_result.txt'
            f=open(self.result_path+'/'+result_file_name,'w')
            f.write(str(result))
            f.close()
    Copy the code
  3. HomeworkCorrect.py

    Homeworkcorrect. py encapsulates the method of requesting two homework correcting apis, the main difference between which is the URL and the APP sample. The core methods are connect_pic() and connect_context()

    Connect_pic () :

    def connect_pic(pic_path,grade) :
        f = open(pic_path, 'rb')  Open the image file in binary mode
        q = base64.b64encode(f.read())  Read the content of the file and convert it to Base64 encoding
        f.close()
        data = {}
    
        curtime = str(int(time.time()))
        data['curtime'] = curtime
        salt = str(uuid.uuid1())
        #print(q)
        signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
        sign = encrypt(signStr)
        data['appKey'] = APP_KEY
        data['salt'] = salt
        data['q'] = q
        data['sign'] = sign
        data['grade'] = grade
        data['signType'] = 'v3'
    
        response = do_request(data,YOUDAO_URL_IMAGE)
        result=json.loads(str(response.content,'utf-8'))'Result']
        return result
    Copy the code

    connect_context():

    def connect_context(file_path,grade) :
        f=open(file_path,'rb')
        q=f.read()
        f.close()
        data = {}
        curtime = str(int(time.time()))
        data['curtime'] = curtime
        salt = str(uuid.uuid1())
        signStr = APP_KEY + truncate(q) + salt + curtime + APP_SECRET
        sign = encrypt(signStr)
        data['appKey'] = APP_KEY
        data['q'] = q
        data['salt'] = salt
        data['sign'] = sign
        data['signType'] = "v3"
        data['grade'] = grade
        response = do_request(data,YOUDAO_URL_TEXT)
        print(response.content)
        result = json.loads(str(response.content, 'utf-8'))'Result']
        print(result)
        return result
    Copy the code

Results show

I selected an English picture and TXT document respectively for testing:

Response result description:

{
"errorCode": "Error code"."Result": {
    "uniqueKey": "Unique string identification for each request"."essayLangName": "Linguistic information"."rawEssay": "Request text"."refEssay": "For reference"."stLevel": "Composition Level"."reqSource": "Request source"."extraParams":"Additional request parameters (extension parameters)"."wordNum": "Total number of words in the article"
    "conjWordNum": "Number of articles linking words"."AllFeatureAdvice": { # Suggestions for the characteristics of the composition
        "WordNum": "Suggested word count, for example, if the number of words in the essay is suspected to exceed the required word count for the test."."Spelling": "Suggestions for spelling mistakes"."WordDiversity": "Vocabulary richness suggestion: for example, the vocabulary accumulation is very small, and only some scattered simple words can be given. It is suggested to accumulate more vocabulary."."Structure": "Essay Structure Suggestions"."AdvanceVocab": [
            "xx"."xx"."xx" # Advanced vocabulary used in the text]."AdvanceVocabLeval": [0.1.2].# Corresponds to the level of advanced vocabulary
        "lexicalSubs": [ # word replacement (Note: The words in the candidates may be empty to indicate that there are no synonyms recommended for replacement, but word has been used more than three times)
            {"candidates": ["xx"."xx"]."count": frequency,"word": xx counterpart},... ]"Conjunction": [{
            "used": ["xx"."xx"."xx"] # have used the conjunction
            "advice": ["xx"."xx"."xx"] # recommended word}]."Topic": "Topic Relevance Suggestions"."Grammar": "Grammar advice"."GoodExpression": ["xx"."xx"."xx"] # Good expression
    },
    "AllFeatureScore": { # Corresponding to AllFeatureAdvice score, there is no Advice except NeuralScore. It represents the score of neural network essay, not the final score!
        "NeuralScore": 68.64.# range: [0,100]
        "WordNum": 10.# range: [0, 10] --
        "Spelling": 10.# range: [0, 10]
        "WordDiversity": 0.# range: [0, 10]
        "Structure": 8.# range: [0, 10]
        "AdvanceVocab": 7.61.# range: [0, 10]
        "Conjunction": 6.94.# range: [0, 10]
        "Topic": 6.03.# range: [0, 10]
        "Grammar": 2.5  # range: [0, 10]
        "SentComplex": 10 # range: [0, 10]
    },
    "majorScore": { # is the result of score integration in AllFeatureScore
        "WordScore": 10.Vocabulary score: including the number of words, richness, advanced vocabulary, etc
        "GrammarScore": 10.# Grammar score: includes spelling, grammar, sentence complexity scores, etc
        "StructureScore": 10.# Logic score: Includes paragraph and connective scores
        "topicScore": 10.# Content (topic relevance) score, if no reference is made to the essay, the score will be considered for grammar and complexity
    },
    "essayFeedback": {"sentsFeedback": [{"sentId": "Sentences are numbered in the full text, starting from 0."."paraId": "Paragraph number of the sentence, starting with 0."."rawSent": "The original"."segSent": "The result after the participle of the original sentence"."correctedSent": "The result of the revision of the original sentence."."sentStartPos": "The offset of the sentence in the full text relative to the initial position of the text."."errorPosInfos": [{"type": "Error type (including 'grammar', 'typo', 'refactor')"."startPos": "The offset of the error start position relative to the rawSent start position"."endPos": "The offset of the end of the error relative to the start of rawSent"."orgChunk": "The contents of the error block"."correctChunk": "The specific content after the error block is corrected"."error_type": "Specific categories of errors (0 for spelling, 1 for article, 2 for verb tense or third person singular or plural, 3 for noun singular or plural, 4 for case, 5 for preposition, 6 for other grammatical errors, 7 for text formatting, 8 for correctness)"."new_error_type": "Error categories (0 means absolutely right, 1 said writing format is not standard, 2 said spelling errors, 3 means punctuation, 4 said error, article 5 said verb errors, 6 said animals error, 7 said pronouns error, 8 said preposition errors, 9 says adjective errors, 10 said adverb error, 11 said conjunction errors, 20 means other errors, 21 means all syntax errors (compatible)
                        "new_sub_error_type": "Segmentation error categories (0 said right, 1 said unknown error, said two words missing, said three word redundancy, four function words misuse said, 5 said prepositions misuse, 6 said verb grammatical errors, 7 said verb tense errors, 8 said after the modal verb can base verbs errors, 9 said the passive error, 10 said the verb infinitive errors, 11 said verb errors, 12 says adjective comparative errors, 13 said superlative adjective errors, 14 said adverbs comparative errors, 15 said adverb error, highest 16 said animals error, 17 said noun errors, 18, said guest personal pronoun confusion, 19, said confused personal pronouns and possessive pronouns, Says adjective and noun pronoun confusion, 20, 21, said the personal pronoun and reflexive pronoun confusion, confused question 22 / relations/connections pronoun, demonstrative pronoun confusion, 23 said 24 said indefinite pronoun confusion, 25, said pronouns error, 26, said the misuse of punctuation, 27, said the spelling errors, 28, said is not standard error)"
                            "Give an example.": if new_error_type =5, new_sub_error_type=2", indicating the absence of a verb"reason": "Specific cause of the error."."isValidLangChunk": "Similar to isValidSent below, check whether the segment is valid (if the language test results do not match the expectation, the segment is considered illegal)"
                        "analysis": "Specific discrimination of the cause of the error (reserved interface, should not be used for the time being)"},... , {},"isValidLangSent": "Whether the sentence is legal or not (legal or not depends on whether the recognition result of language information of the sentence is consistent with the expected result)"
                "sentFeedback": "Error cause feedback, based on concatenation of all Reason fields in errorPosInfos"."isContainTypoError": "Does return contain typo errors?"."isContainGrammarError": "Return any syntax errors"."sentScore": "Sentence score (no use yet, coming soon)"}}]"totalScore": "Essay final score"
    "fullScore": "Full marks for corresponding level"
    "essayAdvice": "Final evaluation of the article"
    "paraNum": "Number of Article Paragraphs"
    "sentNum": "Number of sentences in the article"}}Copy the code

conclusion

The English composition correcting API of Youdao Wisdom Cloud has clear documents and comprehensive functions. It can carry out multi-dimensional correction for compositions of different types of documents and different difficulties. The evaluation indicators are clear, and the correction results are very valuable for reference.

I believe that in the future, there will be more types of homework correcting services, by then, teachers and parents can be liberated…

Project address: github.com/LemonQH/Cor…