toeic-vocab-tw / scripts /make_viewer_json.ps1
LEE-WHITE's picture
Duplicate from kknono668/toeic-vocab-tw
8850458 verified
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