-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparsesynonyms
executable file
·51 lines (43 loc) · 1.48 KB
/
parsesynonyms
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
#!/usr/bin/env sh
# Parameters:
# $1 = input filename
# $2 = server, defaults to localhost:9200
# $3 = output filename, defaults to [input filename]_generated.txt
if [ -z "$1" ]
then
echo Please apply an input file
exit 1
fi
if [ -z "$2" ]
then
server="localhost:9200"
else
server="$2"
fi
if [ -z "$3" ]
then
filename=$(basename "$1")
outputFile="${filename%.*}_generated.txt"
else
outputFile="$3"
fi
# Create test index for analysis
curl -s -XPUT "$server/parse-synonyms-utility-index" -d "$(jq -n '{settings:{analysis:{analyzer:{synonyms:{type:"custom",tokenizer:"commasep",filter:["trim","asciifolding","lowercase","unique"]}},tokenizer:{commasep:{type:"pattern",pattern:","}}}}}')" > /dev/null
while read line; do
# sample input: cimodule,ciplusmodule,,,
# expected response example: {"tokens":[{"token":"cimodule","start_offset":0,"end_offset":8,"type":"word","position":1},{"token":"ciplusmodule","start_offset":9,"end_offset":21,"type":"word","position":2}]}
response=$(curl -s -XGET "$server/parse-synonyms-utility-index/_analyze?analyzer=synonyms" -d "$line" | jq -r '[.tokens[].token] | join(",")')
if [ -z "$response" ] || [ "$response" = "null" ]
then
echo Elasticsearch returned a malformed response
exit 2
fi
if [ -n "$outputResponse" ]
then
#only add newline if outputresponse already exists
outputResponse=$outputResponse$"\n"
fi
#concatenate string
outputResponse=$outputResponse$response
done < $1
echo $outputResponse > $outputFile