Skip to main content

No project description provided

Project description

PyPI

KeywordTree

A Python data structure that maps arbitrary text by using a decision-tree-like structure, based on counting keyword occurrences.

KeywordTree

How it works

  • Each node contains a list of keywords and a value if it's a leaf.
  • A query is given as a string and is broken into tokens by a tokenizer.
  • Starting from the root, the query follows the child with the most keywords present in the query token list.
  • When a leaf is reached, its value is returned.
  • If there are multiple nodes with the most keywords in the token list, the query follows all of them and the results are merged at the end into a list.

Example

Suppose we have the above tree and the query "My pug is so young it can barely woof".

The default tokenizer converts the query to lowercase and splits at whitespaces, so the tokens will be ['my', 'pug', 'is', 'so', 'young', 'it', 'can', 'barely','woof'].

The query will follow the left child of the root, since it has one match for 'woof' and will then match the child with 'pug' in its keywords. Since it's a leaf, its value will be in the final result: ['Pug'].

Usage

KeywordTree instances are constructed from tuples of the form (list of keywords, list of children described as tuples, value).

Non-leaf nodes must have value equal to None.

from keywordtree import KeywordTree

kt = KeywordTree([], [
    (['dog', 'woof'], [
        (['bulldog'], [], 'Bulldog'),
        (['pug'], [], 'Pug'),
        (['boxer'], [], 'Boxer'),
    ], None),
    (['cat', 'meow'], [
        (['persian'], [], 'Persian'),
        (['bengal'], [], 'Bengal'),
        (['siamese'], [], 'Siamese'),
    ], None),
], None)

kt.query('My pug is so young it can barely woof')
# ['Pug']

Other tokenizers can be used as well:

from keywordtree import KeywordTree
from nltk.tokenize.stanford import StanfordTokenizer

stftk = StanfordTokenizer()

def tokenizer(s):
    return stftk.tokenize(s)

kt = KeywordTree([], [
    # list of children
], None, tokenizer=tokenizer)

License

Copyright 2019 Theodoros Diamantidis

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

keywordtree-0.1.2.tar.gz (3.1 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