-
Notifications
You must be signed in to change notification settings - Fork 11
/
cfg_util.py
63 lines (52 loc) · 1.74 KB
/
cfg_util.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
54
55
56
57
58
59
60
61
62
63
import nltk
import numpy as np
import zinc_grammar
def get_zinc_tokenizer(cfg):
long_tokens = [a for a in cfg._lexical_index.keys() if len(a) > 1]
replacements = ['$', '%', '^']
assert len(long_tokens) == len(replacements)
for token in replacements:
assert token not in cfg._lexical_index
def tokenize(smiles):
for i, token in enumerate(long_tokens):
smiles = smiles.replace(token, replacements[i])
tokens = []
for token in smiles:
try:
ix = replacements.index(token)
tokens.append(long_tokens[ix])
except Exception:
tokens.append(token)
return tokens
return tokenize
def encode(smiles):
GCFG = zinc_grammar.GCFG
tokenize = get_zinc_tokenizer(GCFG)
tokens = tokenize(smiles)
parser = nltk.ChartParser(GCFG)
# if you use Python 2: parse_tree = parser.parse(tokens).next()
parse_tree = parser.parse(tokens).__next__()
productions_seq = parse_tree.productions()
productions = GCFG.productions()
prod_map = {}
for ix, prod in enumerate(productions):
prod_map[prod] = ix
indices = np.array([prod_map[prod] for prod in productions_seq], dtype=int)
return indices
def prods_to_eq(prods):
seq = [prods[0].lhs()]
for prod in prods:
if str(prod.lhs()) == 'Nothing':
break
for ix, s in enumerate(seq):
if s == prod.lhs():
seq = seq[:ix] + list(prod.rhs()) + seq[ix+1:]
break
try:
return ''.join(seq)
except Exception:
return ''
def decode(rule):
productions = zinc_grammar.GCFG.productions()
prod_seq = [productions[i] for i in rule]
return prods_to_eq(prod_seq)