-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServidor.java
306 lines (276 loc) · 10.1 KB
/
Servidor.java
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package polibot_servidor;
import interfaces.Interface_cliente;
import interfaces.Interface_servidor;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Serializable;
import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.Random;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
/**
*
* @author wolfteinter
*/
public class Servidor extends UnicastRemoteObject implements Interface_servidor,
Serializable {
// Atributos
private ArrayList<Par> listaEntrenamiento;
private ArrayList<Intent> ListaIntents;
private String pathNameLogfile;
private File archivo; // la clase File es serializable
public Servidor() throws RemoteException {
super();
this.listaEntrenamiento = new ArrayList<Par>();
this.ListaIntents = new ArrayList<Intent>();
listaEntrenamiento();
tokenizarListaIntents();
this.pathNameLogfile = "archivos_servidor/server.log";
}
// Algoritmo edit distances: Retorna la cantidad mínima de operaciones
// (sustituir, eliminar o insertar) para convertir la cadena s1 a s2.
private int comparar(String s1, String s2) {
int m = s1.length();
int n = s2.length();
int dp[][] = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
for (int j = 0; j <= n; j++) {
if (i == 0) {
dp[i][j] = j;
}
else if (j == 0) {
dp[i][j] = i;
}
else if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
dp[i][j] = dp[i - 1][j - 1];
}
else {
dp[i][j] = 1 + Math.min(dp[i][j-1], Math.min(dp[i-1][j], dp[i-1][j-1]));
}
}
}
return dp[m][n];
}
@Override
public String resolver(String msg) throws RemoteException {
if(msg.equals("verde") || msg.equals("green")) return "verde";
String respuesta = "";
int maxPuntuacion = 0;
String intent = "";
msg = adaptar(msg);
String mensaje[] = msg.split(" ");
int lenMensaje = mensaje.length;
int puntuacion;
Random ran = new Random();
for (Par p : this.listaEntrenamiento) {
puntuacion = 0;
String strEntrenamiento[] = p.getFirst().split(" ");
for (int i = 0; i < lenMensaje; i++) {
for (int j = 0; j < strEntrenamiento.length; j++) {
int valor = comparar(strEntrenamiento[j], mensaje[i]);
//System.out.println("Valor: " + valor);
//puntuacion += ((mensaje[i].length()-(double)valor)*100) / mensaje[i].length();
if (valor == 0) {
puntuacion += mensaje[i].length() * 3;
break;
}
else {
puntuacion += mensaje[i].length() - valor;
}
//else if (valor < 5) puntuacion++;
}
}
if (puntuacion > maxPuntuacion) {
maxPuntuacion = puntuacion;
intent = p.getSecond();
//System.out.println("YES");
//System.out.println(" ---------------" + p.getSecond() + " " + puntuacion);
}
//System.out.println("############################## " + puntuacion);
}
if (maxPuntuacion <= 6) {
intent = "default_intent";
}
//System.out.println("Se elige : " + intent); // debug
for (Intent i : this.ListaIntents) {
if ((i.getNombre().toLowerCase()).equals(intent)) {
int pos = ran.nextInt(i.getRespuestas().length);
respuesta = i.getRespuestas()[pos];
break;
}
}
return respuesta;
}
@Override
public byte[] descargarArchivo(String pathname) throws RemoteException {
byte datos[];
File archivo = new File(pathname);
datos = new byte[(int)archivo.length()];
FileInputStream entrada = null;
try {
entrada = new FileInputStream(archivo);
entrada.read(datos, 0, datos.length);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
entrada.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
return datos;
}
@Override
public void registrarActividad(Interface_cliente cliente) throws RemoteException {
String tiempo, nombre, correo;
String pattern = "hh:mm:ss dd-MM-yyyy";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
tiempo = simpleDateFormat.format(new Date());
nombre = cliente.getNombre();
correo = cliente.getCorreo();
// Fecha y hora | Nombre | Correo
System.out.println(tiempo + ", " + nombre + ", " + correo);
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(this.pathNameLogfile, true));
out.write(tiempo + ", " + nombre + ", " + correo + "\n");
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
out.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
private String adaptar(String cadena) {
String aux = cadena.toLowerCase();
aux = aux.replace(",", "");
aux = aux.replace("?", "");
aux = aux.replace("¿", "");
aux = aux.replace("¡", "");
aux = aux.replace("!", "");
aux = aux.replace(")", "");
aux = aux.replace("(", "");
aux = aux.replace(".", "");
aux = aux.replace("á", "a");
aux = aux.replace("é", "e");
aux = aux.replace("ó", "o");
aux = aux.replace("í", "i");
aux = aux.replace("ú", "u");
return aux;
}
//Recupera los intents del archivo
private void tokenizarListaIntents() {
String texto, aux;
LinkedList<String> lista = new LinkedList();
//ArrayList<Patron> patrones = new ArrayList<>();
try {
//recorremos el archivo y lo leemos
FileReader archivos =
new FileReader("archivos_servidor/conocimiento/intents.txt");
BufferedReader lee = new BufferedReader(archivos);
while ((aux = lee.readLine()) != null) {
texto = aux;
lista.add(texto);
}
lee.close();
//System.out.println(lista.size());
ArrayList<String> lista2 = new ArrayList<>();
String clase = "";
for (int i = 0; i < lista.size(); i++) {
StringTokenizer st = new StringTokenizer(lista.get(i), "&");
while (st.hasMoreTokens()) {
lista2.add(st.nextToken());
}
String[] vector = new String[lista2.size() - 1];
for (int x = 1; x < lista2.size(); x++) {
vector[x - 1] = lista2.get(x);
}
clase = lista2.get(0);
this.ListaIntents.add(new Intent(clase, vector));
// patrones.add();
lista2.clear();
}
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex + ""
+ "\nNo se ha encontrado el archivo",
"ADVERTENCIA!!!", JOptionPane.WARNING_MESSAGE);
}
}
//recupera las posibles respuestas
private void listaEntrenamiento() {
String texto, aux;
LinkedList<String> lista = new LinkedList();
try {
//llamamos el método que permite cargar la ventana
//abrimos el archivo seleccionado
//File abre = new File("conocimiento/entrenamiento.txt");
//recorremos el archivo y lo leemos
FileReader archivos =
new FileReader("archivos_servidor/conocimiento/entrenamiento.txt");
BufferedReader lee = new BufferedReader(archivos);
while ((aux = lee.readLine()) != null) {
texto = aux;
texto = adaptar(texto);
lista.add(texto);
}
lee.close();
ArrayList<String> lista2 = new ArrayList<>();
for (int i = 0; i < lista.size(); i++) {
StringTokenizer st = new StringTokenizer(lista.get(i), "&");
while (st.hasMoreTokens()) {
lista2.add(st.nextToken());
}
// a la coleccion de patrones se agrega un nuevo patron
this.listaEntrenamiento.add(new Par(lista2.get(0), lista2.get(1)));
lista2.clear();
}
}
catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex + ""
+ "\nNo se ha encontrado el archivo",
"ADVERTENCIA!!!", JOptionPane.WARNING_MESSAGE);
}
}
public static void main(String[] args) {
try {
Servidor serv = new Servidor();
Registry reg = LocateRegistry.createRegistry(1099);
reg.bind("servidor", serv);
System.out.println("Servidor está corriendo...");
}
catch (RemoteException ex) {
ex.printStackTrace();
}
catch (AlreadyBoundException ex) {
ex.printStackTrace();
}
}
}