【问题标题】:How to make a search by synonyms in Java?如何在 Java 中通过同义词进行搜索?
【发布时间】:2018-01-29 22:41:57
【问题描述】:

我试图制作一个应用程序,用户可以在其中找到输入单词的同义词!用户还可以通过首字母缩略词进行搜索....任何算法都可以帮助我做到这一点或任何想法?并且很多。 像这样的东西:http://www.thesaurus.com/browse/search

【问题讨论】:

    标签: java elasticsearch search full-text-search synonym


    【解决方案1】:

    为简单起见,您可以使用:

    以下是它们如何协同工作:

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class HttpURLConnectionExample {
    
        private final String USER_AGENT = "Mozilla/5.0";
    
        public static void main(String[] args) throws Exception {
            HttpURLConnectionExample http = new HttpURLConnectionExample();
    
            Scanner sc = new Scanner(System.in);
    
            System.out.println("Write a word...");
            String wordToSearch = sc.next();
    
            http.searchSynonym(wordToSearch);
        }
    
    
    
        private void searchSynonym(String wordToSearch) throws Exception {
            System.out.println("Sending request...");
    
            String url = "https://api.datamuse.com/words?rel_syn=" + wordToSearch;
    
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    
            con.setRequestMethod("GET");
            con.setRequestProperty("User-Agent", USER_AGENT);
    
            int responseCode = con.getResponseCode();
            System.out.println("\nSending request to: " + url);
            System.out.println("JSON Response: " + responseCode + "\n");
    
            // ordering the response
            StringBuilder response;
            try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
                String inputLine;
                response = new StringBuilder();
    
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
            }
    
            ObjectMapper mapper = new ObjectMapper();
    
            try {
                // converting JSON array to ArrayList of words
                ArrayList<Word> words = mapper.readValue(
                    response.toString(),
                    mapper.getTypeFactory().constructCollectionType(ArrayList.class, Word.class)
                );
    
                System.out.println("Synonym word of '" + wordToSearch + "':");
                if(words.size() > 0) {
                    for(Word word : words) {
                        System.out.println((words.indexOf(word) + 1) + ". " + word.getWord() + "");
                    }
                }
                else {
                   System.out.println("none synonym word!");
                }
            }
            catch (IOException e) {
                e.getMessage();
            }
        }
    
        // word and score attributes are from DataMuse API
        static class Word {
            private String word;
            private int score;
    
            public String getWord() {return this.word;}
            public int getScore() {return this.score;}
        }
    }
    

    只需将此代码复制粘贴到一个类中,然后将 Jackson jar 添加到项目中:annotationscoredatabind

    希望对你有帮助。

    【讨论】:

    • 我尝试使用DataMuse。它似乎正在工作,但仍然没有为我提供记录。例如,当我说食物时,同义词可以是午餐、晚餐、膳食、小吃、饲料等等。但是 hte API 只为我提供了一个结果——“营养”
    猜你喜欢
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2016-01-08
    • 2014-07-17
    • 1970-01-01
    相关资源
    最近更新 更多