Download pool/shard001/Python/0009161_util.py from Variable-role/sajaniemi_variable_dataset_large: direct link, hf CLI and curl.
- Browser
- Download file 1.04 kB
-
https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard001/Python/0009161_util.py
- Command line
-
hf download hf://datasets/Variable-role/sajaniemi_variable_dataset_large/pool/shard001/Python/0009161_util.py
-
curl -L -o 0009161_util.py https://huggingface.co/datasets/Variable-role/sajaniemi_variable_dataset_large/resolve/main/pool/shard001/Python/0009161_util.py
1.04 kB
| from heapq import merge as _merge | |
| KMER_SIZE = 3 | |
| STEP_SIZE = 1 | |
| def avg(l): | |
| return sum(l)/len(l) | |
| def ngrams(s, k=KMER_SIZE, step=STEP_SIZE): | |
| if len(s) < k: | |
| return [s] | |
| return [s[i:i+k] for i in range(0, len(s)-k+1, step)] | |
| def jaccard(a, b): | |
| if not a and not b: | |
| return 0 | |
| n = len(a.intersection(b)) | |
| return 1 - (float(n) / (len(a) + len(b) - n)) | |
| def openfile(filename_or_handle, mode='r'): | |
| if type(filename_or_handle) is str: | |
| return open(filename_or_handle, mode) | |
| return filename_or_handle | |
| def merge_to_range(ls): | |
| if hasattr(ls, "__len__") and len(ls) == 1: | |
| merged = iter(ls[0]) | |
| else: | |
| merged = _merge(*ls) | |
| ret = [next(merged)] | |
| prev = ret[0] | |
| compacted = False | |
| for x in merged: | |
| if x == prev + 1: | |
| if type(ret[-1]) is list: | |
| ret[-1][-1] = x | |
| else: | |
| ret[-1] = [prev, x] | |
| compacted = True | |
| else: | |
| ret.append(x) | |
| prev = x | |
| return ret, compacted | |