Package: ngramratio
python-ngramratioA method for similarity scoring of two strings.The method, namelynratio, belongs to the classSequenceMatcherExtended, which is an extension of theSequenceMatcherclass of thedifflib package. In particular,nratio(method ofSequenceMatcherExtended) is an augmenation ofratio(method ofSequenceMatcher).ngramratiois to be pronounced as "n gram ratio". The library uses n-grams to find a similarity score via a division (ratio) of the number of matched characters by the total number of characters. See below for more details.MotivationTo compute a similarity score based on matching n-grams (with n>=1 chosen by the user) rather than matching single characters (as in the case of theratiomethod).InstallationTo install the Python library run:pip install ngramratioThe library will be installed asngramratiotobinon
Linux (e.g./usr/bin); or asngramratio.exetoScriptsin your
Python installation on Windows (e.g.C:\Python27\Scripts\ngramratio.exe).You may consider installing the library only for the current user:pip install ngramratio --userIn this case the library will be installed to~/.local/bin/ngramratioon Linux and to%APPDATA%\Python\Scripts\ngramratio.exeon Windows.Library usageThe module provides a method,nratio, which takes an integer number (the user's required minimum n-gram length, i.e. number of consecutive characters, to be matched) and outputs a similarity index (float number in [0,1]).First step: initialize an object of class SequenceMatcherExtended specifying the two strings to be compared:>>> from ngramratio import ngramratio

    >>> SequenceMatcherExtended = ngrmaratio.SequenceMatcherExtended

    >>> a = "ab cde" # string 1
    >>> b = "bcde"   # string 2

    >>> s = SequenceMatcherExtended(a, b)Alternatively, the last line can be rewritten more generally as>>> s = SequenceMatcherExtended(None, a, b, None)where the first and last arguments are used to specify that no string will be considered junk. For more information on these arguments, see the documentation of the originaldifflib package.Second step: apply theratioandnratiomethods and compare similarity scores:>>> s.ratio()
    >>> # Matches any character. Matches: "b" (length 1), "cde"(length 3). Score: (3+1)*2/10.
    0.8
    >>> s.nratio(1)
    >>> # Matches substring of length 1 or more. It replicates `ratio()`'s functionality.
    0.8
    >>> s.nratio(2)
    >>> # Matches substring of length 2 or more. Matches: "cde"(length 3). Score: 3*2/10.
    0.6
    >>> s.nratio(3)
    >>> # Matches substring of length 3 or more. Matches: "cde"(length 3). Score: 3*2/10.
    0.6
    >>> s.nratio(4)
    >>> # Matches substring of length 4 or more. Score 0/10.
    0.0The similarity score is computed asthe number of characters matched(m) mutiplied bytwo(2) and divided bythe total numer of characters(T) of the two strings, i.e. similarity score = 2m/T. Note that Python always returns a float upon computing a division.Testing in a virtual environmentThis project usespytesttesting
framework withtoxanddockerto automate testing in
different python environments. Tests are stored in thetest/folder.To test a specific python version, for example version 3.6, edit the last few characters of thestartTest.shscript topy36AND change the image to python 3.6 on line 4 of thedocker-compose.yamlfile.To run tests, runbash _scripts/startTest.sh. This will start a docker container using the specified python image. After testing, or before testing a different python version, runbash _scripts/teardown.shto remove the docker container.The library has been tested successfully for python >= 3.6.Testing on your local machine with no v.e.You can usetoxdirectly in your local machine. Make sure to installtox,pytestbefore testing.On Linuxtoxexpects to find executables likepython3.6,python3.10etc. On Windows it looks forC:\Python36\python.exeandC:\Python310\python.exerespectively.To test a specific Python environment, use the-eoption. For example, to
test against Python 3.7 run:tox -e py37in the root of the project source tree.To fix code formatting (this will installpre-commitas a dependency), run:tox -e lintSee thetox.inifile in the repository to learn more about the testing instructions being used.ContributionsContributions should include tests and an explanation for the changes
they propose. Documentation (examples, docstrings, README.md) should be
updated accordingly.