天天看點

《懶人Shell腳本》之二——語料庫的格式化輸出

1、引言

在進行自然語言處理(NLP)處理的時候,基本的操作無外乎分詞、分類、聚類、命名實體識别、規則過濾、摘要提取、關鍵字提取、詞性标注、拼音标注等。

分類通用的做法就是根據提供的語言庫自學習識别成對應的分類。現有的複旦大學提供的語料庫有20種分類。(參考:

http://www.nlpir.org/?action-viewnews-itemid-103

),網上也有提供更多種分類的。

分詞網上比較NB的幾個實驗室有:

1)、背景理工大學張華平副教授的 nlp 自然語言處理與資訊共享檢索平台

http://ictclas.nlpir.org/nlpir/

2)、哈工大“語言雲” 以哈工大社會計算與資訊檢索研究中心研發的 “語言技術平台(LTP)”

http://www.ltp-cloud.com/demo/

(本段摘自網絡)文本分類語料庫(複旦)測試語料由複旦大學李榮陸提供。answer.rar為測試語料,共9833篇文檔;train.rar為訓練語料,共9804篇文檔,分為20個類别。訓練語料和測試語料基本按照1:1的比例來劃分。收集工作花費了不少人力和物力,是以請大家在使用時盡量注明來源(複旦大學計算機資訊與技術系國際資料庫中心自然語言處理小組)。檔案較大(訓練測試各50多兆)。

實際項目中需要根據自己的需要進行定制處理。

2、需求點

結合口頭需求,我整理出下面的兩個核心需求點。

需求1:以中文形式輸出語料庫中包含的全部檔案類型到一個類型檔案outtype.txt。

需求2:将預料庫中的所有檔案以[EndEnd]結尾并合并,導出到資料檔案outdata.txt。

其中檔案路徑和檔案個數如下,累計檔案綜合近1.3GB。

《懶人Shell腳本》之二——語料庫的格式化輸出

第一列為檔案類型,第二列為檔案的個數。

3、需求分析

需求1:

1)每個類别命名的檔案夾下存放的就是該類别的檔案。統計下該類别下檔案的個數cnt。

2)内層循環cnt次,檔案名追加輸出到一個檔案。

3)外層循環20次(一共20類預料)即可。

需求2:

1)每個檔案末尾追加[EndEnd];

2) 便利每個路徑下的檔案合成一個檔案。

細節注意事項:需求1的類别和需求2的以[EndEnd]結束的檔案要一一對應,一旦對應偏了,整個工作都會白費。

4、腳本實作

#author:

http://blog.csdn.net/laoyang360

#date:20160304 pm22:38

#version:V0.1

#!/bin/bash

#the dir for use

DIR_NAME=./train

OUT_RESULT=./out_result

CNT_FILE=files_cnt.txt

NAME_FILE=all_file_dir.txt

TOTAL_TYPES_FILE=$OUT_RESULT/outtype.txt

TOTAL_TYPES_BAK_FILE=total_types_bak.txt

TOTAL_OUTFILE=$OUT_RESULT/outdata.txt

#clear the existing contents

function initialize()

{

mkdir $OUT_RESULT

cat /dev/null > $CNT_FILE

cat /dev/null > $NAME_FILE

cat /dev/null > $TOTAL_TYPES_FILE

cat /dev/null > $TOTAL_OUTFILE;

}

#list all files and stat file cnts

function list_all_files()

for file in ` ls $1 | sort`

do

if [ -d $1"/"$file ]

then

file_cnt=`ls $1"/"$file | wc -l`

echo $1"/"$file $file_cnt >> $CNT_FILE

list_all_files $1"/"$file

else

echo $1"/"$file >> $NAME_FILE

fi

done

#construct files sort by types

function constrcut_type_files()

line_cnt=1;

mkdir ./out_put

cat $CNT_FILE | while read line

CNT_NO_FILE=${CNT_FILE}_${line_cnt}

#echo "linecnt="$line_cnt;

echo $line > ./out_put/$CNT_NO_FILE;

#first column of every line

type_name=` awk '{print $1}' ./out_put/$CNT_NO_FILE `

#sencond column of every line, nums for print

type_cnt=` awk '{print $2}' ./out_put/$CNT_NO_FILE `

echo $type_name $type_cnt

#construct files

for((curnum=0;curnum<$type_cnt;curnum++))

echo $type_name >> $TOTAL_TYPES_FILE

line_cnt=$((line_cnt+1));

done;

rm -rf ./out_put

#format types_files

function typefile_format()

#bak for source

cp -f $TOTAL_TYPES_FILE $TOTAL_TYPES_BAK_FILE

sed -i 's#./train/##g' $TOTAL_TYPES_FILE;

sed -i 's#C4-Literature#文學#g' $TOTAL_TYPES_FILE; #17

sed -i 's#C5-Education#教育#g' $TOTAL_TYPES_FILE; #18

sed -i 's#C6-Philosophy#哲學#g' $TOTAL_TYPES_FILE; #19

sed -i 's#C15-Energy#能源#g' $TOTAL_TYPES_FILE; #2

sed -i 's#C16-Electronics#電子#g' $TOTAL_TYPES_FILE; #3 1240

sed -i 's#C17-Communication#通訊#g' $TOTAL_TYPES_FILE; #4

sed -i 's#C29-Transport#運輸#g' $TOTAL_TYPES_FILE; #7

sed -i 's#C35-Law#法學#g' $TOTAL_TYPES_FILE; #11

sed -i 's#C36-Medical#醫學#g' $TOTAL_TYPES_FILE; #12

sed -i 's#C37-Military#軍事#g' $TOTAL_TYPES_FILE; #13

#sed -i 's#C3-Art#藝術#g' $TOTAL_TYPES_FILE; #16 763

#sed -i 's#C7-History#曆史#g' $TOTAL_TYPES_FILE #20

#sed -i 's#C11-Space#空間#g' $TOTAL_TYPES_FILE; #1

#sed -i 's#C19-Computer#電腦#g' $TOTAL_TYPES_FILE; #5

#sed -i 's#C23-Mine#礦#g' $TOTAL_TYPES_FILE; #6

#sed -i 's#C31-Enviornment#環境#g' $TOTAL_TYPES_FILE; #8

#sed -i 's#C32-Agriculture#農業#g' $TOTAL_TYPES_FILE; #9

#sed -i 's#C34-Economy#經濟#g' $TOTAL_TYPES_FILE; #10 1623

#sed -i 's#C38-Politics#政治#g' $TOTAL_TYPES_FILE; #14 1047

#sed -i 's#C39-Sports#體育#g' $TOTAL_TYPES_FILE; #15

#GBK2UTF8, ./train changed to utf-8 format

function GBK2UTF8()

#for all files in ./train

` find $DIR_NAME -type d -exec mkdir -p utf/{} \; `

` find $DIR_NAME -type f -exec iconv -f GBK -t UTF-8 {} -o utf/{} >/dev/null 2>&1 \; `

rm -rf $DIR_NAME;

mv utf/* ./;

#new and "[EndEnd]" at the end of every file

function allfiles_addend()

icnt=0;

cat $NAME_FILE | while read line

echo "[EndEnd]" >> $line;

icnt=$((icnt+1));

#echo "icnt ="$icnt;

#Merage all files together

function merge_all_files()

#find $DIR_NAME -type f -exec cat {} \;>all_files_together.txt

cat $line >> $TOTAL_OUTFILE;

echo "icnt ="$icnt;

#executing for use

initialize;

list_all_files $DIR_NAME;

constrcut_type_files;

typefile_format;

GBK2UTF8;

allfiles_addend;

merge_all_files;

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

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

5、小結

1)Shell腳本寫的多了也就熟悉了。

2)今天起包括後期,所有工程式的Shell腳本都歸檔、整理思路,形成《懶人Shell腳本》系列文章。前面已經有一篇:

http://blog.csdn.net/laoyang360/article/details/49834859

3)Shell腳本有明顯C、C++語言的痕迹,慢慢過渡。腳本畢竟簡練為妙!

4)近期浏覽blog也發現了Python在爬蟲方面的優勢,有時間的話會學習整理。

6、GitHub下載下傳位址:

https://github.com/laoyang360/corpus_process