This is fine, but the code can be simplified a bit by using nltk.compat.string_types