2020-12-18 19:31:44 +08:00
|
|
|
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
from typing import Dict, Iterable, List
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2020-12-09 15:58:39 +08:00
|
|
|
__all__ = ["Vocab"]
|
|
|
|
|
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
class Vocab(object):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" Vocabulary.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
-----------
|
|
|
|
symbols: Iterable[str]
|
|
|
|
Common symbols.
|
|
|
|
|
|
|
|
padding_symbol: str, optional
|
|
|
|
Symbol for pad. Defaults to "<pad>".
|
|
|
|
|
|
|
|
unk_symbol: str, optional
|
|
|
|
Symbol for unknow. Defaults to "<unk>"
|
|
|
|
|
|
|
|
start_symbol: str, optional
|
|
|
|
Symbol for start. Defaults to "<s>"
|
|
|
|
|
|
|
|
end_symbol: str, optional
|
|
|
|
Symbol for end. Defaults to "</s>"
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self,
|
|
|
|
symbols: Iterable[str],
|
|
|
|
padding_symbol="<pad>",
|
|
|
|
unk_symbol="<unk>",
|
|
|
|
start_symbol="<s>",
|
|
|
|
end_symbol="</s>"):
|
2020-10-20 16:06:11 +08:00
|
|
|
self.special_symbols = OrderedDict()
|
|
|
|
for i, item in enumerate(
|
|
|
|
[padding_symbol, unk_symbol, start_symbol, end_symbol]):
|
|
|
|
if item:
|
|
|
|
self.special_symbols[item] = len(self.special_symbols)
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
self.padding_symbol = padding_symbol
|
|
|
|
self.unk_symbol = unk_symbol
|
|
|
|
self.start_symbol = start_symbol
|
|
|
|
self.end_symbol = end_symbol
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
self.stoi = OrderedDict()
|
|
|
|
self.stoi.update(self.special_symbols)
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
for i, s in enumerate(symbols):
|
|
|
|
if s not in self.stoi:
|
2020-10-22 13:04:45 +08:00
|
|
|
self.stoi[s] = len(self.stoi)
|
2020-10-20 16:06:11 +08:00
|
|
|
self.itos = {v: k for k, v in self.stoi.items()}
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.stoi)
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
@property
|
|
|
|
def num_specials(self):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" The number of special symbols.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return len(self.special_symbols)
|
|
|
|
|
|
|
|
# special tokens
|
|
|
|
@property
|
|
|
|
def padding_index(self):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" The index of padding symbol
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return self.stoi.get(self.padding_symbol, -1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unk_index(self):
|
2020-12-18 19:31:44 +08:00
|
|
|
"""The index of unknow symbol.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return self.stoi.get(self.unk_symbol, -1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def start_index(self):
|
2020-12-18 19:31:44 +08:00
|
|
|
"""The index of start symbol.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return self.stoi.get(self.start_symbol, -1)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def end_index(self):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" The index of end symbol.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return self.stoi.get(self.end_symbol, -1)
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
def __repr__(self):
|
|
|
|
fmt = "Vocab(size: {},\nstoi:\n{})"
|
|
|
|
return fmt.format(len(self), self.stoi)
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
def lookup(self, symbol):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" The index that symbol correspond.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return self.stoi[symbol]
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
def reverse(self, index):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" The symbol thar index cottespond.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
return self.itos[index]
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
def add_symbol(self, symbol):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" Add a new symbol in vocab.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
if symbol in self.stoi:
|
2020-12-18 19:31:44 +08:00
|
|
|
return
|
2020-10-20 16:06:11 +08:00
|
|
|
N = len(self.stoi)
|
|
|
|
self.stoi[symbol] = N
|
|
|
|
self.itos[N] = symbol
|
2020-12-18 19:31:44 +08:00
|
|
|
|
2020-10-20 16:06:11 +08:00
|
|
|
def add_symbols(self, symbols):
|
2020-12-18 19:31:44 +08:00
|
|
|
""" Add multiple symbols in vocab.
|
|
|
|
"""
|
2020-10-20 16:06:11 +08:00
|
|
|
for symbol in symbols:
|
|
|
|
self.add_symbol(symbol)
|