简体   繁体   English

通过阅读网站创建拼写检查器

[英]creating spell checker by reading a website

For my computer science class we are to create a program that has the user input a word and we take that word and guess to see if it is spelled right. 对于我的计算机科学班,我们将创建一个程序,让用户输入一个单词,然后我们将该单词用作猜测,以查看其拼写是否正确。 We are inputting the word into dictionary.com and reading it from their. 我们正在将单词输入dictionary.com并从他们的网站中读取。

We are adding the word they inputted to the dictionary.com url and then we check to see if that word shows up on the website. 我们将他们输入的单词添加到dictionary.com url中,然后检查该单词是否出现在网站上。 If it does it is spelled correctly and then if it's not it's not spelled correctly. 如果正确,则拼写正确,如果不正确,则拼写错误。 Here is the code for reading the URL: 这是读取URL的代码:

URL url = new URL("http://www.dictionary.com/search?q=" + word);

        // open a buffered reader on the url
        InputStream inStr = url.openStream();

        BufferedReader reader = 
          new BufferedReader(new InputStreamReader(inStr));

When they do spell the word correctly everything works out great but when they spell it incorrectly it throws a file not found exception. 当他们正确拼写该单词时,一切工作都很好,但是当他们拼写不正确时,它将抛出文件未找到异常。 It is throwing this because if you search a word on dictionary.com it changes the link. 之所以抛出此错误,是因为如果您在dictionary.com上搜索单词,则会更改链接。 Where it would usually be "www.dictionary.com/search?q=word" it changes to " http://www.dictionary.com/misspelling?term=wrod&s=t " 通常为“ www.dictionary.com/search?q=word”的地方,将其更改为“ http://www.dictionary.com/misspelling?term=wrod&s=t

Because it changes the link, it throws the exception and the program does not work. 因为它更改了链接,所以它将引发异常,并且程序无法运行。 Does anybody have any suggestions on what I should do or change? 有人对我应该做什么或更改有什么建议吗? Thank you! 谢谢!

Hacky way: Catch the exception and return "not a word." hacky方式:捕获异常并返回“一言不发”。 The problem with that approach is that if the website is unavailable it will return "not a word." 这种方法的问题在于,如果网站不可用,它将返回“一言不发”。

Best way: check to see if you get a HTTP 200 code or an HTTP 302 (the redirect) and return "not a word." 最佳方法:检查是否收到HTTP 200代码或HTTP 302 (重定向)并返回“ not a word”。

I agree with the highly voted comment that you should catch the exception, but I would recommend that you use the HttpURLConnection class to make your GET call to dictionary.com with a given word. 我同意您应该捕获该异常的高度赞誉的评论,但是我建议您使用HttpURLConnection类使用给定的单词对Dictionary.com进行GET调用。 If you follow the pattern below, you can still capture any error stream coming from the site. 如果遵循以下模式,您仍然可以捕获来自该站点的任何错误流。 And you should be able to parse the response and check to make sure that in fact it came from a wrong word. 而且您应该能够解析响应并检查以确保它实际上来自错误的单词。 There is the possibility of an exception happening for some other reason, you should not necessarily count this as the word not being found. 由于某些其他原因可能会发生异常,您不必将其视为未找到该词。

String url = "http://www.dictionary.com/search?q=" + word";

URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");

int responseCode = con.getResponseCode();
// check the response code if you wish
InputStream inputStream = null;
try {
    inputStream = con.getInputStream();
}
catch (IOException exception) {
    inputStream = con.getErrorStream();
}
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));

Update: 更新:

I just tried entering a word which could not be found, and the HTTP response code is 404 . 我只是尝试输入一个找不到的单词,HTTP响应代码是404 So you might be able to get away with just checking the response code for 404 , or to be more thorough you could even parse the error stream. 因此,您可能只需要检查404的响应代码就可以摆脱404 ,或者更彻底地了解错误流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM