-
Notifications
You must be signed in to change notification settings - Fork 30
/
hash.py
31 lines (24 loc) · 593 Bytes
/
hash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Copyright (c) 2018-present. Ben Athiwaratkun
All rights reserved.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
"""
import subprocess
import numpy as np
# BenA:
# the constant values here reflect the values in original FastText implementation
BOW = "<"
EOW = ">"
M32 = 0xffffffffL
def m32(n):
return n & M32
def mmul(a, b):
return m32(a*b)
def hash(str):
h = m32(2166136261L)
for c in str:
cc = m32(long(ord(c)))
h = m32(h ^ cc)
h = mmul(h, 16777619L)
return h