File size: 847 Bytes
97b6013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""Utility module for sentiment analysis."""

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

import numpy as np

START_CHAR = 1
END_CHAR = 2
OOV_CHAR = 3


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

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

  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))

  return sentence