esconj

Conjugates Spanish verbs in the terminal
git clone git://mattcarlson.org/repos/esconj.git
Log | Files | Refs | README

esconj (4861B)


      1 #!/bin/bash
      2 # Title       : esconj
      3 # Description : Conjugates Spanish verbs in the terminal.
      4 # Author	  : Matthew Carlson <matt@mattcarlson.org>
      5 # Date        : 2020-09-03
      6 # Version     : 0.1
      7 # Usage		  : esconj [options]
      8 # Notes       : Should handle common verbs. No guarantees for obscure verbs.
      9 
     10 #    Copyright (C) 2020 Matthew Carlson <matt@mattcarlson.org>
     11 #
     12 #    This program is free software: you can redistribute it and/or modify
     13 #    it under the terms of the GNU General Public License as published by
     14 #    the Free Software Foundation, either version 3 of the License, or
     15 #    (at your option) any later version.
     16 #
     17 #    This program is distributed in the hope that it will be useful,
     18 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
     19 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     20 #    GNU General Public License for more details.
     21 #
     22 #    You should have received a copy of the GNU General Public License
     23 #    along with this program.  If not, see <https://www.gnu.org/licenses/>.
     24 
     25 NL='
     26 '
     27 # file from https://github.com/ghidinelli/fred-jehle-spanish-verbs
     28 VERBS='/usr/share/esconj/verbs.csv'
     29 WORDS="$(cat ${VERBS})"
     30 
     31 check() {
     32     verb="$(printf '%s\n' "${1}" | tr '[:upper:]' '[:lower:]')"
     33 
     34     # check if valid spanish verb
     35     if ! printf '%s\n' "${verb}" | grep -q '^[a-z]*\(ar\|er\|ir\)\{1\}\(se\)\?$'; then
     36         printf '%s\n' "Verb is in incorrect format.${NL}Verb must be a word ending in -ar, -er, -ir (with -se in the case of reflexives)."
     37         exit 1
     38     fi
     39 
     40     # handle suffixes
     41     suffix="${verb#"${verb%??}"}"
     42     case "${suffix}" in
     43         # -se means reflexive
     44         se)
     45             # check if reflexive version of verb exists
     46             if search_dict "${verb}"; then
     47                 conjugate "${verb}"
     48             # if not, check without -se
     49             else
     50                 check "${verb%??}"
     51             fi
     52             ;;
     53         # standard -ar, -er, -ir
     54         ar|er|ir)
     55             search_dict "${verb}"
     56             conjugate "${verb}"
     57             ;;
     58     esac
     59 }
     60 
     61 conjugate() {
     62     # declare arrays which will store conjugation table info
     63     declare -A conjugations
     64     declare -a tenses
     65 
     66     # get all rows containing verb
     67     rows="$(awk -v verb="${1}" -F '","|^"|"$' '{sub("^\"","")} $1==verb' "${VERBS}")"
     68 
     69     # store info in arrays
     70     for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18; do
     71         tenses[${i}]=$(for j in 5 8 9 10 11 12 13; do
     72         conjugations[${i},${j}]="$(printf '%s\n' "${rows}" | awk -v col=${j} -F '","|^"|"$' '{print $col}' | head -${i} | tail +${i})"
     73         [ -n "${conjugations[${i},${j}]}" ] && printf '%s\n' "${conjugations[${i},${j}]}" || printf '%s\n' '-'
     74         done)
     75     done
     76 
     77     print_mood_or_aspect 'Indicativo'
     78     print_tenses 1 5
     79 
     80     print_mood_or_aspect 'Subjuntivo'
     81     print_tenses 11 3
     82 
     83     print_mood_or_aspect 'Aspecto Perfectivo'
     84     print_tenses 6 5
     85 
     86     print_mood_or_aspect 'Aspecto Perfectivo de Subjuntivo'
     87     print_tenses 14 3
     88 
     89     print_mood_or_aspect 'Imperativo (Afirmativo y Negativo)'
     90     print_tenses 17 2
     91 }
     92 
     93 print_tenses() {
     94     tmp=''
     95 
     96     # print tenses in neat columns
     97     for i in "${tenses[@]:${1}:${2}}"; do
     98         tmp=$(paste <(printf '%b\n' "${tmp}") <(printf '%s\n' "${i}") | column -s $'\t' -t)
     99     done
    100 
    101     # print pronouns
    102     paste <(printf '\e[1;32m%s\e[m\n' ''                  \
    103                                       'yo'                \
    104                                       'tu'                \
    105                                       'él/ella/Ud.'       \
    106                                       'nosotros'          \
    107                                       'vosotros'          \
    108                                       'ellos/ellas/Uds.') \
    109     <(printf '%b\n' "${tmp}") | column -s $'\t' -t
    110 
    111     printf '\n'
    112 }
    113 
    114 print_mood_or_aspect() { printf '\e[1;32m%s\n' "${1}" ; }
    115 
    116 search_dict() {
    117     # grep csv for verb
    118     # NOTE: also excludes the word 'er'
    119     if ! printf '%s\n' "${WORDS}" | grep -w "${1}" | grep -qvw 'er'; then
    120         printf '%s\n' "Verb not found.${NL}If you entered a reflexive verb, try it without the -se ending."
    121         exit 1
    122     else
    123         return 0
    124     fi
    125 }
    126 
    127 usage() {
    128     printf '%s\n' "Usage: esconj [options]"
    129     printf '%s\n' "Options:
    130     -c, --conjugate  Conjugate verb
    131     -h, --help       Print this message and exit"
    132 }
    133 
    134 main() {
    135     [ ${#} -eq 0 ] && usage
    136 
    137     for arg in "${@}"; do
    138         # check for dashed argument
    139         [ "${arg%"${arg#?}"}" = '-' ] || continue
    140 
    141         case "${1}" in
    142             -c|--conjugate)
    143                 check "${2}"
    144                 shift 2
    145                 ;;
    146             -h|--help)
    147                 usage
    148                 break
    149                 ;;
    150             *)
    151                 printf '%s\n' "Invalid argument: ${1}"
    152                 usage
    153                 break
    154                 ;;
    155         esac
    156     done
    157 }
    158 
    159 main "${@}"