source: gs3-extensions/maori-lang-detection/gen_SentenceDetection_model.sh@ 33356

Last change on this file since 33356 was 33356, checked in by ak19, 5 years ago

Updating script. Correction to a filepath different in the svn folder structure

  • Property svn:executable set to *
File size: 6.0 KB
Line 
1#!/bin/bash
2
3#####################################################################################################
4# Following instructions at
5# https://stackoverflow.com/questions/36516363/sentence-detection-with-opennlp
6# But contrary to their example, don't insert empty newlines separating the sentences input file
7# Each sentence just needs to be on its own line. But no empty lines between each sentence.
8#####################################################################################################
9
10# 1. Create training file of Maori sentences in the correct format
11# Train this on the 2011 set (rather than 2017) set of 100k Maori language sentences
12# because the 2011 one appears to have fewer accidentally incorporated English sentences
13
14
15if [ ! -z $1 ]; then
16 if [ "x$1" = "x--help" ]; then
17 echo ""
18 echo "Usage: $0 <mri sentences.txt>"
19 echo " Try the one in ./opennlp-corpus/leipzig/data/mri_web_2011_100K-sentences.txt"
20 echo " (Unzip mri-opennlp-corpus.tar.gz)"
21 echo " It's better than the 100k sentences from 2017, as that contains a lot of English lines."
22 echo ""
23 exit
24 else
25 infile=$1
26 fi
27else
28 # use the file opennlp-corpus/leipzig/data/mri_web_2011_100K-sentences.txt as inputfile
29 # Check the file exists at the expected location. If not, check we have the tarball containing it and untar that.
30 # If the tarball doesn't exist, try to get what we want from svn if we have svn. If no svn either, bail.
31 if [ ! -f opennlp-corpus/leipzig/data/mri_web_2011_100K-sentences.txt ]; then
32 if [ -f mri-opennlp-corpus.tar.gz ]; then
33 tar -xvzf mri-opennlp-corpus.tar.gz
34 else
35 svn --help > /dev/null
36 if [ "x$?" != "x0" ]; then
37 echo "Attempted to train the Sentence Detector on Maori sentences on missing mri_web_2011_100K-sentences.txt"
38 echo "The tarball mri-opennlp-corpus.tar.gz (containing mri_web_2011_100K-sentences.txt) didn't exist either."
39 echo "And couldn't get opennlp-corpus/leipzig/data/mri_web_2011_100K-sentences.txt using SVN: no SVN."
40 echo "Exitting..."
41 exit
42 else
43 # Get just what we need from svn
44 # Based on https://www.apache.org/dist/opennlp/models/langdetect/1.8.3/README.txt
45 svn co --depth immediates --trust-server-cert --non-interactive https://svn.apache.org/repos/bigdata/opennlp/trunk opennlp-corpus
46 cd opennlp-corpus
47 svn up --set-depth immediates --trust-server-cert --non-interactive
48 cd leipzig
49 svn up --set-depth immediates --trust-server-cert --non-interactive
50 cd resources/
51 svn up --set-depth infinity --trust-server-cert --non-interactive
52 cd ../data
53 echo "About to download the sample Maori language 100k sentences file. This may take a while (<1 minute)."
54 svn up --trust-server-cert --non-interactive mri_web_2011_100K-sentences.txt
55 cd ..
56 # in opennlp-corpus/leipzig
57 chmod u+x create_langdetect_model.sh
58 cd ../..
59 fi
60 fi
61 fi
62 infile=./opennlp-corpus/leipzig/data/mri_web_2011_100K-sentences.txt
63fi
64
65echo $infile
66
67#tail -100 $infile
68
69
70mkdir -p $OPENNLP_HOME/training_data
71sentences_train_file=$OPENNLP_HOME/training_data/mri-sent.train
72if [ -f "$trainfile" ]; then
73 echo "Removing existing $sentences_train_file"
74 rm "$sentences_train_file"
75fi
76
77# Get just the sentences in the inputfile (remove the starting number followed by tab from each line)
78# see https://stackoverflow.com/questions/36516363/sentence-detection-with-opennlp
79# Don't add an extra newline at end of each sentence (don't insert an empty line between each sentence)
80
81
82# https://stackoverflow.com/questions/7619438/bash-read-a-file-line-by-line-and-process-each-segment-as-parameters-to-other-p
83# how come it removes the first number automatically?
84#while read file num sentence
85#do
86# echo "$num $sentence" >> $sentences_train_file
87#done < "$infile"
88
89# Don't add the extra newline at the end, creating empty lines between sentences output,
90# or the SentenceDetector in step 3 will do a poor job.
91# (The example at the stackoverflow link may have included newlines separating sentences for clarity.)
92#cat $infile | awk -F '\t' '{ print $2 "\n" }' > $sentences_train_file
93cat $infile | awk -F '\t' '{ print $2 }' > $sentences_train_file
94
95# Why did my way below not work?
96# openNLP-lang-detect/opennlp-corpus/leipzig/data>echo "100000\tYWCA Boarding house : ĀwhinaServices and support Kei te pūmanawa o Tāmaki Makaurau a YMCA." | awk -F "\t" '{ print $2 }'
97# $1 contains everything and $2 nothing. Why?
98# The problem appears to be that echo behaves differently from cat and less. Ask Dr Bainbridge what makes echo different.
99# Note that I tried manually inserting \t, after copying the original line with tabspacing had no effect. Still no difference.
100# Note 2: echo doesn't appear to preserve copied tab spaces.
101
102
103# 2. Create mri sentences model from training sentences file
104#$OPENNLP_HOME/bin/opennlp SentenceDetectorTrainer -model mri-sent_trained.bin -lang en -data mri-sent.train -encoding UTF-8
105
106if [ "x$OPENNLP_HOME" = "x" ]; then
107 echo "OPENNLP_HOME not set, attempting to set it to apache-opennlp-1.9.1 (ENSURE THIS EXISTS OR SET OPENNLP_HOME YOURSELF!)"
108 if [ -d apache-opennlp-* ]; then
109 cd apache-opennlp-*
110 export OPENNLP_HOME=`pwd`
111 cd ..
112 else
113 echo "No OPENNLP_HOME set and could not find a subfolder 'apache-opennlp-...' to set it to."
114 echo "Set OPENNLP_HOME yourself before running this script. Exitting..."
115 fi
116fi
117
118mkdir -p $OPENNLP_HOME/models
119
120$OPENNLP_HOME/bin/opennlp SentenceDetectorTrainer -model $OPENNLP_HOME/models/mri-sent_trained.bin -lang en -data $sentences_train_file -encoding UTF-8
121
122
123# 3. Let's try to split some Maori language text into sentences using our model created from training on sentences.
124# http://opennlp.apache.org/docs/1.9.1/manual/opennlp.html#tools.cli.sentdetect
125# Usage: opennlp SentenceDetector model < sentences
126echo ""
127echo "Testing the model."
128echo "Running the SentenceDetector with the generated mri-sent_trained.bin model on sample_mri_paragraphs.txt..."
129echo "****************************"
130$OPENNLP_HOME/bin/opennlp SentenceDetector $OPENNLP_HOME/models/mri-sent_trained.bin < models-trainingdata-and-sampletxts/sample_mri_paragraphs.txt
131echo "****************************"
132echo ""
Note: See TracBrowser for help on using the repository browser.