Invincible, with Python for English teachers to develop an English composition correction artifact (support primary school to IELTS)

Hand tearing code 800 Li 2021-04-08 13:44:52
invincible python english teachers develop


Except for teachers and parents , It can also grade homework

Recently, a story about parents quitting group has been searched in a blog . In the story, the contradiction between the teacher and the parents breaks out from the grading homework , As for which is right or wrong , It's better to leave it to the gourd eaters for evaluation , As a technician , I had a whim , Whether the machine can be used to assist teachers in correcting homework in the future ? It seems like an idea to maintain world peace !

 Insert picture description here

After a while ( search ) study ( Cable ), On the correction of English composition , There are really some mature solutions to use , And the cost of learning is quite low , For example, there is Dao Zhiyun's Correcting English composition service , Just read file Develop applications according to the rules , You can get detailed correction results , A composition can be a picture , It can also be words , Grades can range from primary school to IELTS TOEFL , It covers a wide area .

With excitement , I quickly developed a simple demo, Let's share the development process .

call API Interface preparation

First , It is necessary to create instances on Youdao Zhiyun's personal page 、 Create an 、 Binding applications and instances , Get the application's id And the key . Specific personal registration process and application creation process are detailed in the article Share a batch file translation development process

Here's a special note , There are two forms of composition correction: picture and text , Different api, So you need to create two instances .

 Insert picture description here

The development process is introduced in detail

The following describes the specific code development process .

There are two ways to correct English composition API, They correspond to each other Image recognition and Text input Two forms of composition . The method of calling is similar , They are signatures that generate information such as the content to be corrected and the time stamp post To API Interface , Then the interface returns the correction result .

Image recognition API Input the required parameters in the table below :

Field name type meaning Required remarks
q text The image base64. Not larger than 5MB True The image base64
langType text Language , At present, only English is supported False en
appKey text Application identification ( application ID) True Can be found in Application management see
salt text Random string True hfa12lak56ja9gjl
sign text Signature information :sha256(appKey+input+salt+ secret key ) True xxxxx
grade text Composition grade . The list of support is shown below grade Support List , Default regardless of level , Only judge the sentence false default
title text The title of the composition false 0
modelContent text User provided model text content false 0
goodExpression text Good expression provided by users false 0
needTypo text If you need typo,(true/false), The default is true false true
signType text Signature type true v3
limitedWords text Limit the number of words in a composition false 1000

Text input API The input parameters are shown in the table below :

Field name type meaning Required remarks
q text Corrected text . The text is no more than 5000 character True text
langType text Language , At present, only English is supported False en
appKey text Application identification ( application ID) True Can be found in Application management see
salt text Random string True hfa12lak56ja9gjl
sign text Signature information :sha256(appKey+input+salt+ secret key ) True xxxxx
grade text Composition grade . The list of support is shown below grade Support List , Default regardless of level , Only judge the sentence false default
title text The title of the composition false 0
modelContent text User provided model text content false 0
goodExpression text Good expression provided by users false 0
needTypo text If you need error correction , The default is true(true/false) false true
signType text Signature type true v3
limitedWords text Limit the number of words in a composition false 1000
curtime text At present UTC Time stamp ( second ) true Time stamp

It's better to transmit limitedWords, This is more accurate .
The signature generation algorithm is as follows :

  • signType=v3,sha256( application ID+input+salt+curtime+ secret key ), Recommended

sha256 The signature calculation method is :sha256( application ID+input+salt+ At present UTC Time stamp + secret key ).

among ,input The calculation method of is :input= Multiple q After splicing, before 10 Characters + Multiple q Splicing length + Multiple q The last ten characters ( When more than one q The length after splicing is greater than 20) or input= Multiple q Concatenated string ( When more than one q The length after splicing is less than or equal to 20).

In the interface input parameters ,grade For the following categories :

Level Code
Regardless of level , Just evaluate the sentence default
Primary school elementary
Junior high school junior
high school high
Level Four cet4
Level six cet6
One's deceased father grind graduate
TOEFL toefl
GRE gre
IELTS ielts

Demo Development :

This demo Use python3 Development , Include maindow.py,correctclass.py,HomeworkCorrect.py Three files , Respectively demo The interface of 、 Interface logic processing and encapsulation of interface call method .

  1. Interface part :

    UI Part of it is simpler , The main function is to select the composition file to be corrected 、 Select the storage path of the correction results 、 Choose 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=' Choose the homework to be corrected ( 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=' Choose a 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 marking 
    btn_sure=tk.Button(frm,text=" To correct ",command=correct_files)
    btn_sure.grid(row=4,column=1)
    root.mainloop()
    

    And the start button btn_sure Binding events for correct_files() To start marking , And open the result storage path after completion :

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

    The main cooperation here is UI The logic of , Analyze file types , Choose the right 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 # File path to be corrected 
    self.grade =grade # Rating level 
    self.result_path=result_path # The result path 
    

    get_correct_result() Method determines which encapsulation method should be called according to the file type , And handle the return value , Save the correction results into 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)
    

    save_result() Method realizes the function of saving the result :

     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()
    
  3. HomeworkCorrect.py

    HomeworkCorrect.py It encapsulates the request for two kinds of homework correction API Methods , Two API The main difference is that URL and APP Different examples . The core methods are connect_pic() and connect_context()

    connect_pic():

    def connect_pic(pic_path,grade):
    f = open(pic_path, 'rb') # Open the graph file in binary mode 
    q = base64.b64encode(f.read()) # Read file contents , Convert to base64 code 
    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
    

    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
    

Effect display

I chose a picture in English and txt Document to test :

 Insert picture description here

The response results show that :

{

"errorCode": " Error code ",
"Result": {

"uniqueKey": " Unique string ID for each request ",
"essayLangName": " Language information ",
"rawEssay": " Ask for the original text ",
"refEssay": " Reference model ",
"stLevel": " Composition level ",
"reqSource": " Source of the request ",
"extraParams"" Additional request parameters ( Extension parameters )",
"wordNum": " The total number of words in the article "
"conjWordNum": " The number of conjunctions in the article ",
"AllFeatureAdvice": {
 # Suggestions on the characteristics of composition 
"WordNum": " Number of words suggested , If the number of words in the article is suspected to exceed the number of words required for the examination ",
"Spelling": " Spelling errors suggest ",
"WordDiversity": " Vocabulary richness suggestions , For example, the accumulation of vocabulary is very small , Can only give some scattered simple words , It is suggested to accumulate more words ",
"Structure": " Structure suggestions ",
"AdvanceVocab": [
"xx", "xx", "xx" # The advanced words used in the article 
],
"AdvanceVocabLeval": [0, 1, 2], # Corresponding to the level of advanced vocabulary 
"lexicalSubs": [ # Word substitution ( Be careful :candidates Chinese words may be empty , It means that there is no recommended replacement , but word Used more frequently than 3 Time )
{
"candidates": ["xx", "xx"], "count": frequency , "word": xx Corresponding words }, ...
]
"Conjunction": [{

"used": ["xx", "xx", "xx"] # The conjunction has been used 
"advice": ["xx", "xx", "xx"] # Recommendations 
}],
"Topic": " Topic relevance suggestions ",
"Grammar": " Suggestions on grammar ",
"GoodExpression": ["xx", "xx", "xx"] # Good expression 
},
"AllFeatureScore": {
 # Corresponding to the above AllFeatureAdvice Each feature score , except NeuralScore It's not Advice Of , It represents the result of neural network composition scoring , It's not the final score !
"NeuralScore": 68.64, # Range :[0,100]
"WordNum": 10, # Range :[0, 10] ---> It refers to the word score 
"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": {
 # yes AllFeatureScore in score Integration results 
"WordScore": 10, # Vocabulary score : Including the number of words 、 Abundance 、 Advanced vocabulary, etc 
"GrammarScore": 10, # Grammar score : Including spelling 、 grammar 、 Sentence complexity score, etc 
"StructureScore": 10, # Logical score : Include paragraph and conjunction scores 
"topicScore": 10, # Content ( Topic relevance ) score , If there is no reference to the model text , This part has to consider grammar and complexity 
},
"essayFeedback":{

"sentsFeedback": [
{

"sentId": " Sentence number in full text , from 0 Start ",
"paraId": " The paragraph number of the sentence is , from 0 Start ",
"rawSent": " The original sentence ",
"segSent": " The result of word segmentation in the original sentence ",
"correctedSent": " The result of the correction of the original sentence ",
"sentStartPos": " The offset of the sentence from the original position of the article in the full text ",
"errorPosInfos": [
{

"type": " Wrong type ( Include `grammar`,`typo`,`refactor`)",
"startPos": " The starting position of the error is relative to rawSent The offset of the starting position ",
"endPos": " The end of the error is relative to rawSent The offset of the starting position ",
"orgChunk": " The specific content of the error block ",
"correctChunk": " The specific content after the error block is corrected ",
"error_type": "( Abandoning ) Specific categories of errors (0 A spelling mistake ,1 It means that the article is wrong ,2 The third person verb or the third person verb is wrong ,3 It means that a noun is singular or plural ,4 The case is wrong ,5 A preposition error ,6 Other grammatical errors ,7 Indicates that the text format is wrong ,8 Said right )",
"new_error_type": " Error category (0 It means absolutely right ,
1 It means that the writing format is not standardized ,2 A spelling mistake ,
3 Punctuation error ,4 It means that the article is wrong ,5 A verb is wrong ,
6 It means that a noun is singular or plural ,7 A pronoun error ,8 A preposition error ,
9 It means that an adjective is wrong ,10 It means that the adverb is wrong ,11 It means that the conjunction is wrong ,
20 Indicates other errors ,21 All grammatical errors ( compatible ))"
"new_sub_error_type": " Subdivide error categories (0 Said right ,1 Indicates an unknown error ,2 A word is missing ,3 Words are redundant ,
4 It means that the article is misused ,5 A preposition is misused ,6 It means that the subject predicate agreement of a verb is wrong ,7 The verb tense is wrong ,8 The original form of a modal verb is wrong ,
9 It means that the passive voice is wrong ,10 The infinitive is wrong ,11 A verb is wrong ,12 A comparative error in an adjective ,
13 It's a mistake to express the superlative of an adjective ,14 A comparative error of adverb ,15 It means that the highest level of adverb is wrong ,16 It means that a noun is singular or plural ,
17 A noun is wrong ,18 The subject object case of a personal pronoun is confused ,19 A person pronoun is confused with a possessive pronoun ,20 It means that adjectives are confused with nominal pronouns ,
21 It means that personal pronouns are confused with reflexive pronouns ,22 To express doubt / Relationship / The conjunction pronouns are confused ,23 To indicate the confusion of demonstrative pronouns ,24 It means indefinite pronoun confusion ,
25 A pronoun error ,26 To indicate a misuse of punctuation ,27 A spelling mistake ,28 It means a non-standard error )"
" Illustrate with examples ": If new_error_type=5, new_sub_error_type=2, It's a verb missing
"reason": " The specific reason for the mistake ",
"isValidLangChunk": " Similar to the following isValidSent, Judge whether it's a legal fragment ( If the result of language detection is inconsistent with the expectation , It's illegal )"
"analysis": " A concrete analysis of the causes of the error ( Keep the interface , It should be useless for the time being )"
}, ..., {
}
],
"isValidLangSent": " Whether a sentence is legal or not ( Whether it is legal or not depends on whether the recognition result of the sentence is consistent with the expected result )"
"sentFeedback": " Error cause feedback , be based on errorPosInfos All in reason Field splicing ",
"isContainTypoError": " Return whether it contains typo error ",
"isContainGrammarError": " Returns whether there is a syntax error ",
"sentScore": " Sentence score ( It's not useful for the time being , The implementation )"
}
]
}
"totalScore": " The final score of the article "
"fullScore": " Full marks for the corresponding level "
"essayAdvice": " The final evaluation of the article is "
"paraNum": " The number of paragraphs in the article "
"sentNum": " The number of sentences in the article "
}
}

summary

You Dao Zhiyun's English composition correction API Documents are clear , Comprehensive function , It can be used for different types of files 、 Multi dimensional correction of compositions with different levels of difficulty , The evaluation index is clear , The results are very valuable for reference , Fabulous !

Believe in the future , There will be more types of homework marking services , By that time , Teachers and parents can be liberated …

Project address :https://github.com/LemonQH/CorrectWriting

版权声明
本文为[Hand tearing code 800 Li]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/04/20210408132112712l.html

  1. 小白量化投资交易入门课(python入门金融分析)
  2. Python:PyCharm选择性忽略PEP8警告
  3. Python: pychar selectively ignores pep8 warnings
  4. Django-模板
  5. Django template
  6. Python正则表达式大全
  7. 最全Python正则表达式来袭
  8. A python knowledge for Xiaobai
  9. 2. Flexible pandas index
  10. 1. Get to know pandas
  11. See how I use Python to create a magic with baby (one play can play for a day)?
  12. Wow, python can do real-time translation
  13. Python经典编程习题100例
  14. 100 examples of Python classic programming exercises
  15. Invincible, with Python for English teachers to develop a magic tool for English composition correction (support primary school to IELTS)
  16. 抖音数据采集教程,最全python库selenium自动化使用
  17. Pandas 11-综合练习
  18. Pandas 11 - comprehensive exercises
  19. Pandas基础|用户游览日志时间合并排序
  20. python自学 第三章 python语言基础之保留字、标识符与内置函数
  21. python学习例程3-函数
  22. Python GUI 之Tkinter小结 - 知乎
  23. Pandas foundation | user travel log time merge sort
  24. Chapter 3 reserved words, identifiers and built-in functions of the foundation of Python
  25. Tkinter summary of Python GUI - Zhihu
  26. 【Python常用包】itertools
  27. Itertools
  28. [Python] Matplotlib 图表的绘制和美化技巧
  29. Drawing and beautifying skills of [Python] Matplotlib chart
  30. Drawing and beautifying skills of [Python] Matplotlib chart
  31. Python序列之列表(一)
  32. Python解析库lxml与xpath用法总结
  33. Python解析库lxml与xpath用法总结
  34. Usage Summary of Python parsing library lxml and XPath
  35. Usage Summary of Python parsing library lxml and XPath
  36. Python web/HTML GUI
  37. Why is sanic better than Django flame?
  38. Wechat applet Python sends subscription message
  39. How can I use Python to create a magic with children (one can play for one day)?
  40. Pandas module
  41. Machine learning in Python - Boston house price forecast
  42. 50 Great Python modules
  43. Share the survival status of Python practitioners and tell you the real salary of general programmers
  44. Pandas basic operation update
  45. Python Programming day02 Python operator
  46. Usage Summary of Python parsing library lxml and XPath
  47. 1. First meeting pandas
  48. Conversion between Python and base conversion between Python and base
  49. Basics of Python
  50. Fundamentals of python (XIV): errors and exceptions
  51. Fundamentals of python (8): time related modules
  52. Fundamentals of python (I): necessary knowledge for getting started
  53. Operators in Python 3
  54. The list of national computer non graduate schools (captured by Python), just look at this one!
  55. Python data visualization: Seaborn
  56. Quick start pandas (lower)
  57. Operators in Python 3
  58. Python tarfile module
  59. Python basic syntax