preprocess.sh 1.7 KB
Newer Older
1
#!/bin/bash
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright (c) 2016 Baidu, Inc. 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.

H
hupeng03 已提交
16 17 18 19 20
# 1. size of pos : neg = 1:1.
# 2. size of testing set = min(25k, len(all_data) * 0.1), others is traning set.
# 3. distinct train set and test set.
# 4. build dict

D
dangqingqing 已提交
21
set -e
H
hupeng03 已提交
22

W
wenboyang 已提交
23
export LC_ALL=C
24 25
UNAME_STR=`uname`

G
gangliao 已提交
26
if [ ${UNAME_STR} == 'Linux' ]; then
27 28 29 30
  SHUF_PROG='shuf'
else
  SHUF_PROG='gshuf'
fi
W
wenboyang 已提交
31

D
dangqingqing 已提交
32
mkdir -p data/tmp
Z
zhangjinchao01 已提交
33
python preprocess.py -i data/reviews_Electronics_5.json.gz
H
hupeng03 已提交
34 35
# uniq and shuffle
cd data/tmp
D
dangqingqing 已提交
36
echo 'uniq and shuffle...'
37 38
cat pos_*|sort|uniq|${SHUF_PROG}> pos.shuffed
cat neg_*|sort|uniq|${SHUF_PROG}> neg.shuffed
H
hupeng03 已提交
39 40

min_len=`sed -n '$=' neg.shuffed`
X
xuwei06 已提交
41
test_num=$((min_len/10))
H
hupeng03 已提交
42 43 44
if [ $test_num -gt 12500 ];then
 test_num=12500
fi
45
train_num=$((min_len-test_num))
H
hupeng03 已提交
46 47 48 49 50 51

head -n$train_num pos.shuffed >train.pos
head -n$train_num neg.shuffed >train.neg
tail -n$test_num pos.shuffed >test.pos
tail -n$test_num neg.shuffed >test.neg

52 53
cat train.pos train.neg | ${SHUF_PROG} >../train.txt
cat test.pos test.neg | ${SHUF_PROG} >../test.txt
H
hupeng03 已提交
54 55 56 57

cd -
echo 'data/train.txt' > data/train.list
echo 'data/test.txt' > data/test.list
Z
zhangjinchao01 已提交
58 59

# use 30k dict
H
hupeng03 已提交
60
rm -rf data/tmp
Z
zhangjinchao01 已提交
61 62
mv data/dict.txt data/dict_all.txt
cat data/dict_all.txt | head -n 30001 > data/dict.txt
H
hupeng03 已提交
63
echo 'preprocess finished'