python倒排索引(Inverted index) 倒排索引为什么叫倒排索引
python\u5012\u6392\u7d22\u5f15\u5efa\u7acb\u6c42\u52a9\u8d34cdays-3-test.txt \u5185\u5bb9:
1 key12 key23 key17 key38 key210 key114 key219 key420 key130 key3
\u8bfb\u53d6\u67d0\u4e00\u7b80\u5355\u7d22\u5f15\u6587\u4ef6cdays-3-test.txt\uff0c\u5176\u6bcf\u884c\u683c\u5f0f\u4e3a\u6587\u6863\u5e8f\u53f7 \u5173\u952e\u8bcd\uff0c\u73b0\u9700\u6839\u636e\u8fd9\u4e9b\u4fe1\u606f\u8f6c\u5316\u4e3a\u5012\u6392\u7d22\u5f15\uff0c\u5373\u7edf\u8ba1\u5173\u952e\u8bcd\u5728\u54ea\u4e9b\u6587\u6863\u4e2d\uff0c\u683c\u5f0f\u5982\u4e0b\uff1a\u5305\u542b\u8be5\u5173\u952e\u8bcd\u7684\u6587\u6863\u6570 \u5173\u952e\u8bcd => \u6587\u6863\u5e8f\u53f7\u3002\u5176\u4e2d\uff0c\u539f\u7d22\u5f15\u6587\u4ef6\u4f5c\u4e3a\u547d\u4ee4\u884c\u53c2\u6570\u4f20\u5165\u4e3b\u7a0b\u5e8f\uff0c\u5e76\u8bbe\u8ba1\u4e00\u4e2acollect\u51fd\u5f0f\u7edf\u8ba1 "\u5173\u952e\u5b57\u5e8f\u53f7" \u7ed3\u679c\u5bf9\uff0c\u6700\u540e\u5728\u4e3b\u7a0b\u5e8f\u4e2d\u8f93\u51fa\u7ed3\u679c\u81f3\u5c4f\u5e55\u3002
s = raw_input()
lines = s.split('
')
dictlines = lines[:100]
mydict = {}
# read
for i,line in enumerate(dictlines ):
for word in line.split():
mydict.setdefault(word,[]).append(i + 1)
# print indices
for word in mydict.keys():
print "%s: %s" % (word,", ".join(map(str,sorted(mydict[word]))))
def andSearch(words_list):
global mydict
a = set(range(1,101))
for word in words_list:
a = a.intersection(set(mydict[word]))
return a
def orSearch(words_list):
global mydict
a = set([])
for word in words_list:
a = a.union(set(mydict[word]))
return a
# Query
index = 100
u = lines[index]
while index < len(lines):
words_list = u.split()
if ":" in u:
if words_list[0] == "OR:":
a = orSearch(words_list)
else:
if words_list[0] == 'AND:':
words_list = words_list[1:]
a = andSearch(words_list)
if not a:
print ", ".join(map(str,list(a)))
else:
print "None"
index += 1
大致思想就是这样。。。。。。。。
绛旓細1 key12 key23 key17 key38 key210 key114 key219 key420 key130 key3 璇诲彇鏌愪竴绠鍗曠储寮曟枃浠禼days-3-test.txt锛屽叾姣忚鏍煎紡涓烘枃妗e簭鍙 鍏抽敭璇嶏紝鐜伴渶鏍规嵁杩欎簺淇℃伅杞寲涓鍊掓帓绱㈠紩锛屽嵆缁熻鍏抽敭璇嶅湪鍝簺鏂囨。涓紝鏍煎紡濡備笅锛氬寘鍚鍏抽敭璇嶇殑鏂囨。鏁 鍏抽敭璇 => 鏂囨。搴忓彿銆傚叾涓紝鍘熺储寮曟枃浠朵綔涓哄懡浠よ鍙傛暟...
绛旓細娉1: match_all 涓嶇敤閬嶅巻鍊掓帓绱㈠紩,姣旂洿鎺ユ煡璇 @timestamp:* 瑕佸揩寰堝銆 娉2:鍒ゆ柇瑕嗙洊淇敼涓 match_all 骞朵笉鏄湡鐨勬寜鐓х储寮曞悕绉,鑰屾槸 ES 浠 2.x 寮濮嬫彁渚涚殑 field_stats 鎺ュ彛鍙互鐩存帴鑾峰彇鍒 @timestamp 鍦ㄦ湰绱㈠紩鍐呯殑 max/min 鍊笺傚綋鐒朵粠姒傚康涓婂姝ょ悊瑙d篃鏄彲浠ユ帴鍙楃殑銆 鍝嶅簲缁撴灉濡備笅: 鍜宖ilter cache ...
绛旓細2锛塩ut_all鍙傛暟鐢ㄦ潵鎺у埗鏄惁閲囩敤鍏ㄦā寮 jieba.cut_for_search鏂规硶鎺ュ彈涓涓弬鏁帮細闇瑕佸垎璇嶇殑瀛楃涓,璇ユ柟娉曢傚悎鐢ㄤ簬鎼滅储寮曟搸鏋勫缓鍊掓帓绱㈠紩鐨勫垎璇嶏紝绮掑害姣旇緝缁 娉ㄦ剰锛氬緟鍒嗚瘝鐨勫瓧绗︿覆鍙互鏄痝bk瀛楃涓层乽tf-8瀛楃涓叉垨鑰卽nicode jieba.cut浠ュ強jieba.cut_for_search杩斿洖鐨勭粨鏋勯兘鏄竴涓彲杩唬鐨刧enerator锛屽彲浠ヤ娇鐢╢or...
绛旓細涓.缃戠粶鐖櫕 瑕佺敤C璇█鍘诲仛涓涓埇铏郴缁燂紝鎴愭湰浼氶潪甯搁珮锛屽緢澶氫笢瑗块兘闇瑕佽嚜宸卞疄鐜般傝岀埇铏郴缁燂紝涓汉瑙夊緱Python鐨Scrapy妗嗘灦鏄竴涓緢濂界殑閫夋嫨锛屼粠缃戦〉鐖彇鍒版暟鎹瓨鍏ユ暟鎹簱鏈変竴鏉″畬鏁寸殑閾捐矾銆傜埇铏郴缁熷鎬ц兘鐨勮姹備笉楂橈紝Python瀹屽叏鍙互鑳滀换銆備簩.鍊掓帓绱㈠紩 鍊掓帓绱㈠紩涓昏鍖呮嫭绱㈠紩寤虹珛鍜岀储寮曟洿鏂般傜储寮曠殑鏁版嵁缁撴瀯...
绛旓細Splunk浠h〃涓涓嫢鏈夋悳绱㈠姛鑳界殑绱㈠紩闆嗗悎 姣忎竴涓泦鍚堜腑鍖呭惈涓涓竷闅嗚繃婊ゅ櫒锛屼竴涓鍊掓帓璇嶈〃锛堝瓧鍏革級锛屽拰涓涓瓨鍌ㄦ墍鏈変簨浠剁殑鏁扮粍 褰撲竴涓簨浠惰鍔犲叆鍒扮储寮曠殑鏃跺欙紝浼氬仛浠ヤ笅鐨勯昏緫 涓烘瘡涓涓簨浠剁敓鎴愪竴涓猽nqie id锛岃繖閲屽氨鏄簭鍙 瀵逛簨浠惰繘琛屽垎璇嶏紝鎶婃瘡涓涓瘝鍔犲叆鍒板掓帓璇嶈〃锛屼篃灏辨槸姣忎竴涓瘝瀵瑰簲鐨勪簨浠剁殑id鐨...
绛旓細1.2 绯荤粺鎬濊矾涓庢鏋 鏈郴缁熸讳綋鐨勫疄鐜版濊矾濡傚浘 1 鎵绀猴細涓涓畬鏁寸殑鎼滅储绯荤粺涓昏鐨勬楠ゆ槸锛氬鏂伴椈缃戦〉杩涜鐖櫕寰楀埌璇枡搴 鎶藉彇鏂伴椈鐨勪富浣撳唴瀹癸紝寰楀埌缁撴瀯鍖栫殑 xml 鏁版嵁 鍐呭瓨寮忓崟閬嶆壂鎻忕储寮曟瀯寤烘柟娉曟瀯寤鍊掓帓绱㈠紩锛屼緵妫绱㈡ā鍧椾娇鐢 鐢ㄦ埛杈撳叆鏌ヨ锛屽緱鍒扮浉鍏虫枃妗h繑鍥炵粰鐢ㄦ埛 2 璁捐鏂规 2.1 鏂伴椈鐖彇 ...
绛旓細import repatt = re.compile("\w+")def makeIndex(filename): index = {} with open(filename) as handle: for i, ln in enumerate(handle): for word in patt.findall(ln): index.setdefault(word, []).append(i+1) return indexdef printIndex(index): for wor...
绛旓細鍏ㄦ枃鎼滅储鐨勫熀纭鏄鍊掓帓绱㈠紩锛屽畠閫氳繃璇嶆壘鍒板寘鍚璇嶇殑鏂囨。銆傚浜庤タ鏂囪瑷锛屽垎璇嶇浉瀵圭畝鍗曪紝閫氳繃绌烘牸鍒嗛殧鍗冲彲銆傜劧鑰岋紝涓棩闊╄瑷涓嶄娇鐢ㄧ┖鏍煎垏鍒嗭紝鍥犳闇瑕侀氳繃浜屽厓鍒嗚瘝鏂规硶灏嗗彞瀛愬垎瑙d负鏇村皬鐨勮瘝缁勶紝浠ュ埄鐢∕ongoDB鐨勫叏鏂囨悳绱㈠姛鑳姐傞氳繃浣跨敤Python鐨浜屽厓鍒嗚瘝搴擄紝鎴戜滑鍙互灏嗗叏琛ㄩ亶鍘嗛渶瑕佸叏鏂囨悳绱㈢殑闆嗗悎锛屽苟灏嗘寚瀹氭枃鏈瓧娈...
绛旓細棣栧厛锛屽氨鏄湁缃戠粶鐖櫕涓嶆柇鎶撳彇鍚勪釜缃戠珯鐨勭綉椤碉紝瀛樻斁鍒版悳绱㈠紩鎿庣殑鏁版嵁搴擄紱鎺ョ潃锛岀储寮曠▼搴忚鍙栨暟鎹簱鐨勭綉椤佃繘琛屾竻鐞嗭紝寤虹珛鍊掓帓绱㈠紩锛涙渶鍚庯紝鎼滅储绋嬪簭鎺ユ敹鐢ㄦ埛鐨勬煡璇㈠叧閿瘝锛屽幓绱㈠紩閲岄潰鎵惧埌鐩稿叧鍐呭锛屽苟閫氳繃涓瀹氱殑鎺掑簭绠楁硶锛圥agerank绛夛級鎶婃渶鐩稿叧鏈濂界殑缁撴灉鎺掑湪鏈鍓嶉潰鍛堢幇缁欑敤鎴枫傜湅涓婂幓绠绠鍗曞崟鐨勪笁涓儴鍒嗭紝鍗...
绛旓細鑳戒笉鑳界啛鎮変笅鍊掓帓绱㈠紩鐨勫師鐞?鑳戒笉鑳界啛鎮変笅妫绱㈢殑鍩烘湰娴佺▼?鑳戒笉鑳界啛鎮変笅 NLP 鎶鏈湪鎼滅储涓ぇ姒傛湁鍝簺浣滅敤?濡傛灉閮藉緢鐔熸倝浜,閭h兘涓嶈兘浜嗚В涓嬭闊宠緭鍏ュ拰杈撳嚭鐨勫ぇ姒傚師鐞,涔熷氨鏄闊宠瘑鍒拰璇煶鍚堟垚?绗簩绫荤殑瀹炰範鐢熺鍒拌繃鍑犱釜銆備笉杩囪繖绫诲疄涔犵敓鍙亣鑰屼笉鍙眰,闈㈣瘯杩囩殑铏界劧寰堝,浣嗘槸姣曠珶浠栦滑閫夋嫨浼楀,灏辨槸缂樹唤鍒颁簡,鑳芥潵...