forked from j-luo93/NeuroDecipher
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
2,804 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*.cover | ||
.hypothesis/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# dotenv | ||
.env | ||
|
||
# virtualenv | ||
.venv | ||
venv/ | ||
ENV/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# mypy | ||
.mypy_cache/ | ||
|
||
# my stuff | ||
data/* | ||
.pytest_cache/* | ||
log/* | ||
.testmondata | ||
.tmontmp | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
es fr en | ||
es1 fr1 en1 | ||
es2 fr2|fr3 en2 | ||
es3 fr3 en3 | ||
es4 _ en4 | ||
_ fr4 en2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Hiroyuki Tanaka | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
============ | ||
editdistance | ||
============ | ||
|
||
Fast implementation of the edit distance (Levenshtein distance). | ||
|
||
This library simply implements `Levenshtein distance <http://en.wikipedia.org/wiki/Levenshtein_distance>`_ with C++ and Cython. | ||
|
||
The algorithm used in this library is proposed by | ||
`Heikki Hyyrö, "Explaining and extending the bit-parallel approximate string matching algorithm of Myers", (2001) <http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.19.7158&rep=rep1&type=pdf>`_. | ||
|
||
------------- | ||
Binary wheels | ||
------------- | ||
|
||
Thanks to `joerick/cibuildwheel <https://github.com/joerick/cibuildwheel>`_, | ||
There are binary wheels on Linux, Mac OS, and Windows. | ||
|
||
------- | ||
Install | ||
------- | ||
|
||
You can install via pip: | ||
|
||
.. code-block:: bash | ||
pip install editdistance | ||
----- | ||
Usage | ||
----- | ||
|
||
It's quite simple: | ||
|
||
.. code-block:: python | ||
import editdistance | ||
editdistance.eval('banana', 'bahama') | ||
# 2L | ||
---------------- | ||
Simple Benchmark | ||
---------------- | ||
|
||
With IPython, I tried several libraries: | ||
|
||
* `pyxDamerauLevenshtein <https://pypi.python.org/pypi/pyxDamerauLevenshtein>`_ | ||
* `pylev <https://pypi.python.org/pypi/pylev>`_ | ||
* `python-Levenshtein <https://pypi.python.org/pypi/python-Levenshtein>`_ | ||
|
||
On Python 2.7.5: | ||
|
||
.. code-block:: python | ||
a = 'fsffvfdsbbdfvvdavavavavavava' | ||
b = 'fvdaabavvvvvadvdvavavadfsfsdafvvav' | ||
import pylev | ||
timeit pylev.levenshtein(a, b) | ||
# 100 loops, best of 3: 7.48 ms per loop | ||
from pyxdameraulevenshtein import damerau_levenshtein_distance | ||
timeit damerau_levenshtein_distance(a, b) | ||
# 100000 loops, best of 3: 11.4 µs per loop | ||
timeit editdistance.eval(a, b) # my library | ||
# 100000 loops, best of 3: 3.5 µs per loop | ||
import Levenshtein | ||
timeit Levenshtein.distance(a, b) | ||
# 100000 loops, best of 3: 3.21 µs per loop | ||
------------------------ | ||
Distance with Any Object | ||
------------------------ | ||
|
||
Above libraries only support strings. | ||
But Sometimes other type of objects such as list of strings(words). | ||
I support any iterable, only requires hashable object of it: | ||
|
||
.. code-block:: python | ||
Levenshtein.distance(['spam', 'egg'], ['spam', 'ham']) | ||
# --------------------------------------------------------------------------- | ||
# TypeError Traceback (most recent call last) | ||
# <ipython-input-22-3e0b30d145ac> in <module>() | ||
# ----> 1 Levenshtein.distance(['spam', 'egg'], ['spam', 'ham']) | ||
# | ||
# TypeError: distance expected two Strings or two Unicodes | ||
editdistance.eval(['spam', 'egg'], ['spam', 'ham']) | ||
# 1L | ||
So if object's hash is same, it's same. | ||
You can provide ``__hash__`` method to your object instances. | ||
|
||
Enjoy! | ||
|
||
|
||
------- | ||
License | ||
------- | ||
|
||
It is released under the MIT license. | ||
|
||
Copyright (c) 2013 Hiroyuki Tanaka | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from .bycython import eval, eval_all | ||
__all__ = ('eval', 'eval_all') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// ------- | ||
// License | ||
// ------- | ||
// | ||
// It is released under the MIT license. | ||
// | ||
// Copyright (c) 2013 Hiroyuki Tanaka | ||
// | ||
// 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. | ||
|
||
|
||
#include <cstdlib> | ||
#include <cstring> | ||
#include <string> | ||
#include <map> | ||
#include <vector> | ||
#include <iostream> | ||
#include <bitset> | ||
#include <algorithm> | ||
|
||
#include "./_editdistance.h" | ||
|
||
using namespace std; | ||
|
||
template<typename T, typename TVALUE> | ||
unsigned int edit_distance_bpv(T &cmap, int64_t const *vec, size_t const &vecsize, unsigned int const &tmax, unsigned int const &tlen) { | ||
int D = tmax * 64 + tlen; | ||
TVALUE D0, HP, HN, VP, VN; | ||
uint64_t top = (1LL << (tlen - 1)); // 末尾のvectorに適用 | ||
uint64_t lmb = (1LL << 63); | ||
|
||
for(size_t i = 0; i <= tmax; ++i) { | ||
VP[i] = 0; | ||
VN[i] = 0; | ||
} | ||
for(size_t i = 0; i < tmax; ++i) VP[i] = ~0; | ||
for(size_t i = 0; i < tlen; ++i) VP[tmax] |= (1LL << i); | ||
for(size_t i = 0; i < vecsize; ++i) { | ||
TVALUE &PM = cmap[vec[i]]; | ||
for(int r = 0; r <= tmax; ++r) { | ||
uint64_t X = PM[r]; | ||
if(r > 0 && (HN[r - 1] & lmb)) X |= 1LL; | ||
D0[r] = (((X & VP[r]) + VP[r]) ^ VP[r]) | X | VN[r]; | ||
HP[r] = VN[r] | ~(D0[r] | VP[r]); | ||
HN[r] = D0[r] & VP[r]; | ||
X = (HP[r] << 1LL); | ||
if(r == 0 || HP[r - 1] & lmb) X |= 1LL; | ||
VP[r] = (HN[r] << 1LL) | ~(D0[r] | X); | ||
if(r > 0 && (HN[r - 1] & lmb)) VP[r] |= 1LL; | ||
VN[r] = D0[r] & X; | ||
} | ||
if(HP[tmax] & top) ++D; | ||
else if(HN[tmax] & top) --D; | ||
} | ||
return D; | ||
} | ||
|
||
|
||
/// c.f. http://handasse.blogspot.com/2009/04/c_29.html | ||
template<typename T> | ||
unsigned int edit_distance_dp(T const *str1, size_t const size1, T const *str2, size_t const size2) { | ||
// vectorより固定長配列の方が速いが、文字列が長い時の保険でのみ呼ばれるのでサイズを決め打ちできない | ||
vector< vector<uint32_t> > d(size1 + 1, vector<uint32_t>(size2 + 1)); | ||
for (int i = 0; i < size1 + 1; i++) d[i][0] = i; | ||
for (int i = 0; i < size2 + 1; i++) d[0][i] = i; | ||
for (int i = 1; i < size1 + 1; i++) { | ||
for (int j = 1; j < size2 + 1; j++) { | ||
// original | ||
// d[i][j] = min(min(d[i-1][j], d[i][j-1]) + 1, d[i-1][j-1] + (str1[i-1] == str2[j-1] ? 0 : 1)); | ||
// smaller cost for insertion | ||
// d[i][j] = min(min(d[i-1][j] + 1, d[i][j-1] + 1), d[i-1][j-1] + (str1[i-1] == str2[j-1] ? 0 : 2)); | ||
// smaller cost for insertion/deletion | ||
d[i][j] = min(min(d[i-1][j], d[i][j-1]) + 1, d[i-1][j-1] + (str1[i-1] == str2[j-1] ? 0 : 2)); | ||
} | ||
} | ||
return d[size1][size2]; | ||
} | ||
|
||
template <size_t N> | ||
struct varr { | ||
uint64_t arr_[N]; | ||
uint64_t & operator[](size_t const &i) { | ||
return arr_[i]; | ||
} | ||
}; | ||
|
||
|
||
template<size_t N> | ||
unsigned int edit_distance_map_(int64_t const *a, size_t const asize, int64_t const *b, size_t const bsize) { | ||
typedef map<int64_t, varr<N> > cmap_v; | ||
cmap_v cmap; | ||
unsigned int tmax = (asize - 1) >> 6; | ||
unsigned int tlen = asize - tmax * 64; | ||
for(size_t i = 0; i < tmax; ++i) { | ||
for(size_t j = 0; j < 64; ++j) cmap[a[i * 64 + j]][i] |= (1LL << j); | ||
} | ||
for(size_t i = 0; i < tlen; ++i) cmap[a[tmax * 64 + i]][tmax] |= (1LL << i); | ||
return edit_distance_bpv<cmap_v, typename cmap_v::mapped_type>(cmap, b, bsize, tmax, tlen); | ||
} | ||
|
||
|
||
unsigned int edit_distance(const int64_t *a, const unsigned int asize, const int64_t *b, const unsigned int bsize) { | ||
if(asize == 0) return bsize; | ||
else if(bsize == 0) return asize; | ||
// 要素数の大きいほうがa | ||
int64_t const *ap, *bp; | ||
unsigned int const *asizep, *bsizep; | ||
// don't make it symmetric | ||
// ap = a, bp = b, asizep = &asize, bsizep = &bsize; | ||
if(asize < bsize) ap = b, bp = a, asizep = &bsize, bsizep = &asize; | ||
else ap = a, bp = b, asizep = &asize, bsizep = &bsize; | ||
// 必要な配列サイズを調べる | ||
size_t vsize = ((*asizep - 1) >> 6) + 1; // 64までは1, 128までは2, ... | ||
// bit-parallelでできそうな限界を超えたら要素数の小さい方をaとする。 | ||
if(vsize > 10) { | ||
int64_t const *_ = ap; | ||
unsigned int const *__ = asizep; | ||
ap = bp, bp = _, asizep = bsizep, bsizep = __; | ||
vsize = ((*asizep - 1) >> 6) + 1; | ||
} | ||
|
||
// if(vsize == 1) return edit_distance_map_<1>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 2) return edit_distance_map_<2>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 3) return edit_distance_map_<3>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 4) return edit_distance_map_<4>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 5) return edit_distance_map_<5>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 6) return edit_distance_map_<6>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 7) return edit_distance_map_<7>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 8) return edit_distance_map_<8>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 9) return edit_distance_map_<9>(ap, *asizep, bp, *bsizep); | ||
// else if(vsize == 10) return edit_distance_map_<10>(ap, *asizep, bp, *bsizep); | ||
return edit_distance_dp<int64_t>(ap, *asizep, bp, *bsizep); // dynamic programmingに任せる | ||
} |
Oops, something went wrong.