tobby48 5 年 前
コミット
3e6d61114d

+ 66
- 0
src/main/python/kr/co/swh/lecture/opensource/project/naver-blog-nlp-ranking-d3.py ファイルの表示

@@ -0,0 +1,66 @@
1
+from konlpy.tag import Kkma
2
+import os
3
+import sys
4
+import urllib.request
5
+import json
6
+import re
7
+
8
+def naver_blog_search(client_id, client_secret, text):
9
+    encText = urllib.parse.quote(text)
10
+    url = "https://openapi.naver.com/v1/search/blog?query=" + encText # json 결과
11
+    # url = "https://openapi.naver.com/v1/search/blog.xml?query=" + encText # xml 결과
12
+    request = urllib.request.Request(url)
13
+    request.add_header("X-Naver-Client-Id",client_id)
14
+    request.add_header("X-Naver-Client-Secret",client_secret)
15
+    response = urllib.request.urlopen(request)
16
+    rescode = response.getcode()
17
+    if(rescode==200):
18
+        response_body = response.read()
19
+        return response_body.decode('utf-8')
20
+    else:
21
+        print("Error Code:" + rescode)
22
+    
23
+def striphtml(data):
24
+    p = re.compile('<.*?>')
25
+    return p.sub('', data)
26
+
27
+def nlp_process():
28
+    result = []
29
+    kkma = Kkma()
30
+    naver_result = naver_blog_search('OJgN42xxZiJXpnZtCH1j', 'crXggqJhhW', '코딩')
31
+    y = json.loads(naver_result)
32
+    for b in y['items']:
33
+        for word, pos in kkma.pos(striphtml(b['description'])):
34
+            if pos == 'NNG' or pos == 'NNP':
35
+                result.append(word)
36
+                
37
+    words = set(result)
38
+    loofWords = list(words)
39
+    countList = []
40
+    for b in loofWords:
41
+        if result.count(b) > 1:
42
+            dic = dict()
43
+            dic["text"] = b
44
+            dic["size"] = result.count(b)
45
+            countList.append(dic)
46
+    return countList
47
+
48
+
49
+
50
+from flask import Flask, jsonify, render_template
51
+import requests
52
+from bs4 import BeautifulSoup
53
+
54
+app = Flask(__name__)
55
+app.config['JSON_AS_ASCII'] = False
56
+countList = ""
57
+
58
+@app.route("/")
59
+def view():
60
+    return render_template("naver-blog-nlp-ranking-d3.html", value=countList)
61
+
62
+if __name__ == "__main__":
63
+    countResultList = nlp_process()
64
+    print(str(countResultList).replace("'", ""))
65
+    countList = str(countResultList).replace("'", "")
66
+    app.run(host="127.0.0.1", port="8080")

+ 1
- 1
src/main/python/kr/co/swh/lecture/opensource/project/naver-blog-nlp-ranking.py ファイルの表示

@@ -21,7 +21,7 @@ def naver_blog_search(client_id, client_secret, text):
21 21
         print("Error Code:" + rescode)
22 22
     
23 23
 def striphtml(data):
24
-    p = re.compile(r'<.*?>')
24
+    p = re.compile('<.*?>')
25 25
     return p.sub('', data)
26 26
 
27 27
 result = []

+ 75
- 0
src/main/python/kr/co/swh/lecture/opensource/project/templates/naver-blog-nlp-ranking-d3.html ファイルの表示

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