Skip to main content

InvertedIndex implementation using hash lists (dictionaries)

Project description

TravisCI AppVeyor CodeCov PyPi

Fast and simple InvertedIndex implementation using hash lists (python dictionaries).

Free software: BSD license

Installing

The easiest way to install hashindex is through pypi

pip install hashedindex

Features

hashedindex provides a simple to use inverted index structure that is flexible enough to work with all kinds of use cases.

Basic Usage:

import hashedindex
index = hashedindex.HashedIndex()

index.add_term_occurrence('hello', 'document1.txt')
index.add_term_occurrence('world', 'document1.txt')

index.get_documents('hello')
Counter({'document1.txt': 1})

index.items()
{'hello': Counter({'document1.txt': 1}),
'world': Counter({'document1.txt': 1})}

example = 'The Quick Brown Fox Jumps Over The Lazy Dog'

for term in example.split():
    index.add_term_occurrence(term, 'document2.txt')

hashedindex is not limited to strings, any hashable object can be indexed.

index.add_term_occurrence('foo', 10)
index.add_term_occurrence(('fire', 'fox'), 90.2)

index.items()
{'foo': Counter({10: 1}), ('fire', 'fox'): Counter({90.2: 1})}

Text Parsing

The hashedindex module comes included with a powerful textparser module with methods to split text into tokens.

from hashedindex import textparser
list(textparser.word_tokenize("hello cruel world"))
[('hello',), ('cruel',), ('world',)]

Tokens are wrapped within tuples due to the ability to specify any number of n-grams required:

list(textparser.word_tokenize("Life is about making an impact, not making an income.", ngrams=2))
[(u'life', u'is'), (u'is', u'about'), (u'about', u'making'), (u'making', u'an'), (u'an', u'impact'),
 (u'impact', u'not'), (u'not', u'making'), (u'making', u'an'), (u'an', u'income')]

Take a look at the function’s docstring for information on how to use stopwords, specify a min_length or ignore_numeric terms.

Integration with Numpy and Pandas

The idea behind hashedindex is to provide a really quick and easy way of generating matrices for machine learning with the additional use of numpy, pandas and scikit-learn. For example:

from hashedindex import textparser
import hashedindex
import numpy as np

index = hashedindex.HashedIndex()

documents = ['spam1.txt', 'ham1.txt', 'spam2.txt']
for doc in documents:
    with open(doc, 'r') as fp:
         for term in textparser.word_tokenize(fp.read()):
             index.add_term_occurrence(term, doc)

# You *probably* want to use scipy.sparse.csr_matrix for better performance
X = np.as_matrix(index.generate_feature_matrix(mode='tfidf'))

y = []
for doc in index.documents():
    y.append(1 if 'spam' in doc else 0)
y = np.asarray(doc)

from sklearn.svm import SVC
classifier = SVC(kernel='linear')
classifier.fit(X, y)

You can also extend your feature matrix to a more verbose pandas DataFrame:

import pandas as pd
X  = index.generate_feature_matrix(mode='tfidf')
df = pd.DataFrame(X, columns=index.terms(), index=index.documents())

All methods within the code have high test coverage so you can be sure everything works as expected.

Reporting Bugs

Found a bug? Nice, a bug found is a bug fixed. Open an Issue or better yet, open a pull request.

History

0.1.0 (2015-01-11)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hashedindex-0.4.2.tar.gz (20.6 kB view details)

Uploaded Source

File details

Details for the file hashedindex-0.4.2.tar.gz.

File metadata

  • Download URL: hashedindex-0.4.2.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hashedindex-0.4.2.tar.gz
Algorithm Hash digest
SHA256 480a9274ff4db1bc98dab0a414757e7d97b30f48e6b8ad1db7cc97e1f6ef03ca
MD5 66bba344df4949f6c019f0022d1fc89a
BLAKE2b-256 83fae1a6fe41af326017fc3e1aa756708512fa4b54038239cbcd97f3567bba3d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page