/
TextSimilaritySimpleWrapper
63 lines (48 loc) · 1.88 KB
/
TextSimilaritySimpleWrapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.rxnlp.library;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TextSimilaritySimpleWrapper {
public static void main(String args[]){
//This is the target URL
URL targetUrl;
try {
targetUrl = new URL("https://rxnlp-core.p.mashape.com/computeSimilarity");
//First set the headers
HttpURLConnection httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setDoOutput(true);
httpConnection.setRequestMethod("POST");
httpConnection.setRequestProperty("Content-Type", "application/json");
httpConnection.setRequestProperty("X-Mashape-Key", "<GET_YOUR_MASHAPE_KEY>");
String str1="كتب عربية قصيرة : قصص قصيرة، مختصرات ، الخ";
String str2="عربية قصيرة : قصص قصيرة، مختصرات ، الخ";
//Then set input
String input = "{\"text1\":\""+str1
+"\",\"text2\":\""+str2
+"\",\"clean\":\"true\"}";
//Next, process output
OutputStream outputStream = httpConnection.getOutputStream();
outputStream.write(input.getBytes());
outputStream.flush();
//Throw exception on error
if (httpConnection.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ httpConnection.getResponseCode());
}
BufferedReader responseBuffer = new BufferedReader(new InputStreamReader((httpConnection.getInputStream())));
//Printing output from server (you can use a json parser here instead)
String output;
System.out.println("Output from Server:\n");
while ((output = responseBuffer.readLine()) != null) {
System.out.println(output);
}
//disconnect from server
httpConnection.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}