Roshrini commented on a change in pull request #12535: [MXNET-954] Implementation of Structured-Self-Attentive-Sentence-Embedding URL: https://github.com/apache/incubator-mxnet/pull/12535#discussion_r225731127
########## File path: example/self_attentive_sentence_embedding/code/prepare_data.py ########## @@ -0,0 +1,169 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +# This module is used to parse the raw data and process the training data needed for the model. +# author: kenjewu + +import mxnet as mx +import numpy as np +import gluonnlp as nlp + +import os +import re +import json +import pickle +import collections +import warnings +warnings.filterwarnings('ignore') + +from sklearn.model_selection import train_test_split + + +UNK = '<unk>' +PAD = '<pad>' + + +def clean_str(string): + """ + Tokenization/string cleaning. + Original from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py + """ + string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string) + string = re.sub(r"\'s", " \'s", string) + string = re.sub(r"\'ve", " \'ve", string) + string = re.sub(r"n\'t", " n\'t", string) + string = re.sub(r"\'re", " \'re", string) + string = re.sub(r"\'d", " \'d", string) + string = re.sub(r"\'ll", " \'ll", string) + string = re.sub(r",", " , ", string) + string = re.sub(r"!", " ! ", string) + string = re.sub(r"\(", " \( ", string) + string = re.sub(r"\)", " \) ", string) + string = re.sub(r"\?", " \? ", string) + string = re.sub(r"\s{2,}", " ", string) + + return string.strip().lower() + + +def pad_sequences(sequences, max_len, pad_value): + ''' + Fill the sequence to the specified length, long truncation + Args: + sequences: A list of all sentences, a list of list + max_len: Specified maximum length + pad_value: Specified fill value + Returns: + pades_seqs: A numpy array + ''' + + # max_len = max(map(lambda x: len(x), sequences)) Review comment: remove commented line ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
