class_docstr: Test case for SentencePieceProcessor
class_docstr_tok: ["Test","case","for","SentencePieceProcessor"]
code: def test_immutable_proto(self):
      text = "I saw a girl with a telescope."
      s1 = self.sp_.EncodeAsImmutableProto(text)
      s2 = self.sp_.SampleEncodeAsImmutableProto(text, 10, 0.2)
      s3 = self.sp_.NBestEncodeAsImmutableProto(text, 10)
      s4 = self.sp_.DecodePiecesAsImmutableProto(["foo", "bar"])
      s5 = self.sp_.DecodeIdsAsImmutableProto([20, 30])
  
      print(s1)
      print(s2)
      print(s3)
      print(s4)
      print(s5)
  
      t1 = self.sp_.encode_as_immutable_proto(text)
      t2 = self.sp_.sample_encode_as_immutable_proto(text, 10, 0.2)
      t3 = self.sp_.nbest_encode_as_immutable_proto(text, 10)
      t4 = self.sp_.decode_pieces_as_immutable_proto(["foo", "bar"])
      t5 = self.sp_.decode_ids_as_immutable_proto([20, 30])
  
      y1 = self.sp_.encode(text, out_type="immutable_proto")
      y2 = self.sp_.encode(
          text, enable_sampling=True, out_type="immutable_proto"
      )
      y3 = self.sp_.nbest_encode(
          text, out_type="immutable_proto", nbest_size=10
      )
      y4 = self.sp_.decode(["foo", "bar"], out_type="immutable_proto")
      y5 = self.sp_.decode([20, 30], out_type="immutable_proto")
  
      self.assertEqual(s1, t1)
      self.assertEqual(s3, t3)
      self.assertEqual(s4, t4)
      self.assertEqual(s5, t5)
      self.assertEqual(s1, y1)
      self.assertEqual(s3, y3)
      self.assertEqual(s4, y4)
      self.assertEqual(s5, y5)
  
      hset_piece = defaultdict(int)
  
      # eq test
      for i in range(len(s1.pieces)):
          self.assertEqual(s1.pieces[i], t1.pieces[i])
          hset_piece[s1.pieces[i]] += 1
          hset_piece[t1.pieces[i]] += 1
  
      self.assertEqual(len(hset_piece), len(s1.pieces))
  
      # has test
      hset = defaultdict(int)
      hset[s1] += 1
      hset[t1] += 1
      hset[s3] += 1
      hset[t3] += 1
  
      self.assertEqual(len(hset), 2)
      self.assertEqual(hset[s1], 2)
      self.assertEqual(hset[s3], 2)
      self.assertEqual(hset[t1], 2)
      self.assertEqual(hset[t3], 2)
  
      x1 = self.sp_.encode_as_serialized_proto(text)
      x2 = self.sp_.sample_encode_as_serialized_proto(text, 10, 0.2)
      x3 = self.sp_.nbest_encode_as_serialized_proto(text, 10)
      x4 = self.sp_.decode_pieces_as_serialized_proto(["foo", "bar"])
      x5 = self.sp_.decode_ids_as_serialized_proto([20, 30])
  
      self.assertEqual(x1, t1.SerializeAsString())
      self.assertEqual(x3, t3.SerializeAsString())
      self.assertEqual(x4, t4.SerializeAsString())
      self.assertEqual(x5, t5.SerializeAsString())
  
      v1 = self.sp_.EncodeAsIds(text)
      v2 = self.sp_.EncodeAsPieces(text)
      self.assertEqual([x.id for x in s1.pieces], v1)
      self.assertEqual([x.piece for x in s1.pieces], v2)
      self.assertEqual(text, s1.text)
  
      surfaces1 = [s1.text[x.begin : x.end] for x in s1.pieces]
      surfaces2 = [x.surface for x in s1.pieces]
      self.assertEqual(surfaces1, surfaces2)
  
      ids = []
      for i in range(len(s1.pieces)):
          ids.append(s1.pieces[i].id)
      self.assertEqual(ids, v1)
  
      pieces = []
      for i in range(len(s1.pieces)):
          pieces.append(s1.pieces[i].piece)
      self.assertEqual(pieces, v2)
  
      for v in s3.nbests:
          self.assertEqual(text, v.text)
          self.assertEqual(
              self.sp_.Decode([x.id for x in v.pieces]), text
          )
  
      for i in range(len(s3.nbests)):
          self.assertEqual(text, s3.nbests[i].text)
          self.assertEqual(
              self.sp_.Decode([x.id for x in s3.nbests[i].pieces]), text
          )
  
      # slice
      self.assertEqual(s1.pieces[::-1], list(reversed(s1.pieces)))
      self.assertEqual(s3.nbests[::-1], list(reversed(s3.nbests)))
  
      # Japanese offset
      s1 = self.jasp_.EncodeAsImmutableProto(
          "吾輩は猫である。Hello world. ABC 123"
      )
      surfaces1 = [s1.text[x.begin : x.end] for x in s1.pieces]
      surfaces2 = [x.surface for x in s1.pieces]
      self.assertEqual(surfaces1, surfaces2)
  
      ids = [x.id for x in s1.pieces]
      s2 = self.jasp_.DecodeIdsAsImmutableProto(ids)
      self.assertEqual(s2, s1)
  
      pieces = [x.piece for x in s1.pieces]
      s2 = self.jasp_.DecodePiecesAsImmutableProto(pieces)
      self.assertEqual(s2, s1)
  
imports: from collections import defaultdict
  import io
  import os
  import pickle
  import sys
  import unittest
  import sentencepiece
label_desc: Use image node_id 10 for calling the TestSentencepieceProcessor obj's underlying member method code with example usage: obj.test_immutable_proto() without return types