tobby48 пре 5 година
родитељ
комит
27d7d8100d

+ 18
- 0
src/main/java/kr/co/swh/lecture/opensource/project/sparkjava/ListObjectRemoveExample.java Прегледај датотеку

@@ -0,0 +1,18 @@
1
+package kr.co.swh.lecture.opensource.project.sparkjava;
2
+
3
+import java.util.ArrayList;
4
+import java.util.List;
5
+
6
+public class ListObjectRemoveExample {
7
+	
8
+	public static void main(String[] args) {
9
+		List<WordCloud> list = new ArrayList<WordCloud>();
10
+		WordCloud w1 = new WordCloud("코딩", 1);
11
+		list.add(w1);
12
+		WordCloud w2 = new WordCloud("코딩", 2);
13
+		list.remove(w2);
14
+		list.add(w2);
15
+		System.out.println(list.size());
16
+		System.out.println(list.get(0).getSize());
17
+	}
18
+}

+ 1
- 1
src/main/java/kr/co/swh/lecture/opensource/project/sparkjava/Step1.java Прегледај датотеку

@@ -59,7 +59,7 @@ public class Step1 {
59 59
 				jArr.add(i);		// json문자열로 넣지 말고, 객체형태로 추가
60 60
 			}
61 61
 			attributes.put("message", jArr);
62
-			return modelAndView(attributes, "news.ftl");
62
+			return modelAndView(attributes, "news-table.ftl");
63 63
 		}, new FreeMarkerTemplateEngine());
64 64
 	}
65 65
 	public static void main(String[] args) {

+ 90
- 0
src/main/java/kr/co/swh/lecture/opensource/project/sparkjava/Step2.java Прегледај датотеку

@@ -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
+}

+ 37
- 0
src/main/java/kr/co/swh/lecture/opensource/project/sparkjava/WordCloud.java Прегледај датотеку

@@ -0,0 +1,37 @@
1
+package kr.co.swh.lecture.opensource.project.sparkjava;
2
+
3
+public class WordCloud {
4
+	private String text;
5
+	private Integer size;
6
+	
7
+	public WordCloud(String text, Integer size) {
8
+		this.text = text;
9
+		this.size = size;
10
+	}
11
+
12
+	public String getText() {
13
+		return text;
14
+	}
15
+
16
+	public void setText(String text) {
17
+		this.text = text;
18
+	}
19
+
20
+	public Integer getSize() {
21
+		return size;
22
+	}
23
+
24
+	public void setSize(Integer size) {
25
+		this.size = size;
26
+	}
27
+	
28
+	@Override
29
+	public boolean equals(Object obj) {
30
+		// TODO Auto-generated method stub
31
+		if(obj instanceof WordCloud) {
32
+			WordCloud u = (WordCloud)obj;
33
+			if(this.text.equals(u.getText())) return true;
34
+		}
35
+		return false;
36
+	}
37
+}

+ 84
- 0
src/main/resources/freemarker/news-cloud.ftl Прегледај датотеку

@@ -0,0 +1,84 @@
1
+<html>
2
+	<!-- Body tag is where we will append our SVG and SVG objects-->
3
+	<body>
4
+	</body>
5
+    <head>
6
+        <title>블로그 검색어 Word Cloud</title>
7
+        <script src="https://d3js.org/d3.v3.min.js"></script>
8
+		<script src="https://rawgit.com/jasondavies/d3-cloud/master/build/d3.layout.cloud.js" type="text/JavaScript"></script>
9
+		<script type="text/JavaScript">
10
+			var datasToDraw = new Array();
11
+			<#list message as i>
12
+				var person = {}; //또는 var person = new Object();
13
+				person.text = '${i.text}';
14
+				person.size = ${i.size} * 5;
15
+	        	datasToDraw.push(person);
16
+	        </#list>
17
+
18
+			// Next you need to use the layout script to calculate the placement, rotation and size of each word:
19
+
20
+			var width = 960;
21
+			var height = 500;
22
+			var fill = d3.scale.category20();
23
+			
24
+			var svg = d3.select("body").append("svg")
25
+			  .attr("width", width)
26
+			  .attr("height", height);
27
+			var svg = d3.select("svg")
28
+			  .append("g")
29
+			  .attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
30
+			var keywords = ["JavaScript", "Actionscript", "coffeescript"]
31
+			
32
+			function showCloud(words) {
33
+			  d3.layout.cloud()
34
+			    .size([width, height])
35
+			    .words(words)
36
+			    .rotate(function() {
37
+			      return ~~(Math.random() * 2) * 90;
38
+			    })
39
+			    .font("Impact")
40
+			    .fontSize(function(d) {
41
+			      return d.size;
42
+			    })
43
+			    .on("end", drawSkillCloud)
44
+			    .start();
45
+			}
46
+			showCloud(datasToDraw);
47
+			
48
+			// Finally implement `drawSkillCloud`, which performs the D3 drawing:
49
+			
50
+			// apply D3.js drawing API
51
+			function drawSkillCloud(words) {
52
+			  var cloud = svg.selectAll("text").data(words)
53
+			  //Entering words
54
+			  cloud.enter()
55
+			    .append("text")
56
+			    .style("font-family", "overwatch")
57
+			    .style("fill", function(d) {
58
+			      return (keywords.indexOf(d.text) > -1 ? "#fbc280" : "#405275");
59
+			    })
60
+			    .style("fill-opacity", .5)
61
+			    .attr("text-anchor", "middle")
62
+			    .attr('font-size', 1)
63
+			    .text(function(d) {
64
+			      return d.text;
65
+			    });
66
+			  cloud
67
+			    .transition()
68
+			    .duration(600)
69
+			    .style("font-size", function(d) {
70
+			      return d.size + "px";
71
+			    })
72
+			    .attr("transform", function(d) {
73
+			      return "translate(" + [d.x, d.y] + ")rotate(" + d.rotate + ")";
74
+			    })
75
+			    .style("fill-opacity", 1);
76
+			}
77
+			
78
+			// set the viewbox to content bounding box (zooming in on the content, effectively trimming whitespace)
79
+			setInterval(function() {
80
+			  showCloud(datasToDraw);
81
+			}, 2000)
82
+		</script>
83
+    </head>
84
+</html>

src/main/resources/freemarker/news.ftl → src/main/resources/freemarker/news-table.ftl Прегледај датотеку