MCPcopy Create free account
hub / github.com/SooLab/CGFormer / bpe

Method bpe

utils/simple_tokenizer.py:80–119  ·  view source on GitHub ↗
(self, token)

Source from the content-addressed store, hash-verified

78 self.pat = re.compile(r"""<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+""", re.IGNORECASE)
79
80 def bpe(self, token):
81 if token in self.cache:
82 return self.cache[token]
83 word = tuple(token[:-1]) + ( token[-1] + '</w>',)
84 pairs = get_pairs(word)
85
86 if not pairs:
87 return token+'</w>'
88
89 while True:
90 bigram = min(pairs, key = lambda pair: self.bpe_ranks.get(pair, float('inf')))
91 if bigram not in self.bpe_ranks:
92 break
93 first, second = bigram
94 new_word = []
95 i = 0
96 while i < len(word):
97 try:
98 j = word.index(first, i)
99 new_word.extend(word[i:j])
100 i = j
101 except:
102 new_word.extend(word[i:])
103 break
104
105 if word[i] == first and i < len(word)-1 and word[i+1] == second:
106 new_word.append(first+second)
107 i += 2
108 else:
109 new_word.append(word[i])
110 i += 1
111 new_word = tuple(new_word)
112 word = new_word
113 if len(word) == 1:
114 break
115 else:
116 pairs = get_pairs(word)
117 word = ' '.join(word)
118 self.cache[token] = word
119 return word
120
121 def encode(self, text):
122 bpe_tokens = []

Callers 1

encodeMethod · 0.95

Calls 1

get_pairsFunction · 0.85

Tested by

no test coverage detected