Classes and functions for turning a piece of text into an indexable stream of "tokens" (usually equivalent to words). There are three general classes involved in analysis:
标记化器总是在文本处理管道的开始。它们接受一个字符串并产生与文本中的标记(字)相对应的标记对象(实际上,出于性能原因,相同的标记对象反复出现)。
每个记号赋予器都是一个可调用的,它接受一个字符串并返回记号的迭代器。
过滤器从标记器中获取标记并对其执行各种转换。例如,LowercaseFilter将所有标记转换为小写,这在为常规英语文本编制索引时通常是必需的。
每个过滤器都是一个可调用的,它接受一个令牌生成器并返回一个令牌生成器。
分析器是方便的函数/类,它将标记器和零个或多个过滤器“打包”成一个单元。例如,StandardAnalyzer结合了RegExtokenizer、LowercaseFilter和StopFilter。
每个分析器都是可调用的,它接受一个字符串并返回一个令牌迭代器。(因此,如果不需要任何过滤,标记化器可以用作分析工具)。
您可以使用 |
性状:
my_analyzer = RegexTokenizer() | LowercaseFilter() | StopFilter()
The first item must be a tokenizer and the rest must be filters (you can't put a filter first or a tokenizer after the first item).
whoosh.analysis.
IDAnalyzer
(lowercase=False)¶不推荐使用,直接使用idtokenizer,如果需要的话,使用LowercaseFilter。
whoosh.analysis.
KeywordAnalyzer
(lowercase=False, commas=False)¶解析空白或逗号分隔的标记。
>>> ana = KeywordAnalyzer()
>>> [token.text for token in ana("Hello there, this is a TEST")]
["Hello", "there,", "this", "is", "a", "TEST"]
参数: |
|
---|
whoosh.analysis.
RegexAnalyzer
(expression='\\w+(\\.?\\w+)*', gaps=False)¶不推荐使用,直接使用regextokenizer。
whoosh.analysis.
SimpleAnalyzer
(expression=re.compile('\\w+(\\.?\\w+)*'), gaps=False)¶用一个较低的caseFilter组合一个regexeckenizer。
>>> ana = SimpleAnalyzer()
>>> [token.text for token in ana("Hello there, this is a TEST")]
["hello", "there", "this", "is", "a", "test"]
参数: |
|
---|
whoosh.analysis.
StandardAnalyzer
(expression=re.compile('\\w+(\\.?\\w+)*'), stoplist=frozenset({'may', 'your', 'or', 'when', 'are', 'have', 'for', 'as', 'and', 'of', 'a', 'will', 'you', 'from', 'we', 'it', 'can', 'yet', 'at', 'the', 'with', 'on', 'tbd', 'if', 'that', 'an', 'this', 'us', 'by', 'be', 'to', 'is', 'in', 'not'}), minsize=2, maxsize=None, gaps=False)¶用一个较低的casefilter和可选的stopfilter组合一个regextokenizer。
>>> ana = StandardAnalyzer()
>>> [token.text for token in ana("Testing is testing and testing")]
["testing", "testing", "testing"]
参数: |
|
---|
whoosh.analysis.
StemmingAnalyzer
(expression=re.compile('\\w+(\\.?\\w+)*'), stoplist=frozenset({'may', 'your', 'or', 'when', 'are', 'have', 'for', 'as', 'and', 'of', 'a', 'will', 'you', 'from', 'we', 'it', 'can', 'yet', 'at', 'the', 'with', 'on', 'tbd', 'if', 'that', 'an', 'this', 'us', 'by', 'be', 'to', 'is', 'in', 'not'}), minsize=2, maxsize=None, gaps=False, stemfn=<function stem>, ignore=None, cachesize=50000)¶使用小写过滤器、可选停止过滤器和词干过滤器组合regextokenizer。
>>> ana = StemmingAnalyzer()
>>> [token.text for token in ana("Testing is testing and testing")]
["test", "test", "test"]
参数: |
|
---|
whoosh.analysis.
FancyAnalyzer
(expression='\\s+', stoplist=frozenset({'may', 'your', 'or', 'when', 'are', 'have', 'for', 'as', 'and', 'of', 'a', 'will', 'you', 'from', 'we', 'it', 'can', 'yet', 'at', 'the', 'with', 'on', 'tbd', 'if', 'that', 'an', 'this', 'us', 'by', 'be', 'to', 'is', 'in', 'not'}), minsize=2, maxsize=None, gaps=True, splitwords=True, splitnums=True, mergewords=False, mergenums=False)¶使用字内筛选器、LowerCaseFilter和StopFilter组合RegeXeXeKenizer。
>>> ana = FancyAnalyzer()
>>> [token.text for token in ana("Should I call getInt or get_real?")]
["should", "call", "getInt", "get", "int", "get_real", "get", "real"]
参数: |
|
---|
whoosh.analysis.
NgramAnalyzer
(minsize, maxsize=None)¶组成一个ngramtokenizer和一个lowercasefilter。
>>> ana = NgramAnalyzer(4)
>>> [token.text for token in ana("hi there")]
["hi t", "i th", " the", "ther", "here"]
whoosh.analysis.
NgramWordAnalyzer
(minsize, maxsize=None, tokenizer=None, at=None)¶whoosh.analysis.
LanguageAnalyzer
(lang, expression=re.compile('\\w+(\\.?\\w+)*'), gaps=False, cachesize=50000)¶为给定语言配置简单的分析器,包括LowercaseFilter、StopFilter和StemFilter。
>>> ana = LanguageAnalyzer("es")
>>> [token.text for token in ana("Por el mar corren las liebres")]
['mar', 'corr', 'liebr']
可用语言列表位于 whoosh.lang.languages. 你可以使用 whoosh.lang.has_stemmer()
和 whoosh.lang.has_stopwords()
检查给定语言是否具有词干函数和/或停止词列表。
参数: |
|
---|
whoosh.analysis.
IDTokenizer
¶生成作为单个标记的整个输入字符串。用于索引但未命名的字段,如文档的路径。
>>> idt = IDTokenizer()
>>> [token.text for token in idt("/a/b 123 alpha")]
["/a/b 123 alpha"]
whoosh.analysis.
RegexTokenizer
(expression=re.compile('\w+(\.?\w+)*'), gaps=False)¶使用正则表达式从文本中提取标记。
>>> rex = RegexTokenizer()
>>> [token.text for token in rex(u("hi there 3.141 big-time under_score"))]
["hi", "there", "3.141", "big", "time", "under_score"]
参数: |
|
---|
whoosh.analysis.
CharsetTokenizer
(charmap)¶根据字符映射对象标记和翻译文本。映射为“无”的字符被视为令牌中断字符。对于所有其他字符,映射用于转换字符。这对于大小写和重音折叠很有用。
这个标记器逐字符循环,因此可能比 RegexTokenizer
.
获取字符映射对象的一种方法是使用 whoosh.support.charset.charset_table_to_dict()
.
>>> from whoosh.support.charset import charset_table_to_dict
>>> from whoosh.support.charset import default_charset
>>> charmap = charset_table_to_dict(default_charset)
>>> chtokenizer = CharsetTokenizer(charmap)
>>> [t.text for t in chtokenizer(u'Stra\xdfe ABC')]
[u'strase', u'abc']
sphinx charset table格式在http://www.sphinxsearch.com/docs/current.html_conf charset table中描述。
参数: | charmap -- unicode.translate()方法使用的从整数字符数到unicode字符的映射。 |
---|
whoosh.analysis.
SpaceSeparatedTokenizer
()¶返回按空白分割标记的regextokenizer。
>>> sst = SpaceSeparatedTokenizer()
>>> [token.text for token in sst("hi there big-time, what's up")]
["hi", "there", "big-time,", "what's", "up"]
whoosh.analysis.
CommaSeparatedTokenizer
()¶按逗号拆分标记。
注意,标记器在正则表达式的每个匹配项上调用unicode.strip()。
>>> cst = CommaSeparatedTokenizer()
>>> [token.text for token in cst("hi there, what's , up")]
["hi there", "what's", "up"]
whoosh.analysis.
NgramTokenizer
(minsize, maxsize=None)¶将输入文本拆分为n个图而不是单词。
>>> ngt = NgramTokenizer(4)
>>> [token.text for token in ngt("hi there")]
["hi t", "i th", " the", "ther", "here"]
请注意,此标记器不使用正则表达式提取单词,因此它发出的克数将包含空格、标点符号等。您可能希望按摩输入或向此标记器的输出添加自定义筛选器。
或者,如果您只需要不带空格的子词grams,那么可以将regexeckenizer与ngramfilter组合起来。
参数: |
|
---|
whoosh.analysis.
PathTokenizer
(expression='[^/]+')¶一个简单的标记器,它给出了一个字符串 "/a/b/c"
生成令牌 ["/a", "/a/b", "/a/b/c"]
.
whoosh.analysis.
PassFilter
¶标识过滤器:不受影响地传递令牌。
whoosh.analysis.
LoggingFilter
(logger=None)¶打印作为调试日志项通过的每个筛选器的内容。
参数: | target -- 要使用的记录器。如果省略,则使用“whoosh.analysis”记录器。 |
---|
whoosh.analysis.
MultiFilter
(**kwargs)¶根据令牌流的“模式”属性选择两个或多个子筛选器中的一个。
使用关键字参数将模式属性值与实例化的筛选器关联。
>>> iwf_for_index = IntraWordFilter(mergewords=True, mergenums=False)
>>> iwf_for_query = IntraWordFilter(mergewords=False, mergenums=False)
>>> mf = MultiFilter(index=iwf_for_index, query=iwf_for_query)
此类期望模式属性的值在令牌流中的所有令牌之间都是一致的。
whoosh.analysis.
TeeFilter
(*filters)¶交错两个或多个过滤器(或过滤器链)的结果。
注意:因为它需要为每个子过滤器创建每个令牌的副本,所以这个过滤器非常慢。
>>> target = "ALFA BRAVO CHARLIE"
>>> # In one branch, we'll lower-case the tokens
>>> f1 = LowercaseFilter()
>>> # In the other branch, we'll reverse the tokens
>>> f2 = ReverseTextFilter()
>>> ana = RegexTokenizer(r"\S+") | TeeFilter(f1, f2)
>>> [token.text for token in ana(target)]
["alfa", "AFLA", "bravo", "OVARB", "charlie", "EILRAHC"]
要将传入的令牌流与过滤器链的输出相结合,请使用``TeeFilter``,并将其中一个筛选器设为:class:PassFilter。
>>> f1 = PassFilter()
>>> f2 = BiWordFilter()
>>> ana = RegexTokenizer(r"\S+") | TeeFilter(f1, f2) | LowercaseFilter()
>>> [token.text for token in ana(target)]
["alfa", "alfa-bravo", "bravo", "bravo-charlie", "charlie"]
whoosh.analysis.
ReverseTextFilter
¶反转每个标记的文本。
>>> ana = RegexTokenizer() | ReverseTextFilter()
>>> [token.text for token in ana("hello there")]
["olleh", "ereht"]
whoosh.analysis.
LowercaseFilter
¶使用unicode.lower()将标记文本小写。
>>> rext = RegexTokenizer()
>>> stream = rext("This is a TEST")
>>> [token.text for token in LowercaseFilter(stream)]
["this", "is", "a", "test"]
whoosh.analysis.
StripFilter
¶对标记文本调用unicode.strip()。
whoosh.analysis.
StopFilter
(stoplist=frozenset({'may', 'your', 'or', 'when', 'are', 'have', 'for', 'as', 'and', 'of', 'a', 'will', 'you', 'from', 'we', 'it', 'can', 'yet', 'at', 'the', 'with', 'on', 'tbd', 'if', 'that', 'an', 'this', 'us', 'by', 'be', 'to', 'is', 'in', 'not'}), minsize=2, maxsize=None, renumber=True, lang=None)¶在流中标记“stop”单词(单词太常见,无法索引)(默认情况下会删除它们)。
确保在此筛选器之前使用 LowercaseFilter
.
>>> stopper = RegexTokenizer() | StopFilter()
>>> [token.text for token in stopper(u"this is a test")]
["test"]
>>> es_stopper = RegexTokenizer() | StopFilter(lang="es")
>>> [token.text for token in es_stopper(u"el lapiz es en la mesa")]
["lapiz", "mesa"]
可用语言列表位于 whoosh.lang.languages. 你可以使用 whoosh.lang.has_stopwords()
检查给定语言是否有可用的停止词列表。
参数: |
|
---|
whoosh.analysis.
StemFilter
(stemfn=<function stem>, lang=None, ignore=None, cachesize=50000)¶使用波特词干算法从令牌文本中删除后缀。词干化试图将同一根词的多种形式(例如,“呈现”、“呈现”、“呈现”等)减少到索引中的单个词。
>>> stemmer = RegexTokenizer() | StemFilter()
>>> [token.text for token in stemmer("fundamentally willows")]
["fundament", "willow"]
您可以将自己的词干函数传递给词干过滤器。默认值是英语的波特词干算法。
>>> stemfilter = StemFilter(stem_function)
您还可以通过传递 lang 关键字参数。
>>> stemfilter = StemFilter(lang="ru")
可用语言列表位于 whoosh.lang.languages. 你可以使用 whoosh.lang.has_stemmer()
检查给定语言是否有可用的词干函数。
默认情况下,此类围绕词干函数包装一个LRU缓存。这个 cachesize
关键字参数设置缓存的大小。要使缓存无边界(类缓存每个输入),请使用 cachesize=-1
. 要禁用缓存,请使用 cachesize=None
.
如果编译并安装py stemmer库,则 PyStemmerFilter
提供对该库中的语言词干分析器的稍微简单的访问。
参数: |
|
---|
whoosh.analysis.
CharsetFilter
(charmap)¶通过使用提供的字符映射对象调用unicode.translate()来转换标记的文本。这对于大小写和重音折叠很有用。
这个 whoosh.support.charset
模块有一个很有用的重音折叠地图。
>>> from whoosh.support.charset import accent_map
>>> retokenizer = RegexTokenizer()
>>> chfilter = CharsetFilter(accent_map)
>>> [t.text for t in chfilter(retokenizer(u'café'))]
[u'cafe']
获取字符映射对象的另一种方法是使用 whoosh.support.charset.charset_table_to_dict()
.
>>> from whoosh.support.charset import charset_table_to_dict
>>> from whoosh.support.charset import default_charset
>>> retokenizer = RegexTokenizer()
>>> charmap = charset_table_to_dict(default_charset)
>>> chfilter = CharsetFilter(charmap)
>>> [t.text for t in chfilter(retokenizer(u'Stra\xdfe'))]
[u'strase']
sphinx charset table格式在http://www.sphinxsearch.com/docs/current.html_conf charset table中描述。
参数: | charmap -- 根据unicode.translate()方法的要求,从整数字符数到unicode字符的字典映射。 |
---|
whoosh.analysis.
NgramFilter
(minsize, maxsize=None, at=None)¶将令牌文本拆分为n个标记。
>>> rext = RegexTokenizer()
>>> stream = rext("hello there")
>>> ngf = NgramFilter(4)
>>> [token.text for token in ngf(stream)]
["hell", "ello", "ther", "here"]
参数: |
|
---|
whoosh.analysis.
IntraWordFilter
(delims='-_'"()!@#$%^&*[]{}<>\|;:, ./?`~=+', splitwords=True, splitnums=True, mergewords=False, mergenums=False)¶将单词拆分为子字,并对子字组执行可选转换。这个过滤器的功能是基于solr中yonik的wordDelimiterFilter,但不共享任何代码。
mergewords和mergenums参数启用子命令的合并。
当合并参数为false时,子字不会合并。
当一个或两个合并参数都为真时,连续运行的字母和/或数字子字将合并到与最后一个子字位置相同的附加标记中。
当使用这个过滤器时,你应该使用一个只在空格上进行拆分的记号赋予器,这样记号赋予器在这个过滤器能看到它们之前不会删除单词内的定界符,并且在任何使用lowercasefilter之前放置这个过滤器。
>>> rt = RegexTokenizer(r"\S+")
>>> iwf = IntraWordFilter()
>>> lcf = LowercaseFilter()
>>> analyzer = rt | iwf | lcf
此过滤器的一个用途是帮助匹配概念的不同书面表示。例如,如果源文本包含 wi-fi, 你可能想要 wifi, WiFi, wi-fi, 等匹配。一种方法是在用于索引的分析器中指定mergewords=true和/或mergenums=true,在用于查询的分析器中指定mergewords=false/mergenums=false。
>>> iwf_i = IntraWordFilter(mergewords=True, mergenums=True)
>>> iwf_q = IntraWordFilter(mergewords=False, mergenums=False)
>>> iwf = MultiFilter(index=iwf_i, query=iwf_q)
>>> analyzer = RegexTokenizer(r"\S+") | iwf | LowercaseFilter()
(见 MultiFilter
)
参数: |
|
---|
whoosh.analysis.
CompoundWordFilter
(wordset, keep_compound=True)¶给定一组单词(或具有 __contains__
方法),将流中由单词集中的单词组成的任何标记分解为各自的部分。
给定正确的一组单词,此过滤器可以分解run together单词和商标(例如“turbosuid”、“applescript”)。它也可以用于粘合语言,如德语。
这个 keep_compound
参数允许您决定是否将复合词与词段一起保留在标记流中。
>>> cwf = CompoundWordFilter(wordset, keep_compound=True)
>>> analyzer = RegexTokenizer(r"\S+") | cwf
>>> [t.text for t in analyzer("I do not like greeneggs and ham")
["I", "do", "not", "like", "greeneggs", "green", "eggs", "and", "ham"]
>>> cwf.keep_compound = False
>>> [t.text for t in analyzer("I do not like greeneggs and ham")
["I", "do", "not", "like", "green", "eggs", "and", "ham"]
参数: |
|
---|
whoosh.analysis.
BiWordFilter
(sep='-')¶将相邻标记合并为“双字”标记,例如:
"the", "sign", "of", "four"
变成::
"the-sign", "sign-of", "of-four"
这可用于创建用于伪短语搜索的字段,如果所有术语都匹配,则文档可能包含该短语,但搜索速度比对单个词进行短语搜索要快。
这个 BiWordFilter
is much faster than using the otherwise equivalent ShingleFilter(2)
.
whoosh.analysis.
ShingleFilter
(size=2, sep='-')¶将一定数量的相邻标记合并为多字标记,例如:
"better", "a", "witty", "fool", "than", "a", "foolish", "wit"
具有 ShingleFilter(3, ' ')
变成::
'better a witty', 'a witty fool', 'witty fool than', 'fool than a',
'than a foolish', 'a foolish wit'
这可用于创建用于伪短语搜索的字段,如果所有术语都匹配,则文档可能包含该短语,但搜索速度比对单个词进行短语搜索要快。
如果你用的是两个词的木瓦,你应该使用功能上相同的木瓦。 BiWordFilter
而是因为它比 ShingleFilter
.
whoosh.analysis.
DelimitedAttributeFilter
(delimiter='^', attribute='boost', default=1.0, type=<class 'float'>)¶在每个标记的文本中查找分隔符字符,并将数据存储在标记的命名属性中分隔符之后。
默认设置为使用 ^
字符作为分隔符,并将值存储在 ^
作为令牌的助推器。
>>> daf = DelimitedAttributeFilter(delimiter="^", attribute="boost")
>>> ana = RegexTokenizer("\\S+") | DelimitedAttributeFilter()
>>> for t in ana(u("image render^2 file^0.5"))
... print("%r %f" % (t.text, t.boost))
'image' 1.0
'render' 2.0
'file' 0.5
请注意,您需要确保标记器包含分隔符和数据作为标记的一部分!
参数: |
|
---|
whoosh.analysis.
DoubleMetaphoneFilter
(primary_boost=1.0, secondary_boost=0.5, combine=False)¶使用Lawrence Philips的双变音算法转换标记的文本。该算法试图对单词进行编码,使发音相似的单词减少为相同的代码。这可能对包含人名和地名的字段以及其他需要允许拼写差异的用途很有用。
参数: |
|
---|
whoosh.analysis.
SubstitutionFilter
(pattern, replacement)¶对标记文本执行正则表达式替换。
这对于从标记中删除文本尤其有用,例如连字符:
ana = RegexTokenizer(r"\S+") | SubstitutionFilter("-", "")
Because it has the full power of the re.sub() method behind it, this filter can perform some fairly complex transformations. 例如,获取令牌 'a=b', 'c=d', 'e=f'
把它们换成 'b=a', 'd=c', 'f=e'
::
# Analyzer that swaps the text on either side of an equal sign
rt = RegexTokenizer(r"\S+")
sf = SubstitutionFilter("([^/]*)/(./*)", r"\2/\1")
ana = rt | sf
参数: |
|
---|
whoosh.analysis.
Token
(positions=False, chars=False, removestops=True, mode='', **kwargs)¶表示从被索引的源文本中提取的“标记”(通常是单词)。
有关详细信息,请参阅《用户指南》中的“高级分析”。
由于Python中的对象实例化速度很慢,标记化器应该创建一个单一的标记对象并反复生成它,每次都更改属性。
这个技巧意味着令牌(即过滤器)的使用者决不能试图在循环迭代之间保留令牌对象,或者将令牌生成器转换为列表。相反,保存迭代之间的属性,而不是对象:
def RemoveDuplicatesFilter(self, stream):
# Removes duplicate words.
lasttext = None
for token in stream:
# Only yield the token if its text doesn't
# match the previous token.
if lasttext != token.text:
yield token
lasttext = token.text
...or, call token.copy() to get a copy of the token object.
参数: |
|
---|
whoosh.analysis.
unstopped
(tokenstream)¶从token.stopped=true的令牌流中删除令牌。