File size: 1,740 Bytes
8850458
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Param()

$ErrorActionPreference = 'Stop'

$root = Split-Path $PSScriptRoot -Parent
$src = Join-Path $root 'data' 'toeic_vocabulary.json'
$dst = Join-Path $root 'data' 'toeic_vocabulary_view.json'

if (!(Test-Path $src)) { throw "找不到資料檔:$src" }

$obj = Get-Content -Path $src -Raw -Encoding UTF8 | ConvertFrom-Json

# 支援兩種結構:
# 1) 頂層陣列(新結構)
# 2) 含 vocabulary_by_importance(舊結構)

$items = @()
if ($obj -is [System.Array]) {
  $items = $obj
} else {
  $groups = $obj.vocabulary_by_importance.PSObject.Properties
  foreach ($prop in $groups) {
    $importance = $prop.Name
    foreach ($it in $prop.Value) {
      # 注入 importance_group 以統一欄位
      $it | Add-Member -NotePropertyName importance_group -NotePropertyValue $importance -Force
      $items += $it
    }
  }
}

$out = @()
foreach ($r in $items) {
  # 保持欄位型別簡單:純字串、整數
  $pos = $r.parts_of_speech
  if ($null -eq $pos) { $pos = @() }
  if ($pos -isnot [System.Array]) { $pos = @($pos) }
  $out += [pscustomobject]@{
    english_word       = [string]$r.english_word
    chinese_definition = [string]$r.chinese_definition
    star_rating        = [int]$r.star_rating
    category           = [string]$r.category
    parts_of_speech    = ($pos -join '|')
    word_forms_json    = if ($r.word_forms) { ($r.word_forms | ConvertTo-Json -Depth 20 -Compress) } else { $null }
    examples_json      = if ($r.examples) { ($r.examples | ConvertTo-Json -Depth 20 -Compress) } else { $null }
    importance_group   = [string]$r.importance_group
  }
}

$out | ConvertTo-Json -Depth 5 | Set-Content -Path $dst -Encoding UTF8
Write-Host "已輸出 Viewer 友善 JSON -> $dst" -ForegroundColor Green