- 完成上述所有操作,然后打印出拼写错误的单词的行号和该行。例如1:我是一只住在房子里的狐狸
- 所有这些都应该打印在命令行上
最佳答案
我使用快速 jSpell 检查器进行拼写检查 注意:- 生成您自己的 API key
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SOTest
{
public static void main(String[] args) {
try {
//Load text file into List
List<String> list = Files.readAllLines(Paths.get("Words.txt"), StandardCharsets.UTF_8);
//Iterate List to scan the spell error
for (String line : list) {
for(String err:spellCheck(line)){
if(line.contains(err)) {
line = line.replace(err, "^^"+err+"^^");
}
}
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static List<String> spellCheck(String input) throws IOException {
List<String> results = new ArrayList<String>();
byte[] postData = new String("{\t\"language\": \"enUS\",\t\"fieldvalues\": \""+input+"\",\t\"config\": {\t\t\"forceUpperCase\": false,\t\t\"ignoreIrregularCaps\": false,\t\t\"ignoreFirstCaps\": true,\t\t\"ignoreNumbers\": true,\t\t\"ignoreUpper\": false,\t\t\"ignoreDouble\": false,\t\t\"ignoreWordsWithNumbers\": true\t}}").getBytes();
String request = "https://jspell-checker.p.rapidapi.com/check";
URL url = new URL(request);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setDoOutput( true );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "x-rapidapi-host", "jspell-checker.p.rapidapi.com");
conn.setRequestProperty( "x-rapidapi-key", "32efb09328msh3e3b62d34ac8cfcp1467a4jsnb3ef821b4b23");
conn.setRequestProperty( "content-type", "application/json");
conn.setRequestProperty( "accept", "application/json");
conn.setRequestProperty( "useQueryString", "true");
conn.setUseCaches( false );
try( DataOutputStream wr = new DataOutputStream( conn.getOutputStream())) {
wr.write( postData );
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
final String regex = "\"word\":\"[a-zA-Z]*\"";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(strCurrentLine);
while (matcher.find()) {
results.add(matcher.group(0).split(":")[1].replace("\"", ""));
}
}
return results;
}
}
在Words.txt内输入
this is example
thiss is example
my Dog is hudden under cor
输出
this is example
^^thiss^^ is example
my Dog is ^^hudden^^ under ^^cor^^
关于java - 使用 Java 的拼写检查器,必须打印出特定行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61876871/