用于拼写检查多个文档和报告结果的脚本

时间:2016-09-06 18:14:33

标签: python

我教授一堂课,学生每次都会对指定的阅读作出短暂的反应。作业根据简单的规则进行评分,其中一个是完美的拼写。完美的拼写是一种评估他们在课堂上工作的简单方法,我有兴趣花时间阅读内容提交。

我想编写一个脚本,收集所有学生提交的内容并逐一检查拼写是否完美。问题是,我不知道如何最好地做到这一点。我有一个想法是编写一个Python脚本,它接受每个任务并通过Microsoft Word运行它并收集拼写错误的单词数。但是这样的事情甚至可能吗?

2 个答案:

答案 0 :(得分:1)

我不确定Microsoft Word路由是否可行,但您可以使用API​​,例如this.

以下是他们在文档网站上的Javascript示例:

<!DOCTYPE html>
<html>
<head>
<title>JSSample</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
</head>
<body>

<script type="text/javascript">
$(function() {
    var params = {
        // Request parameters
        "text": "Bill Gatas",
        "mode": "{string}",
        "preContextText": "{string}",
        "postContextText": "{string}",
    };

    $.ajax({
        url: "https://api.cognitive.microsoft.com/bing/v5.0/spellcheck/?" + $.param(params),
        beforeSend: function(xhrObj){
            // Request headers
            xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","{subscription key}");
        },
        type: "GET",
        // Request body
        data: "{body}",
    })
    .done(function(data) {
        alert("success");
    })
    .fail(function() {
        alert("error");
    });
});
</script>
</body>
</html>

以下是2个Python示例:

########### Python 2.7 #############
import httplib, urllib, base64

headers = {
# Request headers
'Ocp-Apim-Subscription-Key': '{subscription key}',
}

params = urllib.urlencode({
# Request parameters
'text': 'Bill Gatas',
'mode': '{string}',
'preContextText': '{string}',
'postContextText': '{string}',
})

try:
conn = httplib.HTTPSConnection('api.cognitive.microsoft.com')
conn.request("GET", "/bing/v5.0/spellcheck/?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

####################################

########### Python 3.2 #############
import http.client, urllib.request, urllib.parse, urllib.error, base64

headers = {
# Request headers
'Ocp-Apim-Subscription-Key': '{subscription key}',
}

params = urllib.parse.urlencode({
# Request parameters
'text': 'Bill Gatas',
'mode': '{string}',
'preContextText': '{string}',
'postContextText': '{string}',
})

try:
conn = http.client.HTTPSConnection('api.cognitive.microsoft.com')
conn.request("GET", "/bing/v5.0/spellcheck/?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

####################################

答案 1 :(得分:1)

如果使用pip等下载PyEnchant拼写检查库,则任务大大简化。

以下是我刚刚撰写的一段代码示例,您可以将其用作模板:

#!/usr/bin/env python

import enchant

THRESHOLD = 1   # harsh

def numIncorrect(words_in_file):
    """
    @param words_in_file - an iterable of words in the current students submission.
    @return - the number of misspelled words in this submission.
    """  
    word_dict = enchant.Dict("en_US")
    count = 0
    for word in file:
        if not word_dict.check(word): count +=1
    return count;


def main():
    for filename in os.listdir('.'):    # assuming student submissions are in current directory. You can change this depending on how the files are stored (if they are online you could download them etc.)
        # ... Process filename i.e get student name out and open file, create a list with all its words (one liner) 
        if numIncorrect(words_in_file) > THRESHOLD:
            # ... mark this student for a deduction

if __name__ == '__main__':
    main()
相关问题