whoosh mysql_Whoosh例子

whoosh mysql_Whoosh例子Whoosh 的分词是基于正则表达式的 所以只需要写出合适的正则表达式就可以正确分词 当然 因为 Whoosh 是纯 python 的 你要重新实现分词模块或是使用第三方分词模块都是很容易的 下面是一些例子 基于正则表达式 可能有不完善的地方 需要继续完善完善 测试分词 usr bin envpython coding UTF 8 fromwhoosh analysisimp

Whoosh的分词是基于正则表达式的,所以只需要写出合适的正则表达式就可以正确分词。

当然,因为Whoosh是纯python的,你要重新实现分词模块或是使用第三方分词模块都是很容易的。

下面是一些例子(基于正则表达式),可能有不完善的地方,需要继续完善完善。

#测试分词

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

from whoosh.analysis import RegexAnalyzer

rex = RegexAnalyzer(ur”([\u4e00-\u9fa5])|(\w+(\.?\w+)*)”)

print [token.text for token in rex(u”hi 中 000 中文测试中文 there 3.141 big-time under_score”)]

#一个完整的演示

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

from whoosh.index import create_in

from whoosh.fields import *

from whoosh.analysis import RegexAnalyzer

analyzer = RegexAnalyzer(ur”([\u4e00-\u9fa5])|(\w+(\.?\w+)*)”)

schema = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT(stored=True, analyzer=analyzer))

ix = create_in(“indexdir”, schema)

writer = ix.writer()

writer.add_document(title=u”First document”, path=u”/a”,

content=u”This is the first document we’ve added!”)

writer.add_document(title=u”Second document”, path=u”/b”,

content=u”The second one 你 中文测试中文 is even more interesting!”)

writer.commit()

searcher = ix.searcher()

results = searcher.find(“content”, u”first”)

print results[0]

results = searcher.find(“content”, u”你”)

print results[0]

results = searcher.find(“content”, u”测试”)

print results[0]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/220756.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月17日 下午7:43
下一篇 2026年3月17日 下午7:44


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号