code: def get_vocab_mapping(seq_list, num_vocab):
      """
      Input:
          seq_list: a list of sequences
          num_vocab: vocabulary size
      Output:
          vocab2idx:
              A dictionary that maps vocabulary into integer index.
              Additioanlly, we also index '__UNK__' and '__EOS__'
              '__UNK__' : out-of-vocabulary term
              '__EOS__' : end-of-sentence
  
          idx2vocab:
              A list that maps idx to actual vocabulary.
  
      """
  
      vocab_cnt = {}
      vocab_list = []
      for seq in seq_list:
          for w in seq:
              if w in vocab_cnt:
                  vocab_cnt[w] += 1
              else:
                  vocab_cnt[w] = 1
                  vocab_list.append(w)
  
      cnt_list = np.array([vocab_cnt[w] for w in vocab_list])
      topvocab = np.argsort(-cnt_list, kind="stable")[:num_vocab]
  
      print("Coverage of top {} vocabulary:".format(num_vocab))
      print(float(np.sum(cnt_list[topvocab])) / np.sum(cnt_list))
  
      vocab2idx = {
          vocab_list[vocab_idx]: idx
          for idx, vocab_idx in enumerate(topvocab)
      }
      idx2vocab = [vocab_list[vocab_idx] for vocab_idx in topvocab]
  
      # print(topvocab)
      # print([vocab_list[v] for v in topvocab[:10]])
      # print([vocab_list[v] for v in topvocab[-10:]])
  
      vocab2idx["__UNK__"] = num_vocab
      idx2vocab.append("__UNK__")
  
      vocab2idx["__EOS__"] = num_vocab + 1
      idx2vocab.append("__EOS__")
  
      # test the correspondence between vocab2idx and idx2vocab
      for idx, vocab in enumerate(idx2vocab):
          assert idx == vocab2idx[vocab]
  
      # test that the idx of '__EOS__' is len(idx2vocab) - 1.
      # This fact will be used in decode_arr_to_seq, when finding __EOS__
      assert vocab2idx["__EOS__"] == len(idx2vocab) - 1
  
      return vocab2idx, idx2vocab
  
file_path: ogb/examples/graphproppred/code2/utils.py
imports: import torch
  from collections import Counter
  import numpy
  import torch
label_desc: Use image node_id 1 for calling a global function with example usage: get_vocab_mapping(seq_list, num_vocab) and returns: vocab2idx,  idx2vocab