Returns text encoded in a way suitable for print or `tf.logging`.
(text)
| 98 | |
| 99 | |
| 100 | def printable_text(text): |
| 101 | """Returns text encoded in a way suitable for print or `tf.logging`.""" |
| 102 | |
| 103 | # These functions want `str` for both Python2 and Python3, but in one case |
| 104 | # it's a Unicode string and in the other it's a byte string. |
| 105 | if six.PY3: |
| 106 | if isinstance(text, str): |
| 107 | return text |
| 108 | elif isinstance(text, bytes): |
| 109 | return text.decode("utf-8", "ignore") |
| 110 | else: |
| 111 | raise ValueError("Unsupported string type: %s" % (type(text))) |
| 112 | elif six.PY2: |
| 113 | if isinstance(text, str): |
| 114 | return text |
| 115 | elif isinstance(text, unicode): |
| 116 | return text.encode("utf-8") |
| 117 | else: |
| 118 | raise ValueError("Unsupported string type: %s" % (type(text))) |
| 119 | else: |
| 120 | raise ValueError("Not running on Python2 or Python 3?") |
| 121 | |
| 122 | |
| 123 | def load_vocab(vocab_file): |
no test coverage detected