เครื่องมือจับคู่และแทนที่คำหลัก Trie และคำหลักประสิทธิภาพสูง
มันใช้งานโดย cython และจะถูกคอมไพล์เป็น cpp โครงสร้างข้อมูล trie คือซีดาร์ ซึ่งเป็นอาร์เรย์คู่ที่ได้รับการปรับปรุงให้เหมาะสม รองรับ Python2.7 และ 3.4+ รองรับดองเพื่อเทและบรรทุก
หากคุณพบว่าสิ่งนี้มีประโยชน์ โปรดให้ดาว!
โมดูลนี้เขียนด้วย Cython คุณต้องติดตั้ง Cython
pip install cyac
จากนั้นสร้าง Trie
>>> from cyac import Trie
>>> trie = Trie()
เพิ่ม/รับ/ลบคำสำคัญ
>>> trie.insert(u"哈哈") # return keyword id in trie, return -1 if doesn't exist
>>> trie.get(u"哈哈") # return keyword id in trie, return -1 if doesn't exist
>>> trie.remove(u"呵呵") # return keyword in trie
>>> trie[id] # return the word corresponding to the id
>>> trie[u"呵呵"] # similar to get but it will raise exeption if doesn't exist
>>> u"呵呵" in trie # test if the keyword is in trie
รับคีย์เวิร์ดทั้งหมด
>>> for key, id_ in trie.items():
>>> print(key, id_)
คำนำหน้า/ทำนาย
>>> # return the string in the trie which starts with given string
>>> for id_ in trie.predict(u"呵呵"):
>>> print(id_)
>>> # return the prefix of given string which is in the trie.
>>> for id_, len_ in trie.prefix(u"呵呵"):
>>> print(id_, len_)
ลองใช้สารสกัดแทนที่
>>> python_id = trie.insert(u"python")
>>> trie.replace_longest("python", {python_id: u"hahah"}, set([ord(" ")])) # the second parameter is seperator. If you specify seperators. it only matches strings tween seperators. e.g. It won't match 'apython'
>>> for id_, start, end in trie.match_longest(u"python", set([ord(" ")])):
>>> print(id_, start, end)
สารสกัดอาโฮ คอราซิค
>>> ac = AC.build([u"python", u"ruby"])
>>> for id, start, end in ac.match(u"python ruby"):
>>> print(id, start, end)
ส่งออกเป็นไฟล์ จากนั้นเราสามารถใช้ mmap เพื่อโหลดไฟล์ แชร์ข้อมูลระหว่างกระบวนการต่างๆ
>>> ac = AC.build([u"python", u"ruby"])
>>> ac.save("filename")
>>> ac.to_buff(buff_object)
เริ่มต้นจาก Python Buffer
>>> import mmap
>>> with open("filename", "r+b") as bf:
buff_object = mmap.mmap(bf.fileno(), 0)
>>> AC.from_buff(buff_object, copy=True) # it allocs new memory
>>> AC.from_buff(buff_object, copy=False) # it shares memory
ตัวอย่างกระบวนการหลายขั้นตอน
import mmap
from multiprocessing import Process
from cyac import AC
def get_mmap():
with open("random_data", "r+b") as bf:
buff_object = mmap.mmap(bf.fileno(), 0)
ac_trie = AC.from_buff(buff_object, copy=False)
# Do your aho searches here. "match" function is process safe.
processes_list = list()
for x in range(0, 6):
p = Process(
target=get_mmap,
)
p.start()
processes_list.append(p)
for p in processes_list:
p.join()
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับการประมวลผลหลายตัวและการวิเคราะห์หน่วยความจำใน cyac โปรดดูปัญหานี้
ฟังก์ชั่น "จับคู่" ของหุ่นยนต์ AC นั้นปลอดภัยสำหรับเธรด/กระบวนการ เป็นไปได้ที่จะค้นหาการจับคู่แบบขนานกับหุ่นยนต์ AC ที่ใช้ร่วมกัน แต่จะเขียนหรือต่อท้ายรูปแบบไม่ได้
บน Ubuntu 14.04.5/Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
เมื่อเทียบกับ HatTrie แกนฮอไรซอนคือโทเค็นนัมเบอร์ แกนตั้งใช้เวลาใช้งาน (วินาที)
เปรียบเทียบกับ flashText นิพจน์ทั่วไปช้าเกินไปในงานนี้ (ดูเครื่องหมายมาตรฐานของ flashText) แกนขอบฟ้าเป็นถ่านที่จะจับคู่ แกนตั้งใช้เวลาใช้งาน (วินาที)
เมื่อเปรียบเทียบกับ pyahocorasick แกนฮอไรซอนจะเป็นถ่านที่ตรงกัน แกนตั้งใช้เวลาใช้งาน (วินาที)
>>> len(char.lower()) == len(char) # this is always true in python2, but not in python3
>>> len(u"İstanbul") != len(u"İstanbul".lower()) # in python3
ในกรณีที่การจับคู่แบบไม่คำนึงถึงขนาดตัวพิมพ์ ไลบรารีนี้จะดูแลข้อเท็จจริงและส่งคืนออฟเซ็ตที่ถูกต้อง
python setup.py build
PYTHONPATH= $( pwd ) /build/BUILD_DST python3 tests/test_all.py
PYTHONPATH= $( pwd ) /build/BUILD_DST python3 bench/bench_ * .py