首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >拼写、检查多个文档和报告结果的脚本

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

Stack Overflow用户
提问于 2016-09-06 18:14:33
回答 2查看 700关注 0票数 2

我教一堂课,学生们每周对指定的阅读做简短的反应。作业的评分是基于简单的规则,其中之一是完美的拼写。完美的拼写是一个简单的方法来评估他们的工作,在课堂上,我有兴趣花时间阅读提交的内容。

我想写一个脚本,接受所有学生的意见,并逐个检查拼写是否完美。问题是,我不知道该怎么做。我的一个想法是编写一个Python脚本,它接收每个作业,并通过Microsoft运行它,并收集拼写错误的单词数。但这样的事情可能吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-06 18:44:55

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

下面是我刚刚编写的一段示例代码,您可以使用这些代码作为模板:

代码语言:javascript
代码运行次数:0
运行
复制
#!/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()
票数 1
EN

Stack Overflow用户

发布于 2016-09-06 18:17:33

我不确定Microsoft路由是否可行,但您可以使用API,例如这。

这里是他们的文档网站上的Javascript示例:

代码语言:javascript
代码运行次数:0
运行
复制
<!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>

下面是两个Python示例:

代码语言:javascript
代码运行次数:0
运行
复制
########### 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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39355375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档