需要寫個(gè)小腳本給不會(huì)編程的同事用,大致是他們自己編寫檢索條件然后去運(yùn)行腳本得出結(jié)果。
檢索條件類似這種:虛汗 or ((怕 or 畏) and (寒 or 冷))
目前試了兩種,in 方式( func1 )和正則匹配( func2 )
測(cè)試語(yǔ)料 1220 條,花費(fèi)時(shí)間:
func1 運(yùn)行時(shí)間:26.843648433685303
func2 運(yùn)行時(shí)間:46.992613554000854
當(dāng)語(yǔ)料很大,檢索條件量很多(或者嵌套很多)。效率感覺不怎么理想? 問問有沒思路改進(jìn)一下效率,或者有沒更好的方法思路。
相關(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)換成正則表達(dá)式
遞歸方法
如:['虛汗','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 -- 語(yǔ)料文件名
"""
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 -- 語(yǔ)料文件名
"""
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__':
"""測(cè)試"""
patternFile = '檢索條件_測(cè)試.xlsx'
sentenceFile = '語(yǔ)料_測(cè)試.xlsx'
t1 = time.time()
func2(patternFile,sentenceFile)
t2 = time.time()
print('func2 運(yùn)行時(shí)間:',t2-t1)
大致是他們自己編寫檢索條件然后去運(yùn)行腳本得出結(jié)果
部署一套現(xiàn)成的查詢引擎,比如 Elasticsearch
https://www.elastic.co/produc...
北大青鳥APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問,美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。