tobby48 5 years ago
parent
commit
4933a6e651

+ 67
- 0
src/main/java/kr/co/swh/lecture/opensource/discord/project/DicAnalysis.java View File

@@ -0,0 +1,67 @@
1
+package kr.co.swh.lecture.opensource.discord.project;
2
+
3
+import java.io.IOException;
4
+import java.sql.Connection;
5
+import java.sql.DriverManager;
6
+import java.sql.PreparedStatement;
7
+import java.sql.SQLException;
8
+
9
+import org.apache.http.HttpResponse;
10
+import org.apache.http.client.methods.HttpGet;
11
+import org.apache.http.impl.client.CloseableHttpClient;
12
+import org.apache.http.impl.client.HttpClients;
13
+import org.apache.http.util.EntityUtils;
14
+
15
+import com.google.gson.Gson;
16
+
17
+public class DicAnalysis {
18
+
19
+	public static void main(String[] args) {
20
+		// TODO Auto-generated method stub
21
+		Connection connection = null;
22
+		try {
23
+			Class.forName("com.mysql.jdbc.Driver");
24
+			connection = DriverManager.getConnection("jdbc:mysql://dev-swh.ga:3306/market","root","swhacademy!");
25
+		} catch (SQLException e1) {
26
+			// TODO Auto-generated catch block
27
+			e1.printStackTrace();
28
+		} catch (ClassNotFoundException e) {
29
+			// TODO Auto-generated catch block
30
+			e.printStackTrace();
31
+		}
32
+			
33
+		Gson gson = new Gson();
34
+		CloseableHttpClient client = HttpClients.createDefault();
35
+		HttpResponse response = null;
36
+		String responseString = "";
37
+		HttpGet requestGet = new HttpGet("https://raw.githubusercontent.com/park1200656/KnuSentiLex/master/data/SentiWord_info.json");
38
+		try {
39
+			response = client.execute(requestGet);
40
+			responseString = EntityUtils.toString(response.getEntity());
41
+			SentimentalDic[] value = gson.fromJson(responseString, SentimentalDic[].class);
42
+			for(SentimentalDic dic : value) {
43
+				String insertTableSQL = "INSERT INTO SENTIMENTAL_DIC"
44
+						+ "(word_root, polarity) VALUES"
45
+						+ "(?,?)";
46
+				PreparedStatement preparedStatementInsert = connection.prepareStatement(insertTableSQL);
47
+				preparedStatementInsert.setString(1, dic.getWord_root());
48
+				preparedStatementInsert.setInt(2, Integer.parseInt(dic.getPolarity()));
49
+				preparedStatementInsert.executeUpdate();
50
+			}
51
+		} catch (IOException e2) {
52
+			// TODO Auto-generated catch block
53
+			e2.printStackTrace();
54
+		} catch (SQLException e) {
55
+			// TODO Auto-generated catch block
56
+			e.printStackTrace();
57
+		} finally {
58
+			try {
59
+				connection.close();
60
+			} catch (SQLException e) {
61
+				// TODO Auto-generated catch block
62
+				e.printStackTrace();
63
+			}
64
+		}
65
+	}
66
+
67
+}

+ 44
- 4
src/main/java/kr/co/swh/lecture/opensource/discord/project/SendJDA.java View File

@@ -1,6 +1,11 @@
1 1
 package kr.co.swh.lecture.opensource.discord.project;
2 2
 
3 3
 import java.io.IOException;
4
+import java.sql.Connection;
5
+import java.sql.DriverManager;
6
+import java.sql.ResultSet;
7
+import java.sql.SQLException;
8
+import java.sql.Statement;
4 9
 import java.util.ArrayList;
5 10
 import java.util.Arrays;
6 11
 import java.util.List;
@@ -25,7 +30,7 @@ public class SendJDA {
25 30
 	public static JDA jda;
26 31
 	public static void main(String[] args) {
27 32
 		// TODO Auto-generated method stub
28
-		List<SentimentalDic> dicList = initDatas();
33
+		List<SentimentalDic> dicList = initDatabase();
29 34
 		JDABuilder jb = new JDABuilder(AccountType.BOT);
30 35
 		jb.setAutoReconnect(true);
31 36
 		jb.setStatus(OnlineStatus.DO_NOT_DISTURB);
@@ -51,9 +56,6 @@ public class SendJDA {
51 56
 			response = client.execute(requestGet);
52 57
 			responseString = EntityUtils.toString(response.getEntity());
53 58
 			SentimentalDic[] value = gson.fromJson(responseString, SentimentalDic[].class);
54
-			for(SentimentalDic dic : value) {
55
-				System.out.println(dic.getWord());
56
-			}
57 59
 			dicList.addAll(Arrays.asList(value));
58 60
 		} catch (IOException e2) {
59 61
 			// TODO Auto-generated catch block
@@ -61,4 +63,42 @@ public class SendJDA {
61 63
 		}
62 64
 		return dicList;
63 65
 	}
66
+	
67
+	public static List<SentimentalDic> initDatabase(){
68
+		List<SentimentalDic> dicList = new ArrayList<SentimentalDic>();
69
+		Connection connection = null;
70
+		try {
71
+			Class.forName("com.mysql.jdbc.Driver");
72
+			connection = DriverManager.getConnection("jdbc:mysql://dev-swh.ga:3306/market","root","swhacademy!");
73
+		} catch (SQLException e1) {
74
+			// TODO Auto-generated catch block
75
+			e1.printStackTrace();
76
+		} catch (ClassNotFoundException e) {
77
+			// TODO Auto-generated catch block
78
+			e.printStackTrace();
79
+		}
80
+		Statement statement = null;
81
+		try {
82
+			statement = connection.createStatement();
83
+			ResultSet rs = statement.executeQuery("SELECT * FROM SENTIMENTAL_DIC;");
84
+			while(rs.next()){
85
+				SentimentalDic dic = new SentimentalDic();
86
+				dic.setWord_root(rs.getString("word_root"));
87
+				dic.setPolarity(String.valueOf(rs.getInt("polarity")));
88
+				dicList.add(dic);
89
+			}
90
+			rs.close();
91
+		} catch (SQLException e) {
92
+			// TODO Auto-generated catch block
93
+			e.printStackTrace();
94
+		} finally {
95
+			try {
96
+				connection.close();
97
+			} catch (SQLException e) {
98
+				// TODO Auto-generated catch block
99
+				e.printStackTrace();
100
+			}
101
+		}
102
+		return dicList;
103
+	}
64 104
 }

+ 25
- 10
src/main/java/kr/co/swh/lecture/opensource/discord/project/TListener.java View File

@@ -2,13 +2,15 @@ package kr.co.swh.lecture.opensource.discord.project;
2 2
 
3 3
 import java.util.List;
4 4
 
5
-import kr.co.swh.lecture.opensource.discord.TwitterContents;
5
+import org.bitbucket.eunjeon.seunjeon.Analyzer;
6
+import org.bitbucket.eunjeon.seunjeon.LNode;
7
+import org.bitbucket.eunjeon.seunjeon.Morpheme;
8
+
6 9
 import net.dv8tion.jda.api.entities.Message;
7 10
 import net.dv8tion.jda.api.entities.TextChannel;
8 11
 import net.dv8tion.jda.api.entities.User;
9 12
 import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
10 13
 import net.dv8tion.jda.api.hooks.ListenerAdapter;
11
-import twitter4j.Status;
12 14
 
13 15
 public class TListener extends ListenerAdapter{
14 16
 	List<SentimentalDic> dicList;
@@ -23,16 +25,29 @@ public class TListener extends ListenerAdapter{
23 25
 		TextChannel tc = event.getTextChannel();
24 26
 		Message msg = event.getMessage();
25 27
 		if(user.isBot()) return;
26
-		if(msg.getContentRaw().charAt(0) == '!') {
27
-			String[] args = msg.getContentRaw().substring(1).split(" ");
28
-			if(args.length < 0) return;
29
-			if(args.length == 2 && args[0].equalsIgnoreCase("lol")) {
30
-				List<Status> status = new TwitterContents().getContents(args[1]);
31
-				for(Status s : status) {
32
-					tc.sendMessage(s.getText()).queue();
28
+//		// 형태소 분석
29
+//		for (Eojeol eojeol : Analyzer.parseEojeolJava(msg.getContentRaw())) {
30
+//			for (LNode node : eojeol.nodesJava()) {
31
+//				Morpheme m = node.morpheme();
32
+//				tc.sendMessage(m.surface() + " / 품사:" + m.feature().head()).queue();
33
+//			}
34
+//		}
35
+		int count = 0;
36
+		for (LNode node : Analyzer.parseJava(msg.getContentRaw())) {
37
+			Morpheme m = node.morpheme();
38
+			if(!m.feature().head().equalsIgnoreCase("jks")) {
39
+				for(SentimentalDic dic : dicList) {
40
+					if(dic.getWord_root().equalsIgnoreCase(m.surface())){
41
+						count += Integer.parseInt(dic.getPolarity());
42
+					}
33 43
 				}
34 44
 			}
35
-		}
45
+		}	
46
+		if(count > 0)
47
+			tc.sendMessage(String.format("%s가 해피하네요", user.getName())).queue();
48
+		else if(count < 0)
49
+			tc.sendMessage(String.format("%s가 우울하네요", user.getName())).queue();
50
+		
36 51
 	}
37 52
 
38 53
 }