2 Star 0 Fork 0

lawForNlp/quliang

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
get_mogodb_keys 5.90 KB
一键复制 编辑 原始数据 按行查看 历史
kyroad 提交于 2019-04-26 09:40 . get mogodb py
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 7 17:19:35 2019
@author: quliang
"""
import pymysql
from pymongo import MongoClient
import logging
import re
import json
def get_text(s):
pattern=re.compile(r'\'text\': \'(.*?)\'')
res_list = re.findall(pattern,str(s))
res_str = ' '.join(res_list)
return res_str
def get_court_consider(s):
pattern=re.compile(r'\'text\': \"(.*?)\"')
res_list = re.findall(pattern,str(s))
res_str = ' '.join(res_list)
return res_str
def data_to_mysql(data,keys,test=False):
db_data='test' if test else 'renren_data'
db = pymysql.connect(host='193.112.56.204', user='renrendata', passwd='data@2018',
db=db_data, port=3306, charset='utf8')
cursor = db.cursor()
cols = ", ".join('`{}`'.format(k) for k in keys)
val_cols = ', '.join('%({})s'.format(k) for k in keys)
sql = "insert into judgement_doc(%s) values(%s)"
res_sql = sql % (cols, val_cols)
#print(res_sql)
cursor.executemany(res_sql,data)
db.commit()
logger = logging.getLogger()
logging.basicConfig(format='%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s')
logger.setLevel(logging.ERROR)
client = MongoClient('mongodb://admin:admin@193.112.23.12:27017')
db=client.law_nlp
#collist = db.list_collection_names()
collist=['zhejiang', 'fujian', 'hebei', 'neimenggu',
'hainan', 'hubei','heilongjiang', 'jiangsu', 'anhui','guizhou','liaoning',
'qinghai','gansu','chongqing','henan','hunan', 'guangxi']
#collist1=['zhejiang']
keys=['province', 'save_id', 'judgementId', 'judgementType', 'reason_name',
'title', 'regulation', 'trial_procedure', 'original_told', 'was_told',
'court_get', 'court_consider', 'court_result','frist_court_get','appellor_told',
'by_appellor_told','frist_court_consider','second_court_result']
for col in collist:
w=[]
collection=db[col]
print('finding data ing')
result = collection.find({"content.judgementType":"判决","content.title":{'$regex':'婚'}})
if not result:
continue
print('{} data collected finished'.format(col))
result = list(result)
lens=len(list(result))
print('{}有{}条数据'.format(col,lens))
for inx,res in enumerate(result):
logger.info('res detail',res)
panjue_data={k:'' for k in keys}
panjue_data['province']=col
panjue_data['save_id']=inx
panjue_data['judgementType']=res['content']['judgementType']
panjue_data['title']=res['content']['title']
if res.get('judgementId'):
panjue_data['judgementId']=res['judgementId']
if res['content'].get('reason'):
if res['content']['reason'].get('name'):
panjue_data['reason_name']=res['content']['reason']['name']
#解析法条
if res['content'].get('regulationGroupByTrialRoundInfos'):
for trialRoundText in res['content']['regulationGroupByTrialRoundInfos']:
#一审 , 二审 分隔符&&
if trialRoundText.get('trialRoundText'):
panjue_data['regulation']+='&'+trialRoundText['trialRoundText']+'&'+'\n'
for sectionParagraphs in trialRoundText['regulations']:
logger.info('sectionParagraphs detail',sectionParagraphs)
panjue_data['regulation']+=sectionParagraphs['text']
panjue_data['regulation']+=get_text(sectionParagraphs)
#多个法条分割符'######'
panjue_data['regulation']+='\n'
else:
logger.info('judgementId',res['judgementId'],col,'is not exsixt keywords:regulationGroupByTrialRoundInfos')
#解析各个分块
for sub in res['content']['paragraphs']:
if sub['typeText']=='审理经过':
panjue_data['trial_procedure']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='一审法院查明':
panjue_data['frist_court_get']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='原告诉称':
panjue_data['original_told']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='上诉人诉称':
panjue_data['appellor_told']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='被告辩称':
panjue_data['was_told']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='被上诉人辩称':
panjue_data['by_appellor_told']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='本院查明':
panjue_data['court_get']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='一审法院认为':
panjue_data['frist_court_consider']=get_court_consider(sub['subParagraphs'])
continue
if sub['typeText']=='本院认为':
panjue_data['court_consider']=get_court_consider(sub['subParagraphs'])
continue
if sub['typeText']=='裁判结果':
panjue_data['court_result']=get_text(sub['subParagraphs'])
continue
if sub['typeText']=='二审裁判结果':
panjue_data['second_court_result']=get_text(sub['subParagraphs'])
continue
#str_json=json.dumps(panjue_data,indent=2, ensure_ascii=False)
#with open('data.txt','a') as f:
#f.write(str_json+'\n')
w.append(panjue_data)
if inx%1000==0:
print('=======> {}有{}条数据已保存'.format(col,inx))
data_to_mysql(w,keys,test=False)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lawForNlp/quliang.git
git@gitee.com:lawForNlp/quliang.git
lawForNlp
quliang
quliang
master

搜索帮助