Skip to main content

Python package for strings binary classification, based on regular expressions put in a decision tree.

Project description

Basics to strtree

strtree is a Python package for strings binary classification, based on regular expressions put in a decision tree.

With strtree you can:

  • Do a binary classification of your strings using automatically extracted regular expressions
  • Find shortest regular expressions which covers strings with positive labels in the most accurate way

Look at a quick example.

Example

Firstly, let's build a tree from strings and their labels.

import strtree


strings = ['Samsung X-500', 'Samsung SM-10', 'Samsung X-1100', 'Samsung F-10', 'Samsung X-2200',
           'AB Nokia 1', 'DG Nokia 2', 'THGF Nokia 3', 'SFSD Nokia 4', 'Nokia XG', 'Nokia YO']
labels = [1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0]

tree = StringTree()
tree.build(strings, labels, min_precision=0.75, min_token_length=1)

Let's see what regular expressions were extracted.

for leaf in tree.leaves:
    print(leaf)

# Output:
# PatternNode(".+ .+a.+", right=None, left=PatternNode(.+0.+), n_strings=11, precision=1.0, recall=0.57)
# PatternNode(".+0.+", right=None, left=None, n_strings=7, precision=1.0, recall=1.0)

You may need to check the precision and recall of the whole tree for a given set of strings and true labels.

print('Precision: {}'.format(tree.precision_score(strings, labels)))
# Precision: 1.0

print('Recall: {}'.format(tree.precision_score(strings, labels)))
# Recall: 1.0

Finally, you can pass any strings you want and see if they match to extracted regular expressions or not.

matches = tree.match(other_strings)

# You will receive a vector of the same size as other_strings containing 0's (no match) or 1's (match)

Installing

  1. Use PyPI: pip install strtree
  2. Use a distribution file located in the dist folder: pip install file.whl

Contribution

You are very welcome to participate in the project. You may solve the current issues or add new functionality - it is up to you to.

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

strtree-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

strtree-0.1.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file strtree-0.1.0.tar.gz.

File metadata

  • Download URL: strtree-0.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.0

File hashes

Hashes for strtree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1db1c61ed1be69330fed95d041fbeea0128453b9d28dae229864f9b83c3fede7
MD5 f8df65c5b427739e88926eda515d8d64
BLAKE2b-256 f64ac1ea74f8ee45d7dcf95cc66fe64508c91499b8c095eae6bc5c7dcf6c200b

See more details on using hashes here.

File details

Details for the file strtree-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: strtree-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.0

File hashes

Hashes for strtree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4a63e45ecabe0bdc4c3f0473d5b0ca24393d76f2d49d833c4a767bafcfc683d
MD5 e496ee558904499a5e8490d7d109c970
BLAKE2b-256 388ea205c8a6178ddb2c0bd88e3b1006a74d02dd43b3040d6a71a2c5c28317bf

See more details on using hashes here.

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