analysis 模块

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"]
参数
  • lowercase -- 是否将标记小写。

  • commas -- 如果为真,则项目之间用逗号分隔,而不是用空格分隔。

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"]
参数
  • expression -- 用于提取标记的正则表达式模式。

  • gaps -- 如果是,标记器 splits 在表达式上,而不是在表达式上匹配。

whoosh.analysis.StandardAnalyzer(expression=re.compile('\\w+(\\.?\\w+)*'), stoplist=frozenset({'a', 'an', 'and', 'are', 'as', 'at', 'be', 'by', 'can', 'for', 'from', 'have', 'if', 'in', 'is', 'it', 'may', 'not', 'of', 'on', 'or', 'tbd', 'that', 'the', 'this', 'to', 'us', 'we', 'when', 'will', 'with', 'yet', 'you', 'your'}), 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"]
参数
  • expression -- 用于提取标记的正则表达式模式。

  • stoplist -- 停止词列表。将此设置为“无”以禁用停止字筛选器。

  • minsize -- 小于此值的单词将从流中删除。

  • maxsize -- 将此从流中删除的时间更长的单词。

  • gaps -- 如果是,标记器 splits 在表达式上,而不是在表达式上匹配。

whoosh.analysis.StemmingAnalyzer(expression=re.compile('\\w+(\\.?\\w+)*'), stoplist=frozenset({'a', 'an', 'and', 'are', 'as', 'at', 'be', 'by', 'can', 'for', 'from', 'have', 'if', 'in', 'is', 'it', 'may', 'not', 'of', 'on', 'or', 'tbd', 'that', 'the', 'this', 'to', 'us', 'we', 'when', 'will', 'with', 'yet', 'you', 'your'}), 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"]
参数
  • expression -- 用于提取标记的正则表达式模式。

  • stoplist -- 停止词列表。将此设置为“无”以禁用停止字筛选器。

  • minsize -- 小于此值的单词将从流中删除。

  • maxsize -- 将此从流中删除的时间更长的单词。

  • gaps -- 如果是,标记器 splits 在表达式上,而不是在表达式上匹配。

  • ignore -- 一组不可词干的词。

  • cachesize -- 要缓存的词干字的最大数目。这个数字越大,词干速度越快,但使用的内存越多。对于无缓存使用none,对于无边界缓存使用-1。

whoosh.analysis.FancyAnalyzer(expression='\\s+', stoplist=frozenset({'a', 'an', 'and', 'are', 'as', 'at', 'be', 'by', 'can', 'for', 'from', 'have', 'if', 'in', 'is', 'it', 'may', 'not', 'of', 'on', 'or', 'tbd', 'that', 'the', 'this', 'to', 'us', 'we', 'when', 'will', 'with', 'yet', 'you', 'your'}), 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"]
参数
  • expression -- 用于提取标记的正则表达式模式。

  • stoplist -- 停止词列表。将此设置为“无”以禁用停止字筛选器。

  • minsize -- 小于此值的单词将从流中删除。

  • maxsize -- 将此从流中删除的时间更长的单词。

  • gaps -- 如果是,标记器 splits 在表达式上,而不是在表达式上匹配。

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() 检查给定语言是否具有词干函数和/或停止词列表。

参数
  • expression -- 用于提取标记的正则表达式模式。

  • gaps -- 如果是,标记器 splits 在表达式上,而不是在表达式上匹配。

  • cachesize -- 要缓存的词干字的最大数目。这个数字越大,词干速度越快,但使用的内存越多。

记号器

class whoosh.analysis.IDTokenizer

生成作为单个标记的整个输入字符串。用于索引但未命名的字段,如文档的路径。

>>> idt = IDTokenizer()
>>> [token.text for token in idt("/a/b 123 alpha")]
["/a/b 123 alpha"]
class 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"]
参数
  • expression -- 正则表达式对象或字符串。表达式的每个匹配项都等于一个标记。组0(整个匹配的文本)用作标记的文本。如果需要更复杂的表达式匹配处理,只需编写自己的标记器。

  • gaps -- 如果是,标记器 splits 在表达式上,而不是在表达式上匹配。

class 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"]
class 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组合起来。

参数
  • minsize -- N-grams的最小尺寸。

  • maxsize -- N-grams的最大尺寸。如果省略此参数,则maxSize==minSize。

class whoosh.analysis.PathTokenizer(expression='[^/]+')

一个简单的标记器,它给出了一个字符串 "/a/b/c" 生成令牌 ["/a", "/a/b", "/a/b/c"] .

过滤器

class whoosh.analysis.PassFilter

标识过滤器:不受影响地传递令牌。

class whoosh.analysis.LoggingFilter(logger=None)

打印作为调试日志项通过的每个筛选器的内容。

参数

target -- 要使用的记录器。如果省略,则使用“whoosh.analysis”记录器。

class 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)

此类期望模式属性的值在令牌流中的所有令牌之间都是一致的。

class 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"]
class whoosh.analysis.ReverseTextFilter

反转每个标记的文本。

>>> ana = RegexTokenizer() | ReverseTextFilter()
>>> [token.text for token in ana("hello there")]
["olleh", "ereht"]
class whoosh.analysis.LowercaseFilter

使用unicode.lower()将标记文本小写。

>>> rext = RegexTokenizer()
>>> stream = rext("This is a TEST")
>>> [token.text for token in LowercaseFilter(stream)]
["this", "is", "a", "test"]
class whoosh.analysis.StripFilter

对标记文本调用unicode.strip()。

class whoosh.analysis.StopFilter(stoplist=frozenset({'a', 'an', 'and', 'are', 'as', 'at', 'be', 'by', 'can', 'for', 'from', 'have', 'if', 'in', 'is', 'it', 'may', 'not', 'of', 'on', 'or', 'tbd', 'that', 'the', 'this', 'to', 'us', 'we', 'when', 'will', 'with', 'yet', 'you', 'your'}), 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() 检查给定语言是否有可用的停止词列表。

参数
  • stoplist -- 要从流中移除的单词集合。这将转换为冻结集。默认值是常用英文停止词列表。

  • minsize -- 令牌文本的最小长度。文本小于此值的令牌将停止。默认值为2。

  • maxsize -- 令牌文本的最大长度。文本大于此值的令牌将停止。不允许任何长度。

  • renumber -- 更改未停止标记的“pos”属性以反映其位置,同时删除停止的字。

  • lang -- 自动获取给定语言的停止词列表

class 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 提供对该库中的语言词干分析器的稍微简单的访问。

参数
  • stemfn -- 用于词干的函数。

  • lang -- 如果不是“无”,则使用来自 whoosh.lang.snowball 包裹。

  • ignore -- 不应该词干的单词集/列表。它被转换为冻结集。如果省略此参数,则所有标记都是词干的。

  • cachesize -- 要缓存的最大字数。使用 -1 对于无边界缓存,或 None 不需要缓存。

class 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字符的字典映射。

class 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"]
参数
  • minsize -- N-grams的最小尺寸。

  • maxsize -- N-grams的最大尺寸。如果省略此参数,则maxSize==minSize。

  • at -- 如果是“开始”,则只从每个单词的开始处取n克。如果“结束”,则只从每个单词的结尾处取n克。否则,从单词(默认值)中获取所有n-gram。

class whoosh.analysis.IntraWordFilter(delims='-_'"()!@#$%^&*[]{}<>\|;:, ./?`~=+', splitwords=True, splitnums=True, mergewords=False, mergenums=False)

将单词拆分为子字,并对子字组执行可选转换。这个过滤器的功能是基于solr中yonik的wordDelimiterFilter,但不共享任何代码。

  • 在字内分隔符上拆分,例如 Wi-Fi > Wi, Fi.

  • 当splitwords=true时,拆分大小写转换,例如 PowerShot > Power, Shot.

  • 当splitnums=true时,拆分字母数转换,例如 SD500 > SD, 500.

  • 忽略前导和尾随分隔符字符。

  • 从子命令中删除的尾随可能的“'s”,例如 O'Neil's > O, Neil.

mergewords和mergenums参数启用子命令的合并。

当合并参数为false时,子字不会合并。

  • PowerShot > 0: “权力”, 1: “射击”(在哪里) 01 are token positions).

当一个或两个合并参数都为真时,连续运行的字母和/或数字子字将合并到与最后一个子字位置相同的附加标记中。

  • PowerShot -> 0:Power, 1:Shot, 1:PowerShot

  • A's+B's&C's -> 0:A, 1:B, 2:C, 2:ABC

  • Super-Duper-XL500-42-AutoCoder! -> 0:Super, 1:Duper, 2:XL, 2:SuperDuperXL, 3:500, 4:42, 4:50042, 5:Auto, 6:Coder, 6:AutoCoder

当使用这个过滤器时,你应该使用一个只在空格上进行拆分的记号赋予器,这样记号赋予器在这个过滤器能看到它们之前不会删除单词内的定界符,并且在任何使用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

参数
  • delims -- 分隔符字符串。

  • splitwords -- 如果为真,则在案例转换时拆分,例如 PowerShot > Power, “射门”

  • splitnums -- 如果为真,则在字母数转换处拆分,例如 SD500 > SD, “500”

  • mergewords -- 将连续运行的字母子字合并到与最后一个子字位置相同的附加标记中。

  • mergenums -- 将连续运行的数字子字合并到与最后一个子字位置相同的附加标记中。

class 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"]
参数
  • wordset -- 带有 __contains__ 方法,如集合,包含要在标记内查找的字符串。

  • keep_compound -- 如果为真(默认值),则原始复合标记将保留在子命令之前的流中。

class 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) .

class 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 .

class 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

请注意,您需要确保标记器包含分隔符和数据作为标记的一部分!

参数
  • delimiter -- 一个字符串,当出现在令牌的文本中时,它将实际文本与“数据”有效负载分开。

  • attribute -- 要在令牌上存储数据的属性的名称。

  • default -- 用于没有分隔数据的标记的属性的值。

  • type -- 例如,数据的类型 strfloat . 这用于在将数据存储到属性中之前转换数据的字符串值。

class whoosh.analysis.DoubleMetaphoneFilter(primary_boost=1.0, secondary_boost=0.5, combine=False)

使用Lawrence Philips的双变音算法转换标记的文本。该算法试图对单词进行编码,使发音相似的单词减少为相同的代码。这可能对包含人名和地名的字段以及其他需要允许拼写差异的用途很有用。

参数
  • primary_boost -- 应用于包含主代码的令牌的增强。

  • secondary_boost -- 应用于包含辅助代码的令牌的提升(如果有)。

  • combine -- 如果为true,则原始未编码的标记将保留在流中,位于编码的标记之前。

class 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
参数
  • pattern -- 描述要替换的文本的模式字符串或已编译的正则表达式对象。

  • replacement -- 替换文本。

令牌类和函数

class 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.

参数
  • positions -- 标记是否应在“pos”属性中具有标记位置。

  • chars -- 标记是否应在“startchar”和“endchar”属性中具有字符偏移量。

  • removestops -- 是否从流中删除停止字(如果令牌通过停止筛选器)。

  • mode -- 包含一个字符串,描述分析器的调用目的,即“index”或“query”。

whoosh.analysis.unstopped(tokenstream)

从token.stopped=true的令牌流中删除令牌。