util.py 847 Bytes
Newer Older
1
2
3
4
5
6
"""Utility module for sentiment analysis."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

7
8
9
10
11
12
13
import numpy as np

START_CHAR = 1
END_CHAR = 2
OOV_CHAR = 3


14
15
def pad_sentence(sentence, sentence_length):
  """Pad the given sentense at the end.
16

17
18
19
  If the input is longer than sentence_length,
  the remaining portion is dropped.
  END_CHAR is used for the padding.
20

21
22
23
24
25
26
27
28
29
30
  Args:
    sentence: A numpy array of integers.
    sentence_length: The length of the input after the padding.
  Returns:
    A numpy array of integers of the given length.
  """
  sentence = sentence[:sentence_length]
  if len(sentence) < sentence_length:
    sentence = np.pad(sentence, (0, sentence_length - len(sentence)),
                      "constant", constant_values=(START_CHAR, END_CHAR))
31

32
  return sentence