如何通过 Java 中的同义词进行搜索?
How to make a search by synonyms in Java?
我正在尝试制作一个应用程序,用户可以在其中找到输入单词的同义词!用户还可以通过首字母缩写词进行搜索....任何算法都可以帮助我做到这一点或任何想法?非常感谢。
像这样的东西:http://www.thesaurus.com/browse/search
为了简单起见,您可以使用:
- HttpURLConnection 用于 HTTP 请求;
- DataMuse API 作为要使用的 Web 服务;
- Jackson 将 JSON 响应转换为 Java 对象;
以下是他们如何协同工作:
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;}
}
}
只需将此代码复制粘贴到 class 并将 Jackson 罐子添加到项目中:annotations, core and databind。
希望对您有所帮助。
我正在尝试制作一个应用程序,用户可以在其中找到输入单词的同义词!用户还可以通过首字母缩写词进行搜索....任何算法都可以帮助我做到这一点或任何想法?非常感谢。 像这样的东西:http://www.thesaurus.com/browse/search
为了简单起见,您可以使用:
- HttpURLConnection 用于 HTTP 请求;
- DataMuse API 作为要使用的 Web 服务;
- Jackson 将 JSON 响应转换为 Java 对象;
以下是他们如何协同工作:
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;}
}
}
只需将此代码复制粘贴到 class 并将 Jackson 罐子添加到项目中:annotations, core and databind。
希望对您有所帮助。