需要寫個小腳本給不會編程的同事用,大致是他們自己編寫檢索條件然后去運行腳本得出結(jié)果。
檢索條件類似這種:虛汗 or ((怕 or 畏) and (寒 or 冷))
目前試了兩種,in 方式( func1 )和正則匹配( func2 )
測試語料 1220 條,花費時間:
func1 運行時間:26.843648433685303
func2 運行時間:46.992613554000854
當語料很大,檢索條件量很多(或者嵌套很多)。效率感覺不怎么理想? 問問有沒思路改進一下效率,或者有沒更好的方法思路。
相關(guān)代碼文件 鏈接: https://pan.baidu.com/s/1c2llxlu 密碼: v7ia
代碼下面貼出了自己寫的
# -*- coding=utf-8 -*-
import os
import re
import time
import pandas as pd
from pandas import DataFrame
from itertools import permutations
def str_to_list(p):
""" 處理字符串轉(zhuǎn)換成列表
如:虛汗 or ((怕 or 畏) and (寒 or 冷))
結(jié)果:['虛汗','or',[['怕','or','畏'],'and',['寒','or','冷']]]
"""
p = re.sub('([^\(\)()\s(?:and|or)]+|(?:and|or))','"\\1",',p)
p = p.replace('(','[').replace('(','[').replace(')','],').replace(')','],')
p = '[%s]' % p
return eval(p)
def list_to_regex(p):
""" 處理列表轉(zhuǎn)換成正則表達式
遞歸方法
如:['虛汗','or',[['怕','or','畏'],'and',['寒','or','冷']]]
結(jié)果:(虛汗|((怕|畏).*?(寒|冷)|(寒|冷).*?(怕|畏)))
"""
tempP = [list_to_regex(x) if isinstance(x,list) else x
for x in p
if x not in ['or','and']]
if 'and' in p:
tempP = permutations(tempP) #生成排列
return '(%s)' % '|'.join(['.*?'.join(x) for x in tempP])
else:
return '(%s)' % '|'.join(tempP)
def match_sentence(p,sentence):
"""轉(zhuǎn)成 in 的方式去匹配句子"""
words = p.replace(' ','').replace(',','').replace('(','(').replace(')',')').replace('and',',and,').replace('or',',or,').replace('(',',(,').replace(')',',),').split(',')
scriptStr = [w if w in 'and or ()' \
else '"%s" in "%s"' % (w,sentence) for w in words]
if eval(' '.join(scriptStr)):
return True
return False
def func1(patternFile,sentenceFile):
"""
轉(zhuǎn)成正則再去匹配
patternFile -- 含有檢索條件的文件名
sentenceFile -- 語料文件名
"""
dfS = pd.read_excel(sentenceFile)
dfP = pd.read_excel(patternFile)
#編譯好的正則列表
regexList = [re.compile(list_to_regex(str_to_list(x))) for x in dfP.ix[:,-1]]
resultFile = 'result1.txt'
for senIdx in dfS.index:
for i,patt in enumerate(regexList):
keyword = dfP.ix[i,-2]
sentence = dfS.ix[senIdx,-1]
if patt.search(sentence):
r = dfP.ix[[i],:-1]
with open(resultFile,'a',encoding='utf-8') as f:
f.write('%s\t%s\n' % (keyword, sentence))
def func2(patternFile,sentenceFile):
"""
轉(zhuǎn)成 in 的方式再去匹配
patternFile -- 含有檢索條件的文件名
sentenceFile -- 語料文件名
"""
dfS = pd.read_excel(sentenceFile)
dfP = pd.read_excel(patternFile)
resultFile = 'result2.txt'
for senIdx in dfS.index:
for pattIdx in dfP.index:
keyword = dfP.ix[pattIdx,-2]
sentence = dfS.ix[senIdx,-1]
if match_sentence(dfP.ix[pattIdx,-1],sentence):
with open(resultFile,'a',encoding='utf-8') as f:
f.write('%s\t%s\n' % (keyword, sentence))
if __name__ == '__main__':
"""測試"""
patternFile = '檢索條件_測試.xlsx'
sentenceFile = '語料_測試.xlsx'
t1 = time.time()
func2(patternFile,sentenceFile)
t2 = time.time()
print('func2 運行時間:',t2-t1)大致是他們自己編寫檢索條件然后去運行腳本得出結(jié)果
部署一套現(xiàn)成的查詢引擎,比如 Elasticsearch https://www.elastic.co/produc...
北大青鳥APTECH成立于1999年。依托北京大學優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國家
達內(nèi)教育集團成立于2002年,是一家由留學海歸創(chuàng)辦的高端職業(yè)教育培訓機構(gòu),是中國一站式人才培養(yǎng)平臺、一站式人才輸送平臺。2014年4月3日在美國成功上市,融資1
北大課工場是北京大學校辦產(chǎn)業(yè)為響應(yīng)國家深化產(chǎn)教融合/校企合作的政策,積極推進“中國制造2025”,實現(xiàn)中華民族偉大復(fù)興的升級產(chǎn)業(yè)鏈。利用北京大學優(yōu)質(zhì)教育資源及背
博為峰,中國職業(yè)人才培訓領(lǐng)域的先行者
曾工作于聯(lián)想擔任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔任項目經(jīng)理從事移動互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍懿科技有限責任公司從事總經(jīng)理職務(wù)負責iOS教學及管理工作。
浪潮集團項目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺面向?qū)ο箝_發(fā)經(jīng)驗,技術(shù)功底深厚。 授課風格 授課風格清新自然、條理清晰、主次分明、重點難點突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫,具有快速界面開發(fā)的能力,對瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁制作和網(wǎng)頁游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗。曾經(jīng)歷任德國Software AG 技術(shù)顧問,美國Dachieve 系統(tǒng)架構(gòu)師,美國AngelEngineers Inc. 系統(tǒng)架構(gòu)師。