【发布时间】:2018-01-29 22:41:57
【问题描述】:
我试图制作一个应用程序,用户可以在其中找到输入单词的同义词!用户还可以通过首字母缩略词进行搜索....任何算法都可以帮助我做到这一点或任何想法?并且很多。 像这样的东西:http://www.thesaurus.com/browse/search
【问题讨论】:
标签: java elasticsearch search full-text-search synonym
我试图制作一个应用程序,用户可以在其中找到输入单词的同义词!用户还可以通过首字母缩略词进行搜索....任何算法都可以帮助我做到这一点或任何想法?并且很多。 像这样的东西:http://www.thesaurus.com/browse/search
【问题讨论】:
标签: java elasticsearch search full-text-search synonym
为简单起见,您可以使用:
以下是它们如何协同工作:
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 添加到项目中:annotations、core 和 databind。
希望对你有帮助。
【讨论】: