little question

Philip Swartzleonard starx at pacbell.net
Fri May 10 06:32:33 EDT 2002


Alex Martelli || Fri 10 May 2002 03:20:48a:

> shagshag wrote:
> 
>> Does anyone knows where to find (a) python module(s) to handle
>> inverted index and build it from text/xml file ?
> 
> I'm not sure what an *inverted* index is.  A dictionary indexed by
> word and for each word giving the set of (filename,linenumber) where
> the word appears appears as a tiny example in my Linux Magazine
> article, April issue -- a few lines to build it, a few to use it for
> queries "where is this word found".  But that's what I'd call an
> index, nothing 'inverted' about it, so I don't know what you need.

Hm, I don't get it either. But it sounds like the answer would be
something like this: 

Build a dictionary that repersents whatever you are calling an 'index'.

Then... do something like this:

invindex = {}
for key in index:
    	data = index[key]
    	invindex[data] = key

Note that this will fail silently if the index is not a 1:1 mapping, or,
that is, if any two keys have the same data. (If you know the vertical
line test for functions, think of that for dictionarys). You would have
to do something special for that, like throw an exception or make that
data map to a list of two keys. 


-- 
Philip Sw "Starweaver" [rasx] :: www.rubydragon.com



More information about the Python-list mailing list