
'''simple docstring'''




import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer





class  A				(							UpperCAmelCase__						):





     def 				__lowerCAmelCase							(							self	) ->				Union[str, Any]:






         """simple docstring"""






         A       :   Any					=						tempfile.mkdtemp()
         A       :   str					=						5

         # Realm tok
         A       :   Union[str, Any]					=						[
             '[UNK]',
             '[CLS]',
             '[SEP]',
             '[PAD]',
             '[MASK]',
             'test',
             'question',
             'this',
             'is',
             'the',
             'first',
             'second',
             'third',
             'fourth',
             'fifth',
             'record',
             'want',
             '##want',
             '##ed',
             'wa',
             'un',
             'runn',
             '##ing',
             ',',
             'low',
             'lowest',
         ]
         A       :   Union[str, Any]					=						os.path.join(self.tmpdirname						,			'''realm_tokenizer'''	)
         os.makedirs(snake_case_						,			exist_ok=snake_case_	)
         A       :   int					=						os.path.join(snake_case_						,			VOCAB_FILES_NAMES['''vocab_file''']	)
         with open(self.vocab_file						,			'''w'''						,			encoding='''utf-8'''	) as vocab_writer:
             vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]	)	)

         A       :   List[str]					=						os.path.join(self.tmpdirname						,			'''realm_block_records'''	)
         os.makedirs(snake_case_						,			exist_ok=snake_case_	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname						,			'''realm_tokenizer'''	)	)





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         shutil.rmtree(self.tmpdirname	)





     def 				__lowerCAmelCase							(							self	) ->				Optional[Any]:






         """simple docstring"""






         A       :   Optional[int]					=						RealmConfig(num_block_records=self.num_block_records	)
         return config





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         A       :   Any					=						Dataset.from_dict(
             {
                 '''id''': ['''0''', '''1'''],
                 '''question''': ['''foo''', '''bar'''],
                 '''answers''': [['''Foo''', '''Bar'''], ['''Bar''']],
             }	)
         return dataset





     def 				__lowerCAmelCase							(							self	) ->				Any:






         """simple docstring"""






         A       :   str					=						np.array(
             [
                 b'''This is the first record''',
                 b'''This is the second record''',
                 b'''This is the third record''',
                 b'''This is the fourth record''',
                 b'''This is the fifth record''',
                 b'''This is a longer longer longer record''',
             ]						,			dtype=snake_case_						,			)
         return block_records





     def 				__lowerCAmelCase							(							self	) ->				Dict:






         """simple docstring"""






         A       :   Optional[Any]					=						RealmRetriever(
             block_records=self.get_dummy_block_records()						,			tokenizer=self.get_tokenizer()						,			)
         return retriever





     def 				__lowerCAmelCase							(							self	) ->				str:






         """simple docstring"""






         A       :   Dict					=						self.get_config()
         A       :   Tuple					=						self.get_dummy_retriever()
         A       :   Optional[Any]					=						retriever.tokenizer

         A       :   Dict					=						np.array([0, 3]						,			dtype='''long'''	)
         A       :   int					=						tokenizer(['''Test question''']	).input_ids
         A       :   Dict					=						tokenizer(
             ['''the fourth''']						,			add_special_tokens=snake_case_						,			return_token_type_ids=snake_case_						,			return_attention_mask=snake_case_						,			).input_ids
         A       :   Tuple					=						config.reader_seq_len

         A       :   int					=						retriever(
             snake_case_						,			snake_case_						,			answer_ids=snake_case_						,			max_length=snake_case_						,			return_tensors='''np'''	)

         self.assertEqual(len(snake_case_	)						,			2	)
         self.assertEqual(len(snake_case_	)						,			2	)
         self.assertEqual(len(snake_case_	)						,			2	)
         self.assertEqual(concat_inputs.input_ids.shape						,			(2, 10)	)
         self.assertEqual(concat_inputs.attention_mask.shape						,			(2, 10)	)
         self.assertEqual(concat_inputs.token_type_ids.shape						,			(2, 10)	)
         self.assertEqual(concat_inputs.special_tokens_mask.shape						,			(2, 10)	)
         self.assertEqual(
             tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]	)						,			['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''first''', '''record''', '''[SEP]''']						,			)
         self.assertEqual(
             tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]	)						,			['''[CLS]''', '''test''', '''question''', '''[SEP]''', '''this''', '''is''', '''the''', '''fourth''', '''record''', '''[SEP]''']						,			)





     def 				__lowerCAmelCase							(							self	) ->				Tuple:






         """simple docstring"""






         A       :   str					=						self.get_config()
         A       :   int					=						self.get_dummy_retriever()
         A       :   Dict					=						retriever.tokenizer

         A       :   int					=						np.array([0, 3, 5]						,			dtype='''long'''	)
         A       :   Union[str, Any]					=						tokenizer(['''Test question''']	).input_ids
         A       :   str					=						tokenizer(
             ['''the fourth''', '''longer longer''']						,			add_special_tokens=snake_case_						,			return_token_type_ids=snake_case_						,			return_attention_mask=snake_case_						,			).input_ids
         A       :   Any					=						config.reader_seq_len

         A       :   str					=						retriever(
             snake_case_						,			snake_case_						,			answer_ids=snake_case_						,			max_length=snake_case_						,			return_tensors='''np'''	)

         self.assertEqual([False, True, True]						,			snake_case_	)
         self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]						,			snake_case_	)
         self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]						,			snake_case_	)





     def 				__lowerCAmelCase							(							self	) ->				List[Any]:






         """simple docstring"""






         A       :   int					=						self.get_dummy_retriever()
         retriever.save_pretrained(os.path.join(self.tmpdirname						,			'''realm_block_records'''	)	)

         # Test local path
         A       :   str					=						retriever.from_pretrained(os.path.join(self.tmpdirname						,			'''realm_block_records'''	)	)
         self.assertEqual(retriever.block_records[0]						,			b'''This is the first record'''	)

         # Test mocked remote path
         with patch('''transformers.models.realm.retrieval_realm.hf_hub_download'''	) as mock_hf_hub_download:
             A       :   Any					=						os.path.join(
                 os.path.join(self.tmpdirname						,			'''realm_block_records'''	)						,			_REALM_BLOCK_RECORDS_FILENAME	)
             A       :   Union[str, Any]					=						RealmRetriever.from_pretrained('''google/realm-cc-news-pretrained-openqa'''	)

         self.assertEqual(retriever.block_records[0]						,			b'''This is the first record'''	)











"""simple docstring"""




import os

# Precomputes a list of the 100 first triangular numbers
lowerCamelCase_     :						List[str]						=					[int(0.5 * n * (n + 1)) for n in range(1, 1_01)]





def 						UpperCAmelCase__		(     ):






  """simple docstring"""





  A_  :       Union[str, Any]										=  os.path.dirname(os.path.realpath(_UpperCAmelCase					)					)
  A_  :       Tuple										=  os.path.join(_UpperCAmelCase ,					'words.txt'					)

  A_  :       List[Any]										=  ''
  with open(_UpperCAmelCase					) as f:
    A_  :       int										=  f.readline()

  A_  :       Optional[Any]										=  [word.strip('"'					) for word in words.strip('\r\n'					).split(','					)]
  A_  :       Dict										=  [
      word
      for word in [sum(ord(_UpperCAmelCase					) - 64 for x in word					) for word in words]
      if word in TRIANGULAR_NUMBERS
  ]
  return len(_UpperCAmelCase					)


if __name__ == "__main__":
   print(solution())