Skip to content

Commit c8fec38

Browse files
committed
连接数据库
1 parent 3a5cf52 commit c8fec38

17 files changed

+769409
-0
lines changed

IDEA开源社.txt

+572,857
Large diffs are not rendered by default.

news.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
原标题:形势严峻!这个地方书记市长纪委书记为何连续空降?
2+
市委书记、市长、市委副书记接连落马的广东江门市,政治生态修复从补齐关键岗位开始。
3+
在连续迎来空降市委书记、市长候选人后,江门新一任纪委书记近日也到岗了。值得关注的是,他也是从省里空降的,还是纪检部门。
4+
这位新纪委书记叫项天保,任职省纪委26年,在案例管理、派驻机构、巡视部门等关键岗位都工作过,经验十分丰富。
5+
去年底,江门成立市委巡察工作机构,时任省委巡视办副主任的项天保亲赴江门参加了启动仪式。
6+
长安街知事APP此前曾介绍过,江门是腐败的重灾区,市委书记毛荣楷,市长邓伟根,市委副书记、政法委书记邹家军,市委常委王积俊,市人大常委会副主任聂党权(曾任市委副书记)落马,班子塌方全国罕见。
7+
“中央派来了一个沙瑞金(省委书记),又派来了一个田国富(纪委书记)”,这是《人民的名义》里的一个情节,以此说明推动从严治党的迫切性。
8+
江门的情况与此类似,市委书记林应武、市长候选人刘毅都是从省委组织部副部长任上调来江门的,补位落马前任。如今新纪委书记又从省级纪检部门调来,从一个侧面也反映出地方反腐形势的严峻性。
9+
就在项天保就任的会议上,前任纪委书记胡钛也以新身份亮相,他已经出任市委副书记、政法委书记。也就是说,现在江门市委常委班子中,有两名来自纪检系统的领导。
10+
胡钛是军转干部,2016年底刚刚调任江门市纪委书记。他有两次“救火”经历,一次是梅州,一次是江门。
11+
2014年,梅州市委书记朱泽君和纪委书记李纯德相继被调离,此后又相继被查,媒体对两人“内斗”多有报道。胡钛正是接替了李的梅州纪委书记职务。
12+
而去年赴江门履新,正是该市市委书记毛荣楷和市委副书记邹家军落马之后。
13+
胡钛之前的江门市纪委书记周伟万,也是一名老纪检,在纪检、政法战线工作了30年,今年初当选市政协主席。
14+
面对从严治党的新形势和班子塌方的旧局面,接力反腐,任重道远。
15+
近日召开的江门全市领导干部大会上,广东省委常委、组织部长邹铭根据省委书记胡春华同志的指示,对全市领导干部提出三点要求,其中特别指出——
16+
要进一步严明政治纪律和政治规矩,营造良好的政治生态。要保持干部队伍思想稳定和改革发展大局稳定,积极引导广大干部群众把违纪违法的个人问题与江门整体工作区分开来,不因人废事,不因案划线,不因此否定江门的工作,影响江门的发展稳定。
17+
营造良好的政治生态,更好地推动发展,无疑是江门工作当下的重中之重。
18+
来源:长安街知事
19+
责任编辑:初晓慧
20+
文章关键词: 纪委书记 市长 纪检
21+
我要反馈 保存网页

news_5_18.txt

+2,060
Large diffs are not rendered by default.

news_NewWordFound/FoundNewWords.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# 基于切分的新词发现是对 新词发现的信息熵方法的改进,降低计算量
2+
import pymongo # PyMongo是MongoDB的Python接口开发包。
3+
4+
db = pymongo.MongoClient().baike.items
5+
def texts():
6+
for a in db.find(no_cursor_timeout=True).limit(1000000):
7+
yield a['content']
8+
9+
from collections import defaultdict
10+
from itertools import tee
11+
from tqdm import tqdm
12+
import re
13+
'''Tqdm 是一个快速,可扩展的Python进度条,可以在 Python 长循环中添加一个进度提示信息,用户只需要封装任意的迭代器 tqdm(iterator)。'''
14+
15+
class Find_Words:
16+
def __init__(self, min_count=10, min_proba=1):
17+
self.min_count = min_count
18+
self.min_proba = min_proba
19+
self.chars, self.pairs = defaultdict(int), defaultdict(int)
20+
self.total = 0.
21+
def text_filter(self, texts):
22+
for a in tqdm(texts):
23+
for t in re.split(u'[^\u4e00-\u9fa50-9a-zA-Z]+', a):
24+
if t:
25+
yield t
26+
def count(self, texts):
27+
for text in self.text_filter(texts):
28+
self.chars[text[0]] += 1
29+
for i in range(len(text)-1):
30+
self.chars[text[i+1]] += 1
31+
self.pairs[text[i:i+2]] += 1
32+
self.total += 1
33+
self.chars = {i:j for i,j in self.chars.iteritems() if j >= self.min_count}
34+
self.pairs = {i:j for i,j in self.pairs.iteritems() if j >= self.min_count}
35+
self.strong_segments = {i: self.total*j/(self.chars[i[0]]*self.chars[i[1]]) for i,j in self.pairs.iteritems()}
36+
self.strong_segments = {i:j for i,j in self.strong_segments.iteritems() if j >= self.min_proba}
37+
def find_words(self, texts):
38+
self.words = defaultdict(int)
39+
self.total_words = 0.
40+
for text in self.text_filter(texts):
41+
s = text[0]
42+
for i in range(len(text)-1):
43+
if text[i:i+2] in self.strong_segments:
44+
s += text[i+1]
45+
else:
46+
self.words[s] += 1
47+
self.total_words += 1
48+
s = text[i+1]
49+
self.words = {i:j for i,j in self.words.iteritems() if j >= self.min_count}
50+
51+
fw = Find_Words(16, 1)
52+
fw.count(texts())
53+
fw.find_words(texts())
54+
55+
import pandas as pd
56+
words = pd.Series(fw.words).sort_values(ascending=False)

news_NewWordFound/FoundNewWords_2.py

+89
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# 新词发现的信息熵方法与实现
2+
import numpy as np
3+
import pandas as pd
4+
import re
5+
from numpy import log, min
6+
import pymysql
7+
8+
# 连接数据库
9+
conn = pymysql.connect(host='127.0.0.1',port = 3306,user = 'root',
10+
passwd = '123456',db = 'news_5_23')
11+
c = conn.cursor()
12+
13+
# 查询多条数据fetchAll(文件存在之后就不需要重复创建)
14+
# c.execute("select content from event_news_ref into outfile '/var/lib/mysql-files/event_news_ref.txt'; ")
15+
# r = c.fetchone()
16+
17+
f = open('/home/yanchao/PyCharmProject/TextSummary/event_news_ref.txt', 'r') # 读取文章
18+
s = f.read() # 读取为一个字符串
19+
20+
# 定义要去掉的标点字或者字段
21+
drop_dict = [u',', u'\n', u'。', u'、', u':', u'(', u')', u'[', u']', u'.', u',', u' ', u'\u3000', u'”', u'“', u'?', u'?',
22+
u'!', u'‘', u'’',u'(',u')',u'《',u'》', u'(',u')',u'…',u'-',u'0',u'1',u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9',
23+
u':',u'q',u'w',u'e',u'r',u't',u'y',u'u',u'i',u'o',u'u',u'p',u'a',u's',u'd',u'f',u'g',u'h',u'j',u'k',u'l',u'z',
24+
u'x',u'c',u'v',u'b',u'n',u'm',u'<',u'>',u'@',u'!',u'#',u'$',u'%',u'^',u'&',u'*',u'/',u'?',u'~',u'Q',u'W',u'E',
25+
u'R',u'T',u'Y',u'U',u'I',u'O',u'P',u'A',u'S',u'D',u'F',u'G','H',u'J',u'K',u'L',u'Z',u'X',u'C',u'V',u'B',u'N',u'M',
26+
u'【',u'】',u'|',u'à',u'╰',u'{',u'=',u';',u',',u'﹌﹌﹌']
27+
for i in drop_dict: # 去掉标点字或者字段
28+
s = s.replace(i, '')
29+
30+
# 为了方便调用,自定义了一个正则表达式的词典
31+
myre = {2: '(..)', 3: '(...)', 4: '(....)', 5: '(.....)', 6: '(......)', 7: '(.......)'}
32+
33+
min_count = 10 # 录取词语最小出现次数
34+
min_support = 30 # 录取词语最低支持度,1代表着随机组合
35+
min_s = 3 # 录取词语最低信息熵,越大说明越有可能独立成词
36+
max_sep = 4 # 候选词语的最大字数
37+
t = [] # 保存结果用。
38+
39+
t.append(pd.Series(list(s)).value_counts()) # 逐字统计
40+
tsum = t[0].sum() # 统计总字数
41+
rt = [] # 保存结果用
42+
43+
for m in range(2, max_sep + 1):
44+
print(u'正在生成%s字词...' % m)
45+
t.append([])
46+
for i in range(m): # 生成所有可能的m字词
47+
t[m - 1] = t[m - 1] + re.findall(myre[m], s[i:])
48+
49+
t[m - 1] = pd.Series(t[m - 1]).value_counts() # 逐词统计
50+
t[m - 1] = t[m - 1][t[m - 1] > min_count] # 最小次数筛选
51+
tt = t[m - 1][:]
52+
for k in range(m - 1):
53+
qq = np.array(list(map(lambda ms: tsum * t[m - 1][ms] / t[m - 2 - k][ms[:m - 1 - k]] / t[k][ms[m - 1 - k:]],
54+
tt.index))) > min_support # 最小支持度筛选。
55+
tt = tt[qq]
56+
rt.append(tt.index)
57+
58+
59+
def cal_S(sl): # 信息熵计算函数
60+
return -((sl / sl.sum()).apply(log) * sl / sl.sum()).sum()
61+
62+
63+
for i in range(2, max_sep + 1):
64+
print(u'正在进行%s字词的最大熵筛选(%s)...' % (i, len(rt[i - 2])))
65+
pp = [] # 保存所有的左右邻结果
66+
for j in range(i):
67+
pp = pp + re.findall('(.)%s(.)' % myre[i], s[j:])
68+
pp = pd.DataFrame(pp).set_index(1).sort_index() # 先排序,这个很重要,可以加快检索速度
69+
index = np.sort(np.intersect1d(rt[i - 2], pp.index)) # 作交集
70+
# 下面两句分别是左邻和右邻信息熵筛选
71+
index = index[np.array(list(map(lambda s: cal_S(pd.Series(pp[0][s]).value_counts()), index))) > min_s]
72+
rt[i - 2] = index[np.array(list(map(lambda s: cal_S(pd.Series(pp[2][s]).value_counts()), index))) > min_s]
73+
74+
# # 下面都是输出前处理
75+
# for i in range(len(rt)):
76+
# t[i + 1] = t[i + 1][rt[i]]
77+
# t[i + 1].sort(ascending=False)
78+
79+
# 保存结果并输出
80+
pd.DataFrame(pd.concat(t[1:])).to_csv('result.txt', header=False)
81+
82+
# 性能分析模块:
83+
# python -m cProfile -o FoundNewWords_2.out FoundNewWords_2.
84+
# 随机排序:
85+
# python -m cProfile FoundNewWords.py
86+
# 按耗时排序:
87+
# python -c "import pstats; p=pstats.Stats('FoundNewWords_2.out'); p.sort_stats('time').print_stats()"
88+
89+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 新词发现的信息熵方法与实现
2+
import numpy as np
3+
import pandas as pd
4+
import re
5+
from numpy import log, min
6+
7+
f = open('机器学习.txt', 'r') # 读取文章
8+
s = f.read() # 读取为一个字符串
9+
10+
# 定义要去掉的标点字或者字段
11+
drop_dict = [u',', u'\n', u'。', u'、', u':', u'(', u')', u'[', u']', u'.', u',', u' ', u'\u3000', u'”', u'“', u'?', u'?',
12+
u'!', u'‘', u'’',u'(',u')',u'《',u'》', u'(',u')',u'…',u'-',u'0',u'1',u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9',
13+
u':',u'q',u'w',u'e',u'r',u't',u'y',u'u',u'i',u'o',u'u',u'p',u'a',u's',u'd',u'f',u'g',u'h',u'j',u'k',u'l',u'z',
14+
u'x',u'c',u'v',u'b',u'n',u'm',u'<',u'>',u'@',u'!',u'#',u'$',u'%',u'^',u'&',u'*',u'/',u'?',u'~',u'Q',u'W',u'E',
15+
u'R',u'T',u'Y',u'U',u'I',u'O',u'P',u'A',u'S',u'D',u'F',u'G','H',u'J',u'K',u'L',u'Z',u'X',u'C',u'V',u'B',u'N',u'M',
16+
u'【',u'】',u'|',u'à',u'╰',u'{',u'=',u';',u',',u'﹌﹌﹌']
17+
for i in drop_dict: # 去掉标点字或者字段
18+
s = s.replace(i, '')
19+
20+
# 为了方便调用,自定义了一个正则表达式的词典
21+
myre = {2: '(..)', 3: '(...)', 4: '(....)', 5: '(.....)', 6: '(......)', 7: '(.......)'}
22+
23+
min_count = 10 # 录取词语最小出现次数
24+
min_support = 30 # 录取词语最低支持度,1代表着随机组合
25+
min_s = 3 # 录取词语最低信息熵,越大说明越有可能独立成词
26+
max_sep = 4 # 候选词语的最大字数
27+
t = [] # 保存结果用。
28+
29+
t.append(pd.Series(list(s)).value_counts()) # 逐字统计
30+
tsum = t[0].sum() # 统计总字数
31+
rt = [] # 保存结果用
32+
33+
for m in range(2, max_sep + 1):
34+
print(u'正在生成%s字词...' % m)
35+
t.append([])
36+
for i in range(m): # 生成所有可能的m字词
37+
t[m - 1] = t[m - 1] + re.findall(myre[m], s[i:])
38+
39+
t[m - 1] = pd.Series(t[m - 1]).value_counts() # 逐词统计
40+
t[m - 1] = t[m - 1][t[m - 1] > min_count] # 最小次数筛选
41+
tt = t[m - 1][:]
42+
for k in range(m - 1):
43+
qq = np.array(list(map(lambda ms: tsum * t[m - 1][ms] / t[m - 2 - k][ms[:m - 1 - k]] / t[k][ms[m - 1 - k:]],
44+
tt.index))) > min_support # 最小支持度筛选。
45+
tt = tt[qq]
46+
rt.append(tt.index)
47+
48+
49+
def cal_S(sl): # 信息熵计算函数
50+
return -((sl / sl.sum()).apply(log) * sl / sl.sum()).sum()
51+
52+
53+
for i in range(2, max_sep + 1):
54+
print(u'正在进行%s字词的最大熵筛选(%s)...' % (i, len(rt[i - 2])))
55+
pp = [] # 保存所有的左右邻结果
56+
for j in range(i):
57+
pp = pp + re.findall('(.)%s(.)' % myre[i], s[j:])
58+
pp = pd.DataFrame(pp).set_index(1).sort_index() # 先排序,这个很重要,可以加快检索速度
59+
index = np.sort(np.intersect1d(rt[i - 2], pp.index)) # 作交集
60+
# 下面两句分别是左邻和右邻信息熵筛选
61+
index = index[np.array(list(map(lambda s: cal_S(pd.Series(pp[0][s]).value_counts()), index))) > min_s]
62+
rt[i - 2] = index[np.array(list(map(lambda s: cal_S(pd.Series(pp[2][s]).value_counts()), index))) > min_s]
63+
64+
# # 下面都是输出前处理
65+
# for i in range(len(rt)):
66+
# t[i + 1] = t[i + 1][rt[i]]
67+
# t[i + 1].sort(ascending=False)
68+
69+
# 保存结果并输出
70+
pd.DataFrame(pd.concat(t[1:])).to_csv('result.txt', header=False)
71+
72+
# 性能分析模块:
73+
# python -m cProfile -o FoundNewWords_2.out FoundNewWords_2.
74+
# 随机排序:
75+
# python -m cProfile FoundNewWords.py
76+
# 按耗时排序:
77+
# python -c "import pstats; p=pstats.Stats('FoundNewWords_2.out'); p.sort_stats('time').print_stats()"
78+
79+
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pymysql
2+
3+
# 连接数据库
4+
conn = pymysql.connect(host='127.0.0.1',port = 3306,user = 'root',
5+
passwd = '123456',db = 'news_5_23')
6+
c = conn.cursor()
7+
8+
# 查询多条数据fetchAll
9+
c.execute("select content from event_news_ref into outfile '/var/lib/mysql-files/event_news_ref.txt'; ")
10+
r = c.fetchone()
11+
print(r)
12+
# f = open('event_news_ref.txt', 'w')
13+
print("write to .txt file!")

news_NewWordFound/event_news_ref.txt

Whitespace-only changes.

news_NewWordFound/python_mysql.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# #-------------------python2.7连接MySQL数据库------------------------
2+
#
3+
# #!/usr/bin/env python
4+
#
5+
# import time
6+
# import MySQLdb # 不支持Python3
7+
#
8+
# #连接
9+
# conn = MySQLdb.connect(host="localhost",user="root",passwd="root",db="test",charset="utf8")
10+
# cursor = conn.cursor()
11+
#
12+
# #删除表
13+
# sql = "drop table if exists user"
14+
# cursor.execute(sql)
15+
#
16+
# #创建
17+
# sql = "create table if not exists user(name varchar(128) primary key,created int(10))"
18+
# cursor.execute(sql)
19+
#
20+
# #写入
21+
# sql = "insert into user(name,created) values(%s,%s)"
22+
# param = ("aaa",int(time.time()))
23+
# n = cursor.execute(sql,param)
24+
# print ('insert',n)
25+
#
26+
# #写入多行
27+
# sql = "insert into user(name,created) values(%s,%s)"
28+
# param = (("bbb",int(time.time())),("ccc",33),("ddd",44))
29+
# print ('insertmany',n)
30+
#
31+
# #更新
32+
# sql = "update user set name=%s where name='aaa'"
33+
# param = ("zzz")
34+
# n = cursor.execute(sql,param)
35+
# print ('update',n)
36+
#
37+
# #查询
38+
# n = cursor.execute("select * from user")
39+
# for row in cursor.fetchall():
40+
# print (row)
41+
# for r in row:
42+
# print (r)
43+
#
44+
# #删除 注意:MYSQL的占位符是s%
45+
# sql = "delete from user where name=%s"
46+
# param = ("bbb")
47+
# n = cursor.execute(sql,param)
48+
# print ('delete',n)
49+
#
50+
# #查询
51+
# n = cursor.execute("select * from user")
52+
# print (cursor.fetchall())
53+
#
54+
# cursor.close()
55+
#
56+
# #提交
57+
# coon.commit()
58+
# #关闭
59+
# conn.close()
60+
#
61+
# # -------------------------------------------------------------
62+
# # 以mysql或者sqlite为例,请用代码给出简洁且完整的数据库操作示例。注:请参考视频中的代码。
63+
# # -------------------------------------------------------------
64+
# # [参考代码:]
65+
#
66+
# # 导入MySQL驱动:
67+
# import mysql
68+
# from mysql import connector
69+
#
70+
# # import mysql.connector
71+
# # 注意把password设为你的root口令:
72+
# conn = mysql.connector.connect(user='root', password='password', database='test', use_unicode=True)
73+
# cursor = conn.cursor()
74+
# # 创建user表:
75+
# cursor.execute('create table user (id varchar(20) primary key, name varchar(20))')
76+
# # 插入一行记录,注意MySQL的占位符是%s:
77+
# cursor.execute('insert into user (id, name) values (%s, %s)', ['1', 'Michael'])
78+
#
79+
# # 提交事务:
80+
# conn.commit()
81+
# cursor.close()
82+
# # 运行查询:
83+
# cursor = conn.cursor()
84+
# cursor.execute('select * from user where id = %s', '1')
85+
# values = cursor.fetchall()
86+
# print (values)
87+
#
88+
# # 关闭Cursor和Connection:
89+
# cursor.close()
90+
# conn.close()

0 commit comments

Comments
 (0)