|
@@ -0,0 +1,90 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.project.sparkjava;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+import static spark.Spark.get;
|
|
5
|
+import static spark.Spark.modelAndView;
|
|
6
|
+import static spark.Spark.port;
|
|
7
|
+
|
|
8
|
+import java.sql.Connection;
|
|
9
|
+import java.sql.DriverManager;
|
|
10
|
+import java.sql.ResultSet;
|
|
11
|
+import java.sql.SQLException;
|
|
12
|
+import java.sql.Statement;
|
|
13
|
+import java.util.ArrayList;
|
|
14
|
+import java.util.HashMap;
|
|
15
|
+import java.util.List;
|
|
16
|
+import java.util.Map;
|
|
17
|
+
|
|
18
|
+import org.bitbucket.eunjeon.seunjeon.Analyzer;
|
|
19
|
+import org.bitbucket.eunjeon.seunjeon.LNode;
|
|
20
|
+import org.bitbucket.eunjeon.seunjeon.Morpheme;
|
|
21
|
+
|
|
22
|
+import kr.co.swh.lecture.opensource.sparkjava.FreeMarkerTemplateEngine;
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+public class Step2 {
|
|
26
|
+
|
|
27
|
+ final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
|
|
28
|
+ final String DB_URL = "jdbc:mysql://dev-swh.ga:3306/market";
|
|
29
|
+
|
|
30
|
+ final String USERNAME = "root";
|
|
31
|
+ final String PASSWORD = "swhacademy!";
|
|
32
|
+
|
|
33
|
+ Statement stmt;
|
|
34
|
+ ResultSet res;
|
|
35
|
+ Connection conn = null;
|
|
36
|
+
|
|
37
|
+ List<WordCloud> result = new ArrayList<WordCloud>();
|
|
38
|
+
|
|
39
|
+ public Step2() throws ClassNotFoundException, SQLException {
|
|
40
|
+ Class.forName(JDBC_DRIVER);
|
|
41
|
+ conn = DriverManager.getConnection(DB_URL, USERNAME, PASSWORD);
|
|
42
|
+ }
|
|
43
|
+
|
|
44
|
+ public void excute() {
|
|
45
|
+ port(45678);
|
|
46
|
+ Map<String, Object> attributes = new HashMap<>();
|
|
47
|
+
|
|
48
|
+ get("/news/cloud", (request, response) -> {
|
|
49
|
+ Map<String, Integer> contents = new HashMap<String, Integer>();
|
|
50
|
+
|
|
51
|
+ String acQuery = "select script from NEWS limit 10";
|
|
52
|
+ stmt = conn.createStatement();
|
|
53
|
+ res = stmt.executeQuery(acQuery);
|
|
54
|
+ while (res.next()) {
|
|
55
|
+ for (LNode node : Analyzer.parseJava(res.getString("script"))) {
|
|
56
|
+ Morpheme m = node.morpheme();
|
|
57
|
+// System.out.println(m.surface() + " / 품사:" + m.feature().head());
|
|
58
|
+ if(m.feature().head().equalsIgnoreCase("nnp") || m.feature().head().equalsIgnoreCase("nng")) {
|
|
59
|
+ WordCloud w = null;
|
|
60
|
+ if(contents.containsKey(m.surface())) {
|
|
61
|
+ contents.put(m.surface(), contents.get(m.surface()) + 1);
|
|
62
|
+ w = new WordCloud(m.surface(), contents.get(m.surface()) + 1);
|
|
63
|
+ }else {
|
|
64
|
+ contents.put(m.surface(), 1);
|
|
65
|
+ w = new WordCloud(m.surface(), 1);
|
|
66
|
+ }
|
|
67
|
+ result.remove(w);
|
|
68
|
+ result.add(w);
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+ }
|
|
72
|
+ attributes.put("message", result);
|
|
73
|
+ return modelAndView(attributes, "news-cloud.ftl");
|
|
74
|
+ }, new FreeMarkerTemplateEngine());
|
|
75
|
+ }
|
|
76
|
+ public static void main(String[] args) {
|
|
77
|
+
|
|
78
|
+ Step2 s;
|
|
79
|
+ try {
|
|
80
|
+ s = new Step2();
|
|
81
|
+ s.excute();
|
|
82
|
+ } catch (ClassNotFoundException e) {
|
|
83
|
+ // TODO Auto-generated catch block
|
|
84
|
+ e.printStackTrace();
|
|
85
|
+ } catch (SQLException e) {
|
|
86
|
+ // TODO Auto-generated catch block
|
|
87
|
+ e.printStackTrace();
|
|
88
|
+ }
|
|
89
|
+ }
|
|
90
|
+}
|