java - 使用 Java 的拼写检查器,必须打印出特定行

标签 java dictionary cmd spell-checking

  • 完成上述所有操作,然后打印出拼写错误的单词的行号和该行。例如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/

相关文章:

java - 创建的 String 对象的数量

python - 动态传递字典名称python

Java 相当于 Python 字典

使用cmd命令 'copy'在C中复制文件

r - base::assign (".ptime", proc.time(), pos = "CheckExEnv") 使用 devtools::check 时出现错误

c# - 从 C# 运行时命令行进程不工作

java - spring boot 中嵌入的geoserver 与hibernate/jpa 冲突

java - 如何在 Spring 中将一组组件与 Swing 布局管理器居中对齐

java - 如何使用以 X 作为参数并返回 Observable<Y> 的函数从 Observable<List<X>> 转换为 Observable<List<Y>>

c# - 使用字典作为过滤方法参数的动态 LINQ 函数