digit_to_word() {
	case $1 in
	4) echo "four" ;;
	10) echo "ten" ;;
	14) echo "fourteen" ;;
	*) echo "$1" ;;
	esac
}
 
write_input() {
	cat <<'EOF'
I have two apples
He has 4 apples 
They have 10 pizzas
4 score and 14 years ago
EOF
}

regex='([[:space:]])([0-9]+)([[:space:]])'

write_input |
while IFS= read -r line; do
  line=" ${line} "  # pad with space so first and last words work consistently
  while [[ $line =~ $regex ]]; do       # loop while at least one replacement is pending
    pre_space=${BASH_REMATCH[1]}                # whitespace before the word, if any
    word=${BASH_REMATCH[2]}                     # actual word to replace
    post_space=${BASH_REMATCH[3]}               # whitespace after the word, if any
    replace=$(digit_to_word "$word")  # new word to use
    in=${pre_space}${word}${post_space}         # old word padded with whitespace
    out=${pre_space}${replace}${post_space}     # new word padded with whitespace
    line=${line//$in/$out}                      # replace old w/ new, keeping whitespace
  done
  line=${line#' '}; line=${line%' '}            # remove the padding we added earlier
  printf '%s\n' "$line"                         # write the output line
done