Reputation: 43
I have managed to create a working shell script (OS X) that seems to do what I want it to do (with little to no prior experience with shell). However, even though it seems to work, it does so rather slowly -- so I'm wondering if there are any obvious tweaks that would optimize performance.
So basically what the code does is that it passes a file through a python script to a REST API, and then it parses the resulting text (gets the 'lemma'-s out of it and assembles them into a continous string).
The text that we get back from the REST API looks something like this:
filename
form wsafter anas
Szekó_József " " []
fideszes " " [{"lemma": "fideszes", "tag": "[/Adj][Nom]", "morphana": "Fidesz[/N]=fidesz+es[_Adjz:s/Adj]=es+[Nom]=", "readable": "Fidesz[/N]=fidesz + es[_Adjz:s/Adj] + [Nom]", "twolevel": "f:F i:i d:d e:e s:s z:z :[/N] e:e s:s :[_Adjz:s/Adj] :[Nom]"}, {"lemma": "fideszes", "tag": "[/N][Nom]", "morphana": "Fidesz[/N]=fidesz+es[_Nz:s/N]=es+[Nom]=", "readable": "Fidesz[/N]=fidesz + es[_Nz:s/N] + [Nom]", "twolevel": "f:F i:i d:d e:e s:s z:z :[/N] e:e s:s :[_Nz:s/N] :[Nom]"}]
polgármester " " [{"lemma": "polgármester", "tag": "[/N][Nom]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=", "readable": "polgár[/N] + mester[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom]"}, {"lemma": "polgármester", "tag": "[/N][Nom]", "morphana": "polgármester[/N]=polgármester+[Nom]=", "readable": "polgármester[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom]"}]
tragikus " " [{"lemma": "tragikus", "tag": "[/Adj][Nom]", "morphana": "tragikus[/Adj]=tragikus+[Nom]=", "readable": "tragikus[/Adj] + [Nom]", "twolevel": "t:t r:r a:a g:g i:i k:k u:u s:s :[/Adj] :[Nom]"}, {"lemma": "tragikus", "tag": "[/N][Nom]", "morphana": "tragikus[/N]=tragikus+[Nom]=", "readable": "tragikus[/N] + [Nom]", "twolevel": "t:t r:r a:a g:g i:i k:k u:u s:s :[/N] :[Nom]"}]
halála " " [{"lemma": "halál", "tag": "[/N][Poss.3Sg][Nom]", "morphana": "halál[/N]=halál+a[Poss.3Sg]=a+[Nom]=", "readable": "halál[/N] + a[Poss.3Sg] + [Nom]", "twolevel": "h:h a:a l:l á:á l:l :[/N] a:a :[Poss.3Sg] :[Nom]"}]
után " " [{"lemma": "után", "tag": "[/Post]", "morphana": "után[/Post]=után", "readable": "után[/Post]", "twolevel": "u:u t:t á:á n:n :[/Post]"}]
egy " " [{"lemma": "egy", "tag": "[/Det|Art.NDef]", "morphana": "egy[/Det|Art.NDef]=egy", "readable": "egy[/Det|Art.NDef]", "twolevel": "e:e g:g y:y :[/Det|Art.NDef]"}, {"lemma": "egy", "tag": "[/Num][Nom]", "morphana": "egy[/Num]=egy+[Nom]=", "readable": "egy[/Num] + [Nom]", "twolevel": "e:e g:g y:y :[/Num] :[Nom]"}, {"lemma": "egy", "tag": "[/N|Pro][Nom]", "morphana": "egy[/N|Pro]=egy+[Nom]=", "readable": "egy[/N|Pro] + [Nom]", "twolevel": "e:e g:g y:y :[/N|Pro] :[Nom]"}]
polgármester-jelölt " " [{"lemma": "polgármester-jelöl", "tag": "[/V][Pst.NDef.3Sg]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[Pst.NDef.3Sg]=t", "readable": "polgár[/N] + mester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[Pst.NDef.3Sg]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[Pst.NDef.3Sg]"}, {"lemma": "polgármester-jelölt", "tag": "[/Adj][Nom]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[_PerfPtcp/Adj]=t+[Nom]=", "readable": "polgár[/N] + mester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[_PerfPtcp/Adj] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[_PerfPtcp/Adj] :[Nom]"}, {"lemma": "polgármester-jelölt", "tag": "[/N][Nom]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=+-[Hyph:Hyph]=-+jelölt[/N]=jelölt+[Nom]=", "readable": "polgár[/N] + mester[/N] + [Nom] + -[Hyph:Hyph] + jelölt[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l t:t :[/N] :[Nom]"}, {"lemma": "polgármester-jelöl", "tag": "[/V][Pst.NDef.3Sg]", "morphana": "polgármester[/N]=polgármester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[Pst.NDef.3Sg]=t", "readable": "polgármester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[Pst.NDef.3Sg]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[Pst.NDef.3Sg]"}, {"lemma": "polgármester-jelölt", "tag": "[/Adj][Nom]", "morphana": "polgármester[/N]=polgármester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[_PerfPtcp/Adj]=t+[Nom]=", "readable": "polgármester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[_PerfPtcp/Adj] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[_PerfPtcp/Adj] :[Nom]"}, {"lemma": "polgármester-jelölt", "tag": "[/N][Nom]", "morphana": "polgármester[/N]=polgármester+[Nom]=+-[Hyph:Hyph]=-+jelölt[/N]=jelölt+[Nom]=", "readable": "polgármester[/N] + [Nom] + -[Hyph:Hyph] + jelölt[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l t:t :[/N] :[Nom]"}]
maradt " " [{"lemma": "marad", "tag": "[/V][Pst.NDef.3Sg]", "morphana": "marad[/V]=marad+t[Pst.NDef.3Sg]=t", "readable": "marad[/V] + t[Pst.NDef.3Sg]", "twolevel": "m:m a:a r:r a:a d:d :[/V] t:t :[Pst.NDef.3Sg]"}, {"lemma": "maradt", "tag": "[/Adj][Nom]", "morphana": "marad[/V]=marad+t[_PerfPtcp/Adj]=t+[Nom]=", "readable": "marad[/V] + t[_PerfPtcp/Adj] + [Nom]", "twolevel": "m:m a:a r:r a:a d:d :[/V] t:t :[_PerfPtcp/Adj] :[Nom]"}]
a " " [{"lemma": "a", "tag": "[/Det|Art.Def]", "morphana": "a[/Det|Art.Def]=a", "readable": "a[/Det|Art.Def]", "twolevel": "a:a :[/Det|Art.Def]"}, {"lemma": "a", "tag": "[/Det|Pro|(Post)]", "morphana": "a[/Det|Pro|(Post)]=a", "readable": "a[/Det|Pro|(Post)]", "twolevel": "a:a :[/Det|Pro|(Post)]"}, {"lemma": "a", "tag": "[/N][Nom]", "morphana": "a[/N|Ltr]=a+[Nom]=", "readable": "a[/N|Ltr] + [Nom]", "twolevel": "a:a :[/N|Ltr] :[Nom]"}, {"lemma": "a", "tag": "[/N|Pro|(Post)][Nom]", "morphana": "a[/N|Pro|(Post)]=a+[Nom]=", "readable": "a[/N|Pro|(Post)] + [Nom]", "twolevel": "a:a :[/N|Pro|(Post)] :[Nom]"}]
Baranya " " [{"lemma": "Baranya", "tag": "[/N][Nom]", "morphana": "Baranya[/N]=Baranya+[Nom]=", "readable": "Baranya[/N] + [Nom]", "twolevel": "B:B a:a r:r a:a n:n y:y a:a :[/N] :[Nom]"}]
megyei " " [{"lemma": "megyei", "tag": "[/Adj][Nom]", "morphana": "megye[/N]=megye+i[_Adjz:i/Adj]=i+[Nom]=", "readable": "megye[/N] + i[_Adjz:i/Adj] + [Nom]", "twolevel": "m:m e:e g:g y:y e:e :[/N] i:i :[_Adjz:i/Adj] :[Nom]"}, {"lemma": "megyei", "tag": "[/Adj][Nom]", "morphana": "megyei[/Adj]=megyei+[Nom]=", "readable": "megyei[/Adj] + [Nom]", "twolevel": "m:m e:e g:g y:y e:e i:i :[/Adj] :[Nom]"}]
településen "" [{"lemma": "település", "tag": "[/N][Supe]", "morphana": "telep[/N]=telep+ül[/V]=ül+és[_Ger/N]=és+en[Supe]=en", "readable": "telep[/N] + ül[/V] + és[_Ger/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p :[/N] ü:ü l:l :[/V] é:é s:s :[_Ger/N] e:e n:n :[Supe]"}, {"lemma": "település", "tag": "[/N][Supe]", "morphana": "telep[/N]=telep+ülés[/N]=ülés+en[Supe]=en", "readable": "telep[/N] + ülés[/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p :[/N] ü:ü l:l é:é s:s :[/N] e:e n:n :[Supe]"}, {"lemma": "település", "tag": "[/N][Supe]", "morphana": "települ[/V]=települ+és[_Ger/N]=és+en[Supe]=en", "readable": "települ[/V] + és[_Ger/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p ü:ü l:l :[/V] é:é s:s :[_Ger/N] e:e n:n :[Supe]"}, {"lemma": "település", "tag": "[/N][Supe]", "morphana": "település[/N]=település+en[Supe]=en", "readable": "település[/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p ü:ü l:l é:é s:s :[/N] e:e n:n :[Supe]"}]
, " " [{"lemma": ",", "tag": "[Punct]", "morphana": "", "readable": "", "twolevel": ""}]
a " " [{"lemma": "a", "tag": "[/Det|Art.Def]", "morphana": "a[/Det|Art.Def]=a", "readable": "a[/Det|Art.Def]", "twolevel": "a:a :[/Det|Art.Def]"}, {"lemma": "a", "tag": "[/Det|Pro|(Post)]", "morphana": "a[/Det|Pro|(Post)]=a", "readable": "a[/Det|Pro|(Post)]", "twolevel": "a:a :[/Det|Pro|(Post)]"}, {"lemma": "a", "tag": "[/N][Nom]", "morphana": "a[/N|Ltr]=a+[Nom]=", "readable": "a[/N|Ltr] + [Nom]", "twolevel": "a:a :[/N|Ltr] :[Nom]"}, {"lemma": "a", "tag": "[/N|Pro|(Post)][Nom]", "morphana": "a[/N|Pro|(Post)]=a+[Nom]=", "readable": "a[/N|Pro|(Post)] + [Nom]", "twolevel": "a:a :[/N|Pro|(Post)] :[Nom]"}]
szocialista " " [{"lemma": "szocialista", "tag": "[/Adj][Nom]", "morphana": "szocialista[/Adj]=szocialista+[Nom]=", "readable": "szocialista[/Adj] + [Nom]", "twolevel": "s:s z:z o:o c:c i:i a:a l:l i:i s:s t:t a:a :[/Adj] :[Nom]"}, {"lemma": "szocialista", "tag": "[/N][Nom]", "morphana": "szocialista[/N]=szocialista+[Nom]=", "readable": "szocialista[/N] + [Nom]", "twolevel": "s:s z:z o:o c:c i:i a:a l:l i:i s:s t:t a:a :[/N] :[Nom]"}]
Csorbai_Ferenc "" []
, " " [{"lemma": ",", "tag": "[Punct]", "morphana": "", "readable": "", "twolevel": ""}]
( "" [{"lemma": "(", "tag": "[Punct]", "morphana": "", "readable": "", "twolevel": ""}]
The expected result:
Szekó_József fideszes polgármester tragikus halál után egy polgármester-jelöl marad a Baranya megyei település , a szocialista Csorbai_Ferenc , (
Here is the script itself:
#! /bin/bash
CV=1
for file in /Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support/*
do
echo $file
tempfilename="/Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support-result/temp$CV.txt"
szoveg=$(python3 docker-py.py $file)
echo "$szoveg" >> $tempfilename
sed -E -i "" "1,2d" $tempfilename
sed -i "" '/^[[:space:]]*$/d' $tempfilename
while IFS= read -r line
do
lemmacount=$(echo "$line" | egrep -c '\[{\"lemma\": \".*?\"')
if [ "$lemmacount" -ge "1" ]
then
sp0=$(echo "$line" | egrep -o '\[{\"lemma\": \".*?\"')
sp=${sp0:12}
sp=${sp%?}
sp=$sp" "
echo -n "$sp" >> /Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support-result/kiir$CV.txt
else
sp=$(echo "$line" | sed -E 's|.*\[\](.*)\[\/.*|\1|g')
sp=$(echo $sp | sed -E 's|\s*(.*)\s*|\1|g')
echo -n "$sp " >> /Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support-result/kiir$CV.txt
fi
done < "$tempfilename"
rm $tempfilename
CV=$((CV+1))
done
Thanks for any suggestions and please be as explicit with suggestions as you can be (ideally with code suggestions), seeing that I'm all pretty new to this. Thanks again!
Upvotes: 0
Views: 70
Reputation: 14452
You need to clarify the question - sample output does not match input. However, based on some assumptions, using awk to filter/JQ to parse JSON
cat resp.txt |
awk 'BEGIN { print "[" } END { print "[] ]" } /lemma/ { sub("^[^[]*. ", ""); print $0, "," }' |
jq '.[][].lemma' |
uniq |
tr -d '"' |
tr '\n' ' '
The result looks like below. You should be able to merge them
fideszes polgármester tragikus halál után egy polgármester-jelöl polgármester-jelölt polgármester-jelöl polgármester-jelölt marad
awk is needed to extract the JSON from the response. jq to extract the lemma's.
Consider changing the output file to be JSON only. It will be much easier to use a single tool (jq) to parse it.
Upvotes: 1
Reputation: 17499
I can give you one starting point here, and it's the basic usage of grep
:
Imagine you have a file, which looks like this:
info1
info2
blabla
info3
more blabla
You want to do something with all lines, containing info
.
You seem to do the following (pseudo-code):
while readline(<filename>)
do
use "grep" to see if the word "info" is on that line. In that case, do something
done
This can be done in an easier way like this (grep
is automatically checking every line in the file):
grep "info" <filename> | do something
Upvotes: 1