update code
This commit is contained in:
Binary file not shown.
|
After Width: | Height: | Size: 208 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 98 KiB |
@@ -2,7 +2,7 @@
|
|||||||
"""
|
"""
|
||||||
Created on Wed Dec 23 14:06:10 2015
|
Created on Wed Dec 23 14:06:10 2015
|
||||||
|
|
||||||
@author: czl
|
@author: bitjoy.net
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from os import listdir
|
from os import listdir
|
||||||
@@ -13,7 +13,6 @@ import sqlite3
|
|||||||
import configparser
|
import configparser
|
||||||
from datetime import *
|
from datetime import *
|
||||||
import math
|
import math
|
||||||
import re
|
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -148,45 +147,16 @@ class RecommendationModule:
|
|||||||
idf_file.write('%s %.9f\n'%(word, math.log(n / df)))
|
idf_file.write('%s %.9f\n'%(word, math.log(n / df)))
|
||||||
idf_file.close()
|
idf_file.close()
|
||||||
|
|
||||||
def natural_sort(self, l):
|
def find_k_nearest(self, k, topK):
|
||||||
convert = lambda text: int(text) if text.isdigit() else text.lower()
|
|
||||||
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
|
|
||||||
return sorted(l, key = alphanum_key)
|
|
||||||
|
|
||||||
def partition_files(self, n_partitions):
|
|
||||||
files = listdir(self.doc_dir_path)
|
|
||||||
files = self.natural_sort(files)
|
|
||||||
n = float(len(files))
|
|
||||||
partitions = []
|
|
||||||
sz = int(n / n_partitions)
|
|
||||||
x = 0
|
|
||||||
for i in range(n_partitions - 1):
|
|
||||||
partitions.append(files[x: x + sz])
|
|
||||||
x += sz
|
|
||||||
partitions.append(files[x:])
|
|
||||||
return partitions
|
|
||||||
|
|
||||||
def find_k_nearest(self, k, topK, n_partitions):
|
|
||||||
|
|
||||||
print('-----start gen_idf_file time: %s-----'%(datetime.today()))
|
|
||||||
self.gen_idf_file()
|
self.gen_idf_file()
|
||||||
|
files = listdir(self.doc_dir_path)
|
||||||
partitions = self.partition_files(n_partitions)
|
dt_matrix = self.construct_dt_matrix(files, topK)
|
||||||
i = 1
|
|
||||||
for p in partitions:
|
|
||||||
print('-----start %d construct_dt_matrix time: %s-----'%(i, datetime.today()))
|
|
||||||
dt_matrix = self.construct_dt_matrix(p, topK)
|
|
||||||
|
|
||||||
print('-----start %d construct_k_nearest_matrix time: %s-----'%(i, datetime.today()))
|
|
||||||
self.construct_k_nearest_matrix(dt_matrix, k)
|
self.construct_k_nearest_matrix(dt_matrix, k)
|
||||||
i += 1
|
|
||||||
|
|
||||||
print('-----start write_k_nearest_matrix_to_db time: %s-----'%(datetime.today()))
|
|
||||||
self.write_k_nearest_matrix_to_db()
|
self.write_k_nearest_matrix_to_db()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
print('-----start time: %s-----'%(datetime.today()))
|
print('-----start time: %s-----'%(datetime.today()))
|
||||||
rm = RecommendationModule('../config.ini', 'utf-8')
|
rm = RecommendationModule('../config.ini', 'utf-8')
|
||||||
rm.find_k_nearest(5, 25, 10)
|
rm.find_k_nearest(5, 25)
|
||||||
print('-----finish time: %s-----'%(datetime.today()))
|
print('-----finish time: %s-----'%(datetime.today()))
|
||||||
|
|
||||||
+38
-8
@@ -2,18 +2,48 @@
|
|||||||
"""
|
"""
|
||||||
Created on Fri Dec 25 00:04:40 2015
|
Created on Fri Dec 25 00:04:40 2015
|
||||||
|
|
||||||
@author: czl
|
@author: bitjoy.net
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from spider import get_news_pool
|
||||||
|
from spider import crawl_news
|
||||||
from index_module import IndexModule
|
from index_module import IndexModule
|
||||||
from recommendation_module import RecommendationModule
|
from recommendation_module import RecommendationModule
|
||||||
from datetime import *
|
from datetime import *
|
||||||
|
import urllib.request
|
||||||
|
import configparser
|
||||||
|
|
||||||
|
def get_max_page(root):
|
||||||
|
response = urllib.request.urlopen(root)
|
||||||
|
html = str(response.read())
|
||||||
|
html = html[html.find('var maxPage =') : ]
|
||||||
|
html = html[:html.find(';')]
|
||||||
|
max_page = int(html[html.find('=') + 1 : ])
|
||||||
|
#print(max_page)
|
||||||
|
return(max_page)
|
||||||
|
|
||||||
print('-----start indexing time: %s-----'%(datetime.today()))
|
def crawling():
|
||||||
im = IndexModule('../config.ini', 'utf-8')
|
print('-----start crawling time: %s-----'%(datetime.today()))
|
||||||
im.construct_postings_lists()
|
config = configparser.ConfigParser()
|
||||||
print('-----start recommending time: %s-----'%(datetime.today()))
|
config.read('../config.ini', 'utf-8')
|
||||||
rm = RecommendationModule('../config.ini', 'utf-8')
|
root = 'http://news.sohu.com/1/0903/61/subject212846158'
|
||||||
rm.find_k_nearest(5, 25, 10)
|
max_page = get_max_page(root+'.shtml')
|
||||||
print('-----finish time: %s-----'%(datetime.today()))
|
news_pool = get_news_pool(root, max_page, max_page - 5)
|
||||||
|
crawl_news(news_pool, 140, config['DEFAULT']['doc_dir_path'], config['DEFAULT']['doc_encoding'])
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print('-----start time: %s-----'%(datetime.today()))
|
||||||
|
|
||||||
|
#抓取新闻数据
|
||||||
|
crawling()
|
||||||
|
|
||||||
|
#构建索引
|
||||||
|
print('-----start indexing time: %s-----'%(datetime.today()))
|
||||||
|
im = IndexModule('../config.ini', 'utf-8')
|
||||||
|
im.construct_postings_lists()
|
||||||
|
|
||||||
|
#推荐阅读
|
||||||
|
print('-----start recommending time: %s-----'%(datetime.today()))
|
||||||
|
rm = RecommendationModule('../config.ini', 'utf-8')
|
||||||
|
rm.find_k_nearest(5, 25)
|
||||||
|
print('-----finish time: %s-----'%(datetime.today()))
|
||||||
+43698
File diff suppressed because it is too large
Load Diff
BIN
Binary file not shown.
@@ -164,5 +164,5 @@ class SearchEngine:
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
se = SearchEngine('../config.ini', 'utf-8')
|
se = SearchEngine('../config.ini', 'utf-8')
|
||||||
flag, rs = se.search('朝鲜氢弹试验', 0)
|
flag, rs = se.search('北京雾霾', 0)
|
||||||
print(rs[:10])
|
print(rs[:10])
|
||||||
@@ -64,7 +64,7 @@
|
|||||||
|
|
||||||
<div id="footer">
|
<div id="footer">
|
||||||
<hr>
|
<hr>
|
||||||
© 2015 UCAS 陈镇霖 肖俊斌 罗纯龙 方文征
|
© 2016 <a href="http://bitjoy.net/" target="_blank">bitjoy.net</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user