Source code for torchnlp.word_to_vector.char_n_gram

# BSD 3-Clause License

# Copyright (c) James Bradbury and Soumith Chintala 2016,
# All rights reserved.

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:

# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.

# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.

# * Neither the name of the copyright holder nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import torch

from torchnlp.word_to_vector.pretrained_word_vectors import _PretrainedWordVectors


[docs]class CharNGram(_PretrainedWordVectors): """ Character n-gram is a character-based compositional model to embed textual sequences. Character n-gram embeddings are trained by the same Skip-gram objective. The final character embedding is the average of the unique character n-gram embeddings of wt. For example, the character n-grams (n = 1, 2, 3) of the word “Cat” are {C, a, t, #B#C, Ca, at, t#E#, #B#Ca, Cat, at#E#}, where “#B#” and “#E#” represent the beginning and the end of each word, respectively. Using the character embeddings efficiently provides morphological features. Each word is subsequently represented as xt, the concatenation of its corresponding word and character embeddings shared across the tasks. **Reference:** http://www.logos.t.u-tokyo.ac.jp/~hassy/publications/arxiv2016jmt/ Args: cache (str, optional): directory for cached vectors unk_init (callback, optional): by default, initialize out-of-vocabulary word vectors to zero vectors; can be any function that takes in a Tensor and returns a Tensor of the same size is_include (callable, optional): callable returns True if to include a token in memory vectors cache; some of these embedding files are gigantic so filtering it can cut down on the memory usage. We do not cache on disk if ``is_include`` is defined. Example: >>> from torchnlp.word_to_vector import CharNGram # doctest: +SKIP >>> vectors = CharNGram() # doctest: +SKIP >>> vectors['hello'] # doctest: +SKIP -1.7494 0.6242 ... -0.6202 2.0928 [torch.FloatTensor of size 100] """ name = 'charNgram.txt' url = ('https://www.logos.t.u-tokyo.ac.jp/~hassy/publications/arxiv2016jmt/' 'jmt_pre-trained_embeddings.tar.gz') def __init__(self, **kwargs): super(CharNGram, self).__init__(self.name, url=self.url, **kwargs) def __getitem__(self, token): vector = torch.Tensor(self.dim).zero_() # These literals need to be coerced to unicode for Python 2 compatibility # when we try to join them with read ngrams from the files. chars = ['#BEGIN#'] + list(token) + ['#END#'] num_vectors = 0 for n in [2, 3, 4]: end = len(chars) - n + 1 grams = [chars[i:(i + n)] for i in range(end)] for gram in grams: gram_key = '{}gram-{}'.format(n, ''.join(gram)) if gram_key in self.token_to_index: vector += self.vectors[self.token_to_index[gram_key]] num_vectors += 1 if num_vectors > 0: vector /= num_vectors else: vector = self.unk_init(vector) return vector