soiz1 commited on
Commit
f884343
·
verified ·
1 Parent(s): 347431d

Update detection_ja.sh

Browse files
Files changed (1) hide show
  1. detection_ja.sh +21 -23
detection_ja.sh CHANGED
@@ -1,38 +1,36 @@
1
  #!/bin/bash
2
 
3
- # 対象拡張子
4
  extensions="js|mjs|jsx|json|txt|html|htm|json5|md|css"
5
 
6
  # 一時ファイル
7
  TMP_RESULT=$(mktemp)
8
 
9
- # 検出処理
10
  find . -type f -regextype posix-extended -regex ".*\.($extensions)$" | while read -r file; do
11
- # UTF-8 に変換して読み取り(iconvで変換、読めなければスキップ)
12
- content=$(iconv -f UTF-8 -t UTF-8 "$file" 2>/dev/null)
13
- if [[ -z "$content" ]]; then
14
- continue
15
- fi
16
-
17
- 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:]]*' | \
18
- perl -CSD -pe '
19
- # スペースや記号で分割(日本語のまとまりを維持)
20
- s/([^\p{Han}\p{Hiragana}\p{Katakana}a-zA-Z]+)/\n/g;
21
- s/^\s+|\s+$//g;
22
- ' | grep -P '[\p{Hiragana}\p{Katakana}\p{Han}]' | sort | uniq)
23
 
24
- if [[ -n "$matches" ]]; then
25
- echo "\"$file\": {" >> "$TMP_RESULT"
26
- echo "$matches" | while read -r line; do
27
- escaped=$(echo "$line" | sed 's/"/\\"/g')
28
- echo " \"$escaped\": []," >> "$TMP_RESULT"
29
- done
30
- sed -i '$ s/,$//' "$TMP_RESULT"
31
- echo "}," >> "$TMP_RESULT"
 
32
  fi
33
  done
34
 
35
- # JSON出力
36
  if [[ -s "$TMP_RESULT" ]]; then
37
  echo "{"
38
  sed '$ s/,$//' "$TMP_RESULT"
 
1
  #!/bin/bash
2
 
3
+ # 対象の拡張子(| 区切り)
4
  extensions="js|mjs|jsx|json|txt|html|htm|json5|md|css"
5
 
6
  # 一時ファイル
7
  TMP_RESULT=$(mktemp)
8
 
9
+ # ファイル検索・処理(サブディレクトリも含む)
10
  find . -type f -regextype posix-extended -regex ".*\.($extensions)$" | while read -r file; do
11
+ # UTF-8として読み込めるファイルのみ処理
12
+ if iconv -f UTF-8 -t UTF-8 "$file" -o /dev/null 2>/dev/null; then
13
+ matches=$(cat "$file" | \
14
+ grep -oP '[^\s[:cntrl:]]*[\p{Hiragana}\p{Katakana}\p{Han}a-zA-Z]*[\p{Hiragana}\p{Katakana}\p{Han}]+[^\s[:cntrl:]]*' | \
15
+ perl -CSD -pe '
16
+ # 記号・スペースなどで分割(日本語のかたまりを保つ)
17
+ s/([^\p{Han}\p{Hiragana}\p{Katakana}a-zA-Z]+)/\n/g;
18
+ s/^\s+|\s+$//g;
19
+ ' | grep -P '[\p{Hiragana}\p{Katakana}\p{Han}]' | sort | uniq)
 
 
 
20
 
21
+ if [[ -n "$matches" ]]; then
22
+ echo "\"$file\": {" >> "$TMP_RESULT"
23
+ echo "$matches" | while read -r line; do
24
+ escaped=$(echo "$line" | sed 's/"/\\"/g')
25
+ echo " \"$escaped\": []," >> "$TMP_RESULT"
26
+ done
27
+ sed -i '$ s/,$//' "$TMP_RESULT"
28
+ echo "}," >> "$TMP_RESULT"
29
+ fi
30
  fi
31
  done
32
 
33
+ # JSON形式で表示
34
  if [[ -s "$TMP_RESULT" ]]; then
35
  echo "{"
36
  sed '$ s/,$//' "$TMP_RESULT"