Skip to content

Commit

Permalink
Move search for dot symbol in try-except
Browse files Browse the repository at this point in the history
  • Loading branch information
vandrw committed Nov 19, 2023
1 parent 1e60b91 commit 1d22f1d
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions selfies/utils/encoding_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,20 @@ def selfies_to_encoding(
selfies += "[nop]" * (pad_to_len - len_selfies(selfies))

# integer encode
char_list = list(split_selfies(selfies))

# Check if SELFIES string contains unconnected molecules
if "." in list(char_list) and not "." in vocab_stoi:
raise ValueError(
"The SELFIES string contains two unconnected molecules "
"(given by the '.' character), but vocab_stoi does not "
"contain the '.' key. Please add it or separate the molecules."
)

integer_encoded = [vocab_stoi[char] for char in char_list]
char_list = split_selfies(selfies)

try:
integer_encoded = [vocab_stoi[char] for char in char_list]
except KeyError as e:
if e.args[0] == ".":
raise KeyError(
"The SELFIES string contains two unconnected molecules "
"(given by the '.' character), but vocab_stoi does not "
"contain the '.' key. Please add it to the vocabulary "
"or separate the molecules."
)
raise KeyError(e.args[0])


if enc_type == "label":
return integer_encoded
Expand Down

0 comments on commit 1d22f1d

Please sign in to comment.