1 |
|
---|
2 | import java.util.zip.*;
|
---|
3 | import java.io.BufferedInputStream;
|
---|
4 | import java.io.BufferedOutputStream;
|
---|
5 | import java.io.BufferedReader;
|
---|
6 | import java.io.BufferedWriter;
|
---|
7 | import java.io.File;
|
---|
8 | import java.io.FileInputStream;
|
---|
9 | import java.io.FileNotFoundException;
|
---|
10 | import java.io.FileOutputStream;
|
---|
11 | import java.io.FileReader;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.InputStream;
|
---|
14 | import java.io.InputStreamReader;
|
---|
15 | import java.io.ObjectInputStream;
|
---|
16 | import java.io.OutputStreamWriter;
|
---|
17 | import java.io.PrintWriter;
|
---|
18 | import java.net.Socket;
|
---|
19 | import java.nio.charset.Charset;
|
---|
20 | import java.nio.charset.StandardCharsets;
|
---|
21 | import java.nio.file.Files;
|
---|
22 | import java.nio.file.Paths;
|
---|
23 | import java.util.ArrayList;
|
---|
24 | import java.util.HashMap;
|
---|
25 | import java.util.LinkedList;
|
---|
26 | import java.util.List;
|
---|
27 | import java.util.Scanner;
|
---|
28 |
|
---|
29 |
|
---|
30 | import javax.servlet.ServletConfig;
|
---|
31 | import javax.servlet.ServletException;
|
---|
32 | import javax.servlet.http.HttpServlet;
|
---|
33 | import javax.servlet.http.HttpServletRequest;
|
---|
34 | import javax.servlet.http.HttpServletResponse;
|
---|
35 |
|
---|
36 |
|
---|
37 | public class CapiscoTopicExpansion extends HttpServlet {
|
---|
38 | //private static final long serialVersionUID = 1L;
|
---|
39 |
|
---|
40 | private static final int BUFFER_SIZE = 4096;
|
---|
41 |
|
---|
42 | private static HashMap<String,List<String>> TopicsToSynonyms = new HashMap<String,List<String>>();
|
---|
43 |
|
---|
44 | private static void init_lookup(InputStream is)
|
---|
45 | {
|
---|
46 | try {
|
---|
47 | BufferedInputStream bis = new BufferedInputStream(is);
|
---|
48 | ObjectInputStream ois = new ObjectInputStream(bis);
|
---|
49 | TopicsToSynonyms = (HashMap<String, List<String>>) ois.readObject();
|
---|
50 | bis.close();
|
---|
51 | ois.close();
|
---|
52 | }
|
---|
53 | catch (Exception e) {
|
---|
54 | e.printStackTrace();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | private static void init_lookup(String filename)
|
---|
59 | {
|
---|
60 | try {
|
---|
61 | FileInputStream fis = new FileInputStream(filename);
|
---|
62 | init_lookup(fis);
|
---|
63 | fis.close();
|
---|
64 | }
|
---|
65 | catch (Exception e) {
|
---|
66 | e.printStackTrace();
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private static void lookup_loop()
|
---|
71 | {
|
---|
72 | Scanner scan = new Scanner(System.in);
|
---|
73 | System.out.println("Press enter to start:");
|
---|
74 | String wait = scan.nextLine();
|
---|
75 |
|
---|
76 |
|
---|
77 | System.out.println("Enter words to test (enter '.quit' to quit program)");
|
---|
78 |
|
---|
79 | boolean quit = false;
|
---|
80 | while (!quit) {
|
---|
81 | String test_word = scan.next();
|
---|
82 | if (test_word.equals(".quit")) {
|
---|
83 | quit = true;
|
---|
84 | }
|
---|
85 | else {
|
---|
86 | List<String> synonyms = TopicsToSynonyms.get(test_word);
|
---|
87 |
|
---|
88 | System.out.println("Lookup ["+test_word+"] = " + synonyms);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | scan.close();
|
---|
92 | System.out.println("Program ended.");
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @see Servlet#init(ServletConfig)
|
---|
97 | */
|
---|
98 | public void init(ServletConfig config) throws ServletException
|
---|
99 | {
|
---|
100 | super.init(config);
|
---|
101 |
|
---|
102 | try {
|
---|
103 | InputStream is = getServletContext().getResourceAsStream("/WEB-INF/topics-to-synonyms.ser");
|
---|
104 |
|
---|
105 | init_lookup(is);
|
---|
106 | is.close();
|
---|
107 | }
|
---|
108 | catch (Exception e) {
|
---|
109 | e.printStackTrace();
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
115 | */
|
---|
116 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
---|
117 | PrintWriter pw = response.getWriter();
|
---|
118 |
|
---|
119 | String cgi_topic = request.getParameter("topic");
|
---|
120 |
|
---|
121 | if (cgi_topic != null) {
|
---|
122 | response.setContentType("application/json");
|
---|
123 |
|
---|
124 | String topic = cgi_topic;
|
---|
125 | List<String> synonyms = TopicsToSynonyms.get(topic);
|
---|
126 |
|
---|
127 | pw.append("[");
|
---|
128 | for (int i=0; i<synonyms.size(); i++) {
|
---|
129 | String synonym = synonyms.get(i);
|
---|
130 | if (i>0) {
|
---|
131 | pw.append(",");
|
---|
132 | }
|
---|
133 | pw.append("'" + synonym + "'");
|
---|
134 |
|
---|
135 | }
|
---|
136 |
|
---|
137 | pw.append("]");
|
---|
138 |
|
---|
139 | }
|
---|
140 | else {
|
---|
141 |
|
---|
142 | pw.append("General Info: Number of topics for expansion = " + TopicsToSynonyms.size());
|
---|
143 |
|
---|
144 | }
|
---|
145 | //pw.close();
|
---|
146 |
|
---|
147 | }
|
---|
148 |
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
152 | */
|
---|
153 | protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
---|
154 | doGet(request, response);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | public static void main(String[] args) {
|
---|
159 |
|
---|
160 | String filename = "topics-to-synonyms.ser";
|
---|
161 |
|
---|
162 | init_lookup(filename);
|
---|
163 |
|
---|
164 | lookup_loop();
|
---|
165 |
|
---|
166 | }
|
---|
167 | }
|
---|
168 |
|
---|