Created
November 13, 2013 08:11
-
-
Save duydo/7445455 to your computer and use it in GitHub Desktop.
Simple fulltex search in-memory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import re | |
import shlex | |
def search(query): | |
pieces = shlex.split(query.encode('utf-8')) | |
include, or_include, exclude = [], [], [] | |
for piece in pieces: | |
p = piece.decode('utf-8') | |
if p.startswith('-'): | |
exclude.append(re.compile(ur'\b%s\b' % p[1:], re.U)) | |
elif p.startswith('|'): | |
or_include.append(re.compile(ur'\b%s\b' % p[1:], re.U)) | |
else: | |
include.append(re.compile(ur'\b%s\b' % p, re.U)) | |
def validator(s): | |
return ( | |
( | |
all(p.search(s) for p in include) and | |
not any(p.search(s) for p in exclude) | |
) | |
or any(p.search(s) for p in or_include) | |
) | |
return validator | |
query = u'"Con mèo trèo"' | |
match = search(query) | |
print match(u"Con mèo trèo lên cây cau. Hỏi thăm chú chuột đi đâu vắng nhà. Chú chuột đi chợ đường xa. Mua mắm mua muối giỗ cha chú mèo") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment