Sheldon Robinson
Committed by GitHub

Fix #1901: UnicodeEncodeError running export_bpe_vocab.py (#1902)

@@ -26,6 +26,7 @@ @@ -26,6 +26,7 @@
26 # Please install a version >=0.1.96 26 # Please install a version >=0.1.96
27 27
28 import argparse 28 import argparse
  29 +import codecs
29 from typing import Dict 30 from typing import Dict
30 31
31 try: 32 try:
@@ -56,11 +57,11 @@ def main(): @@ -56,11 +57,11 @@ def main():
56 57
57 sp = spm.SentencePieceProcessor() 58 sp = spm.SentencePieceProcessor()
58 sp.Load(model_file) 59 sp.Load(model_file)
59 - vocabs = [sp.IdToPiece(id) for id in range(sp.GetPieceSize())]  
60 - with open(vocab_file, "w") as vfile: 60 + vocabs = [sp.id_to_piece(id) for id in range(sp.get_piece_size())]
  61 + with codecs.open(vocab_file, "w", "utf-8") as vfile:
61 for v in vocabs: 62 for v in vocabs:
62 - id = sp.PieceToId(v)  
63 - vfile.write(f"{v}\t{sp.GetScore(id)}\n") 63 + id = sp.piece_to_id(v)
  64 + vfile.write(f"{v}\t{sp.get_score(id)}\n")
64 print(f"Vocabulary file is written to {vocab_file}") 65 print(f"Vocabulary file is written to {vocab_file}")
65 66
66 67