1#!/bin/bash
2
3#############
4#  Soothsayer, an extensible predictive text entry system
5#  ------------------------------------------------------
6#
7#  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
8#
9#  This program is free software; you can redistribute it and/or modify
10#  it under the terms of the GNU General Public License as published by
11#  the Free Software Foundation; either version 2 of the License, or
12#  (at your option) any later version.
13#
14#  This program is distributed in the hope that it will be useful,
15#  but WITHOUT ANY WARRANTY; without even the implied warranty of
16#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17#  GNU General Public License for more details.
18#
19#  You should have received a copy of the GNU General Public License along
20#  with this program; if not, write to the Free Software Foundation, Inc.,
21#  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22
23
24####
25# Integration tests
26#
27
28TEXT2NGRAM=../../src/tools/text2ngram
29SIMULATOR=../../src/tools/soothsayer_simulator
30
31TRAINING_CORPUS=${srcdir}/../../COPYING
32CONTROL_CORPUS=`basename ${TRAINING_CORPUS}_excerpt.txt`
33
34CONFIG_TEMPLATE=${srcdir}/../../resources/profiles/soothsayer.xml.template
35CONFIG=soothsayer.xml
36
37NGRAM_CARDINALITY=3
38
39
40DATABASE=database.db
41
42ERROR_CODE=1
43
44####
45# Generate resources for statistical predictive plugins
46#
47# arg1 n-gram order of statistical resources
48# arg2 database name
49function generate_resources()
50{
51    if [ -z "$1" -o -z "$2" ]
52    then
53        echo "Error: generate_resources() requires two arguments"
54	exit $ERROR_CODE
55    fi
56
57    i=1
58    while [ "$i" -le "$1" ]
59    do
60      execute "$TEXT2NGRAM -o $DATABASE -n $i -f sqlite -l $2"
61      i=`expr $i + 1`
62    done
63}
64
65####
66# Tweak configuration file
67#
68function generate_config()
69{
70#    # use generate_soothsayer_config script from src/resources/profiles
71#    ${srcdir}/../../resources/profiles/generate_soothsayer_config.sh ${srcdir}/../../resources/profiles/soothsayer.xml.template soothsayer.xml .
72#
73
74    cp -f ${CONFIG_TEMPLATE} ${CONFIG}
75    replace_config_entry PLUGINS SmoothedNgramPlugin
76    replace_config_entry DBFILENAME ${DATABASE}
77    NGRAM_DELTAS=""
78    COUNTER=1
79    DELTA=10
80    while [ "$COUNTER" -le "$NGRAM_CARDINALITY" ]
81    do
82      NGRAM_DELTAS="${NGRAM_DELTAS} ${DELTA}"
83      DELTA=`expr ${DELTA} \* 10`
84      COUNTER=`expr $COUNTER + 1`
85    done
86    replace_config_entry DELTAS "${NGRAM_DELTAS}"
87}
88
89function replace_config_entry()
90{
91    sed -i.bak -e "s|<$1>.*</$1>|<$1>$2</$1>|" ${CONFIG}
92}
93
94####
95# Restore configuration file
96#
97function restore_config()
98{
99    rm -f ${CONFIG} ${CONFIG}.bak
100}
101
102####
103# Generate control corpus
104#
105function generate_control_corpus
106{
107    # just extract some text from the training
108    # corpus for the time being      # REVISIT
109    cat $TRAINING_CORPUS | head -n 20 | tail -n 10 > $CONTROL_CORPUS
110}
111
112####
113# simulate
114#
115# arg1 control corpus file
116#
117function simulate()
118{
119    if [ -z "$1" ]
120    then
121	echo "Error: simulate requires an argument"
122	exit $ERROR_CODE
123    else
124	execute "$SIMULATOR -i -q -c $CONFIG $CONTROL_CORPUS"
125    fi
126}
127
128####
129# execute()
130#
131# arg1 command line argument to execute
132#
133function execute()
134{
135    if [ -z "$1" ]
136    then
137	echo "Error: invoked execute() with no argument."
138	exit $ERROR_CODE
139    else
140	$1
141	if [ $? -ne 0 ]
142	    then
143	    echo "Error executing command: $1"
144	    exit $ERROR_CODE
145	fi
146    fi
147}
148
149####
150# clean_up()
151#
152# cleans up after itself
153#
154function clean_up()
155{
156    if [ -f "$DATABASE" ]
157    then
158	rm $DATABASE
159    fi
160
161    if [ -f "$CONTROL_CORPUS" ]
162    then
163        rm $CONTROL_CORPUS
164    fi
165}
166
167
168####
169# Mainline
170#
171clean_up
172generate_config
173generate_resources $NGRAM_CARDINALITY $TRAINING_CORPUS
174generate_control_corpus
175simulate $CONTROL_CORPUS
176restore_config
177clean_up
178