-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatbot.java
More file actions
136 lines (116 loc) · 3.85 KB
/
Chatbot.java
File metadata and controls
136 lines (116 loc) · 3.85 KB
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
package chatbot;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.concurrent.TimeUnit;
public class Chatbot extends JFrame {
JTextArea msgArea = new JTextArea();
JTextField type = new JTextField();
JButton ok = new JButton("ok");
JFrame frame = new JFrame();
String msgs = "";
String file = "src\\meta.txt";
String subprogram = "src\\testing.py";
Chatbot() {
frame.setSize(1200, 900);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//clear file
try {
PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
} catch (Exception e) {
System.out.println(e);
}
init();
listeners();
}
public void init() {
msgArea.setBounds(50, 50, 800, 600);
frame.add(msgArea, new Integer(1));
type.setBounds(50, 800, 800, 30);
frame.add(type, new Integer(1));
ok.setBounds(900, 800, 100, 30);
frame.add(ok, new Integer(1));
msgArea.setEditable(false);
}
public void listeners() {
ok.addActionListener((ActionEvent arg0) -> {
update();
python();
try {
TimeUnit.MILLISECONDS.sleep(500);
} catch (Exception e) {
System.out.println(e);
}
String answer = read(file);
msgs = msgs + "\n" + split("AI: " + answer);
msgArea.setText(msgs);
});
}
public void update() {
if (type.getText() != "") {
write(type.getText(), file);
msgs = msgs + "\n" + split("You: " + type.getText());
msgArea.setText(msgs);
type.setText("");
}
}
public void python() {
try {
String prg = "def main():\n"
+ " f = open(\"C:\\\\Users\\\\piopi\\\\Documents\\\\Chatbot\\\\src\\\\meta.txt\", \"a+\")\n"
+ "\n"
+ " f.write(\"Python answer \\n\")\n"
+ "\n"
+ " f.close()\n"
+ "\n"
+ "main()";
BufferedWriter out = new BufferedWriter(new FileWriter(subprogram));
out.write(prg);
out.close();
Process p = Runtime.getRuntime().exec("python " + subprogram);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String ret = in.readLine();
System.out.println("value is : " + ret);
} catch (Exception e) {
System.out.println(e);
}
}
public static void write(String question, String directory) {
try (FileWriter fw = new FileWriter(directory, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(question);
} catch (IOException e) {
System.out.println(e);
}
}
public static String read(String directory) {
String lastLine = "";
try {
String sCurrentLine;
BufferedReader br = new BufferedReader(new FileReader(directory));
while ((sCurrentLine = br.readLine()) != null) {
lastLine = sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
}
return lastLine;
}
public String split(String text) {
String output = "";
int index = 0;
while (index < text.length()) {
output = output + text.substring(index, Math.min(index + 100, text.length())) + "\n";
index += 100;
}
return output;
}
public static void main(String[] args) {
Chatbot c1 = new Chatbot();
}
}