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.
Github repo: stretree
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
- Use PyPI:
pip install strtree
- Use a distribution file located in the
dist
folder:pip install strtree-0.1.0-py3-none-any.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
Built Distribution
File details
Details for the file strtree-0.2.0.tar.gz
.
File metadata
- Download URL: strtree-0.2.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d08f82fda856926de7cb18c1e2188f579b37ddef731259e782e18126fa0b5e92 |
|
MD5 | 093e7d6c2d84fe633d314db92d6d8341 |
|
BLAKE2b-256 | 35729144122bc19f6da03b48e4e2f1dc7b12932213d8e95bdba7148a4a580313 |
File details
Details for the file strtree-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: strtree-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b1d341955334273473baa99ae39fde9d13f5fcbba6c41dec94c5dbe724a2f449 |
|
MD5 | 8a3baf144f67469b529ccf9f900d3428 |
|
BLAKE2b-256 | 9d40732535b37308f901e8d7535c1665286be507516359ffc0fafed9be7afbe6 |