Skip to main content

Python library for fast substring/pattern search written in C++ leveraging Suffix Array Algorithm

Project description

Logo

Python library for fast substring/pattern search written in C++ leveraging Suffix Array Algorithm

license Python Build PyPi

Table of Contents

About The Project

PySubstringSearch is a library intended for searching over an index file for substring patterns. The library is written in C++ to achieve speed and efficiency. The library also uses Msufsort suffix array construction library for string indexing. The created index consists of the original text and a 32bit suffix array structs. The library relies on a proprietary container protocol to hold the original text along with the index in chunks of 512mb to evade the limitation of the Suffix Array Construction implementation.

The module implements multiple methods.

  • search - search concurrently for a substring existed in different entries within the index file. As the index file getting bigger with multiple inner chunks, the concurrency effect increases.
  • count_entries - return the number of entries in the index file consisting of the substring.
  • count_occurrences - return the number of occurrences of the substring in all the entries. If the substring exists multiple times in the same entry, each occurrence will be counted.

Built With

Performance

500MB File

Library Function Time #Results Improvement Factor
ripgrepy Ripgrepy('text_one', '500mb').run().as_string.split('\n') 148ms 2367 1.0x
PySubstringSearch reader.search('text_one') 1.28ms 2367 115.6x
ripgrepy Ripgrepy('text_two', '500mb').run().as_string.split('\n') 116ms 159 1.0x
PySubstringSearch reader.search('text_two') 228µs 159 508.7x

6000MB File

Library Function Time #Results Improvement Factor
ripgrepy Ripgrepy('text_one', '6000mb').run().as_string.split('\n') 2.4s 59538 1.0x
PySubstringSearch reader.search('text_one') 15.4ms 59538 155.8x
ripgrepy Ripgrepy('text_two', '6000mb').run().as_string.split('\n') 1.5s 7266 1.0x
PySubstringSearch reader.search('text_two') 1.97ms 7266 761.4x

Prerequisites

In order to compile this package you should have GCC & Python development package installed.

  • Fedora
sudo dnf install python3-devel gcc-c++
  • Ubuntu 18.04
sudo apt install python3-dev g++-9

Installation

pip3 install PySubstringSearch

Usage

Create an index

import pysubstringsearch

# creating a new index file
# if a file with this name is already exists, it will be overwritten
writer = pysubstringsearch.Writer(
    index_file_path='output.idx',
)

# adding entries to the new index
writer.add_entry('some short string')
writer.add_entry('another but now a longer string')
writer.add_entry('more text to add')

# making sure the data is dumped to the file
writer.finalize()

Search a substring within an index

import pysubstringsearch

# opening an index file for searching
reader = pysubstringsearch.Reader(
    index_file_path='output.idx',
)

# lookup for a substring
reader.search('short')
>>> ['some short string']

# lookup for a substring
reader.search('string')
>>> ['some short string', 'another but now a longer string']

# count the number of occurrences
# ['some short string', 'another string now, but a longer string']
reader.count_occurences('string')
>>> 3

# count the number of entries
# ['some short string', 'another string now, but a longer string']
reader.count_occurences('string')
>>> 2

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Gal Ben David - gal@intsights.com

Project Link: https://github.com/Intsights/PySubstringSearch

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

PySubstringSearch-0.4.0.tar.gz (216.8 kB view hashes)

Uploaded Source

Supported by

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