-
Notifications
You must be signed in to change notification settings - Fork 1
/
stress_utils.py
55 lines (40 loc) · 1.75 KB
/
stress_utils.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import pandas as pd
from urllib.request import urlretrieve
PATH_SPHINX_STRESS_DICT = "sphinx_stress.txt"
URL_SPHINX_STRESS_DICT = 'https://github.com/zamiron/ru4sphinx/blob/master/text2dict/all_form.txt?raw=true'
def add_stress(phrase_ser, u_ser):
"""
Adds stress to each word in each row of phrase_ser. Use u_ser as stress dictionary.
"""
return phrase_ser.str.split(' ').apply(lambda wl: ' '.join((u_ser.get(w, default=w) for w in wl)))
def load_stress_dict():
"""
Loads stress dict.
:return: pandas Series with plain tokens as index and tokens with stress as values
"""
return get_ser(load_stress2())
def load_stress1():
ud1 = pd.read_csv('./stress.txt.gz',header=None,sep='\t',names=['stok'])
ud1['tok']=ud1.stok.str.replace("`",'')
ud1['stok'] = ud1.stok.str.replace(r'(.)`',r'+\1')
return ud1
def load_stress2():
URL, PATH = URL_SPHINX_STRESS_DICT, PATH_SPHINX_STRESS_DICT
if not os.path.exists(PATH_SPHINX_STRESS_DICT):
print('Downloading stress dictionary from %s ...' % URL)
urlretrieve(URL, PATH)
print('Stress dictionary saved to %s' % PATH)
ud2 = pd.read_csv(PATH_SPHINX_STRESS_DICT, sep=' ', header=None, names=['tok','stok'])
l = len(ud2)
assert l > 1.5e6, "Stress dictionary %s has only %d entries, it is probably corrupted. " \
"Try download it manually from %s" % (PATH, l, URL)
return ud2
def get_ser(udf):
udf = udf.drop_duplicates().groupby('tok').agg(['count','first'])
mask = udf['stok']['count'] == 1
print('%f %% tokens have unambiguous stress' % (100*mask.mean()))
udf = udf[mask] # use only unambiguous entries
udf.columns = [x[-1] for x in udf.columns.ravel()]
u_ser = udf['first']
return u_ser