s4s-editor-beta / detection_ja.sh
soiz1's picture
Update detection_ja.sh
347431d verified
raw
history blame
1.41 kB
#!/bin/bash
# 対象拡張子
extensions="js|mjs|jsx|json|txt|html|htm|json5|md|css"
# 一時ファイル
TMP_RESULT=$(mktemp)
# 検出処理
find . -type f -regextype posix-extended -regex ".*\.($extensions)$" | while read -r file; do
# UTF-8 に変換して読み取り(iconvで変換、読めなければスキップ)
content=$(iconv -f UTF-8 -t UTF-8 "$file" 2>/dev/null)
if [[ -z "$content" ]]; then
continue
fi
matches=$(echo "$content" | grep -oE '[^[:space:][:cntrl:]]*[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}a-zA-Z]*[\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Han}]+[^[:space:][:cntrl:]]*' | \
perl -CSD -pe '
# スペースや記号で分割(日本語のまとまりを維持)
s/([^\p{Han}\p{Hiragana}\p{Katakana}a-zA-Z]+)/\n/g;
s/^\s+|\s+$//g;
' | grep -P '[\p{Hiragana}\p{Katakana}\p{Han}]' | sort | uniq)
if [[ -n "$matches" ]]; then
echo "\"$file\": {" >> "$TMP_RESULT"
echo "$matches" | while read -r line; do
escaped=$(echo "$line" | sed 's/"/\\"/g')
echo " \"$escaped\": []," >> "$TMP_RESULT"
done
sed -i '$ s/,$//' "$TMP_RESULT"
echo "}," >> "$TMP_RESULT"
fi
done
# JSON出力
if [[ -s "$TMP_RESULT" ]]; then
echo "{"
sed '$ s/,$//' "$TMP_RESULT"
echo "}"
else
echo "{}"
fi
rm "$TMP_RESULT"