~landscape/zope3/ztk-1.1.3

« back to all changes in this revision

Viewing changes to src/zope/index/text/textindex.txt

  • Committer: Sidnei da Silva
  • Date: 2010-07-05 21:07:01 UTC
  • Revision ID: sidnei.da.silva@canonical.com-20100705210701-zmqhqrbzad1mhzsl
- Reduce deps

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Text Indexes
2
 
============
3
 
 
4
 
Text indexes combine an inverted index and a lexicon to support text
5
 
indexing and searching.  A text index can be created without passing
6
 
any arguments:
7
 
 
8
 
    >>> from zope.index.text.textindex import TextIndex
9
 
    >>> index = TextIndex()
10
 
 
11
 
By default, it uses an "Okapi" inverted index and a lexicon with a
12
 
pipeline consistening is a simple word splitter, a case normalizer,
13
 
and a stop-word remover.
14
 
 
15
 
We index text using the `index_doc` method:
16
 
 
17
 
    >>> index.index_doc(1, u"the quick brown fox jumps over the lazy dog")
18
 
    >>> index.index_doc(2,
19
 
    ...    u"the brown fox and the yellow fox don't need the retriever")
20
 
    >>> index.index_doc(3, u"""
21
 
    ... The Conservation Pledge
22
 
    ... =======================
23
 
    ... 
24
 
    ... I give my pledge, as an American, to save, and faithfully
25
 
    ... to defend from waste, the natural resources of my Country; 
26
 
    ... it's soils, minerals, forests, waters and wildlife.
27
 
    ... """)
28
 
    >>> index.index_doc(4, u"Fran\xe7ois") 
29
 
    >>> word = (
30
 
    ...     u"\N{GREEK SMALL LETTER DELTA}"
31
 
    ...     u"\N{GREEK SMALL LETTER EPSILON}"
32
 
    ...     u"\N{GREEK SMALL LETTER LAMDA}"
33
 
    ...     u"\N{GREEK SMALL LETTER TAU}"
34
 
    ...     u"\N{GREEK SMALL LETTER ALPHA}"
35
 
    ...     )
36
 
    >>> index.index_doc(5, word + u"\N{EM DASH}\N{GREEK SMALL LETTER ALPHA}")
37
 
    >>> index.index_doc(6, u"""
38
 
    ... What we have here, is a failure to communicate.
39
 
    ... """)
40
 
    >>> index.index_doc(7, u"""
41
 
    ... Hold on to your butts!
42
 
    ... """)
43
 
    >>> index.index_doc(8, u"""
44
 
    ... The Zen of Python, by Tim Peters
45
 
    ... 
46
 
    ... Beautiful is better than ugly.
47
 
    ... Explicit is better than implicit.
48
 
    ... Simple is better than complex.
49
 
    ... Complex is better than complicated.
50
 
    ... Flat is better than nested.
51
 
    ... Sparse is better than dense.
52
 
    ... Readability counts.
53
 
    ... Special cases aren't special enough to break the rules.
54
 
    ... Although practicality beats purity.
55
 
    ... Errors should never pass silently.
56
 
    ... Unless explicitly silenced.
57
 
    ... In the face of ambiguity, refuse the temptation to guess.
58
 
    ... There should be one-- and preferably only one --obvious way to do it.
59
 
    ... Although that way may not be obvious at first unless you're Dutch.
60
 
    ... Now is better than never.
61
 
    ... Although never is often better than *right* now.
62
 
    ... If the implementation is hard to explain, it's a bad idea.
63
 
    ... If the implementation is easy to explain, it may be a good idea.
64
 
    ... Namespaces are one honking great idea -- let's do more of those!
65
 
    ... """)
66
 
 
67
 
Then we can search using the apply method, which takes a search
68
 
string.
69
 
 
70
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u'brown fox').items()]
71
 
    [(1, '0.6153'), (2, '0.6734')]
72
 
 
73
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u'quick fox').items()]
74
 
    [(1, '0.6153')]
75
 
 
76
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u'brown python').items()]
77
 
    []
78
 
 
79
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u'dalmatian').items()]
80
 
    []
81
 
 
82
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u'brown or python').items()]
83
 
    [(1, '0.2602'), (2, '0.2529'), (8, '0.0934')]
84
 
 
85
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u'butts').items()]
86
 
    [(7, '0.6948')]
87
 
 
88
 
The outputs are mappings from document ids to float scores. Items
89
 
with higher scores are more relevent.
90
 
 
91
 
We can use unicode characters in search strings.
92
 
 
93
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(u"Fran\xe7ois").items()]
94
 
    [(4, '0.7427')]
95
 
 
96
 
    >>> [(k, "%.4f" % v) for (k, v) in index.apply(word).items()]
97
 
    [(5, '0.7179')]
98
 
 
99
 
We can use globbing in search strings.
100
 
 
101
 
    >>> [(k, "%.3f" % v) for (k, v) in index.apply('fo*').items()]
102
 
    [(1, '2.179'), (2, '2.651'), (3, '2.041')]
103
 
 
104
 
Text indexes support basic statistics:
105
 
 
106
 
    >>> index.documentCount()
107
 
    8
108
 
    >>> index.wordCount()
109
 
    114
110
 
 
111
 
If we index the same document twice, once with a zero value, and then
112
 
with a normal value, it should still work:
113
 
 
114
 
    >>> index2 = TextIndex()
115
 
    >>> index2.index_doc(1, [])
116
 
    >>> index2.index_doc(1, ["Zorro"])
117
 
    >>> [(k, "%.4f" % v) for (k, v) in index2.apply("Zorro").items()]
118
 
    [(1, '0.4545')]
119
 
 
120
 
 
121
 
Tracking Changes
122
 
================
123
 
 
124
 
If we index a document the first time it updates the _totaldoclen of
125
 
the underlying object.
126
 
 
127
 
    >>> index = TextIndex()
128
 
    >>> index.index._totaldoclen()
129
 
    0
130
 
    >>> index.index_doc(100, u"a new funky value")
131
 
    >>> index.index._totaldoclen()
132
 
    3
133
 
 
134
 
If we index it a second time, the underlying index length should not
135
 
be changed.
136
 
 
137
 
    >>> index.index_doc(100, u"a new funky value")
138
 
    >>> index.index._totaldoclen()
139
 
    3
140
 
 
141
 
But if we change it the length changes too.
142
 
 
143
 
    >>> index.index_doc(100, u"an even newer funky value")
144
 
    >>> index.index._totaldoclen()
145
 
    5
146
 
 
147
 
The same as for index_doc applies to unindex_doc, if an object is
148
 
unindexed that is not indexed no indexes chould change state.
149
 
 
150
 
    >>> index.unindex_doc(100)
151
 
    >>> index.index._totaldoclen()
152
 
    0
153
 
 
154
 
    >>> index.unindex_doc(100)
155
 
    >>> index.index._totaldoclen()
156
 
    0