<commit_before>#!/usr/bin/env bash

work_dir="$(pwd)"
tools_dir="$(cd "$(dirname "$0")" && pwd)"
tweet_sh="$tools_dir/tweet.sh/tweet.sh"

source "$tweet_sh"
load_keys

log() {
  echo "$*" 1>&2
}

if [ "$TWEET_BASE_DIR" != '' ]
then
  TWEET_BASE_DIR="$(cd "$TWEET_BASE_DIR" && pwd)"
else
  TWEET_BASE_DIR="$work_dir"
fi

responses_dir="$TWEET_BASE_DIR/responses"

mkdir -p "$responses_dir"


input="$(cat |
           # normalize waves
           sed 's/〜/～/g')"
# +response filename-or-keyword(>(alias))?( +(response))?
# -response filename-or-keyword(>(alias))?( +(response))?

log 'Modifying keyword definitions...'

whitespaces=' \f\n\r\t　'
non_whitespaces='[^ \f\n\r\t　]'

operation="$(echo "$input" | $esed "s/^(${non_whitespaces}+)[$whitespaces].+$/\1/")"
keyword="$(echo "$input" |
  $esed -e "s/^${non_whitespaces}+[$whitespaces]+//" \
        -e "s/[$whitespaces]*((>|&gt;)${non_whitespaces}+)?([$whitespaces].*)?$//")"
alias=''
if echo "$input" |
     egrep "^${non_whitespaces}+[$whitespaces]+[^>&]+(>|&gt;)[$whitespaces]*${non_whitespaces}+" > /dev/null
then
  alias="$(echo "$input" |
    $esed -e "s/^${non_whitespaces}+[$whitespaces]+[^>&]+(>|&gt;)[$whitespaces]*//" \
          -e "s/(${non_whitespaces}+)[$whitespaces]*([$whitespaces].*)?$/\1/")"
fi
response="$(echo "$input" |
  $esed -e "s/^${non_whitespaces}+[$whitespaces]+[^>&$whitespaces]+([$whitespaces]*(>|&gt;)[$whitespaces]*${non_whitespaces}+)?[$whitespaces]*//")"

log "  operation: $operation"
log "  keyword  : $keyword"
log "  alias    : $alias"
log "  response : $response"

if [ "$keyword" = '' ]
then
  log "ERROR: No keyword is given."
  exit 1
fi


process_add_command() {
  local safe_keyword="$(echo "$keyword" |
                         # remove dangerous characters
                         sed "s/[!\[\]<>{}\/:?*'\"]+/_/g")"

  # if there is any file including the keyword in its name, then reuse it.
  while read path
  do
    add_definition "$path" "$alias" "$response"
    exit $?
  done < <(find "$responses_dir" -type f \
                                 -name "*${keyword}*" \
                                 -or -name "*${safe_keyword}*")

  # if there is any file including the keyword in its keyword definitions, then reuse it.
  while read path
  do
    add_definition "$path" "$alias" "$response"
    exit $?
  done < <(egrep -r "^#[$whitespaces]*${keyword}[$whitespaces]*$" "$responses_dir" | cut -d ':' -f 1)

  # otherwise, create new definition file.
  local path="$responses_dir/autoadd_${safe_basename}.txt"
  echo "# $keyword" > "$path"
  add_definition "$path" "$alias" "$response"
  exit $?
}

add_definition() {
  local path=$1
  local alias=$2
  local response=$3<commit_msg>Escape more characters<commit_after>#!/usr/bin/env bash

work_dir="$(pwd)"
tools_dir="$(cd "$(dirname "$0")" && pwd)"
tweet_sh="$tools_dir/tweet.sh/tweet.sh"

source "$tweet_sh"
load_keys

log() {
  echo "$*" 1>&2
}

if [ "$TWEET_BASE_DIR" != '' ]
then
  TWEET_BASE_DIR="$(cd "$TWEET_BASE_DIR" && pwd)"
else
  TWEET_BASE_DIR="$work_dir"
fi

responses_dir="$TWEET_BASE_DIR/responses"

mkdir -p "$responses_dir"


input="$(cat |
           # normalize waves
           sed 's/〜/～/g')"
# +response filename-or-keyword(>(alias))?( +(response))?
# -response filename-or-keyword(>(alias))?( +(response))?

log 'Modifying keyword definitions...'

whitespaces=' \f\n\r\t　'
non_whitespaces='[^ \f\n\r\t　]'

operation="$(echo "$input" | $esed "s/^(${non_whitespaces}+)[$whitespaces].+$/\1/")"
keyword="$(echo "$input" |
  $esed -e "s/^${non_whitespaces}+[$whitespaces]+//" \
        -e "s/[$whitespaces]*((>|&gt;)${non_whitespaces}+)?([$whitespaces].*)?$//")"
alias=''
if echo "$input" |
     egrep "^${non_whitespaces}+[$whitespaces]+[^>&]+(>|&gt;)[$whitespaces]*${non_whitespaces}+" > /dev/null
then
  alias="$(echo "$input" |
    $esed -e "s/^${non_whitespaces}+[$whitespaces]+[^>&]+(>|&gt;)[$whitespaces]*//" \
          -e "s/(${non_whitespaces}+)[$whitespaces]*([$whitespaces].*)?$/\1/")"
fi
response="$(echo "$input" |
  $esed -e "s/^${non_whitespaces}+[$whitespaces]+[^>&$whitespaces]+([$whitespaces]*(>|&gt;)[$whitespaces]*${non_whitespaces}+)?[$whitespaces]*//")"

log "  operation: $operation"
log "  keyword  : $keyword"
log "  alias    : $alias"
log "  response : $response"

if [ "$keyword" = '' ]
then
  log "ERROR: No keyword is given."
  exit 1
fi


process_add_command() {
  local safe_keyword="$(echo "$keyword" |
                         # remove dangerous characters
                         sed "s/[!\[\]<>{}\/\\:;?*'\"|]+/_/g")"

  # if there is any file including the keyword in its name, then reuse it.
  while read path
  do
    add_definition "$path" "$alias" "$response"
    exit $?
  done < <(find "$responses_dir" -type f \
                                 -name "*${keyword}*" \
                                 -or -name "*${safe_keyword}*")

  # if there is any file including the keyword in its keyword definitions, then reuse it.
  while read path
  do
    add_definition "$path" "$alias" "$response"
    exit $?
  done < <(egrep -r "^#[$whitespaces]*${keyword}[$whitespaces]*$" "$responses_dir" | cut -d ':' -f 1)

  # otherwise, create new definition file.
  local path="$responses_dir/autoadd_${safe_basename}.txt"
  echo "# $keyword" > "$path"
  add_definition "$path" "$alias" "$response"
  exit $?
}

add_definition() {
  local path=$1
  local alias=$2
  local response=$3
{'fraction_non_alphanumeric': 0.2243649174856295, 'fraction_numerical': 0.0037085110328203227, 'mean_word_length': 3.5365853658536586, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4249224', 'n_tokens_mistral': 2064, 'n_tokens_neox': 1986, 'n_words': 399}