Skip to content

Instantly share code, notes, and snippets.

@KiruyaMomochi
Last active September 7, 2021 08:17
Show Gist options
  • Save KiruyaMomochi/b3f99ef1e0f7ad795509217fd8bb19fa to your computer and use it in GitHub Desktop.
Save KiruyaMomochi/b3f99ef1e0f7ad795509217fd8bb19fa to your computer and use it in GitHub Desktop.
Checking Priconne (Taiwan) SRT data
param (
[Parameter(ParameterSetName = "json", Mandatory)]
[string]
$SrtJson,
[Parameter(ParameterSetName = "objects", Mandatory, ValueFromPipeline)]
[pscustomobject[]]
$SrtPanels
)
class SrtPanel {
[int]$reading_id
[string]$reading
[int]$read_type
[int]$panel_id
[string]$head_symbol
[string]$tail_symbol
[string]$detail_text
[int]$version
}
function Get-Bopomofo {
[OutputType([string[]])]
param (
[Parameter(Mandatory, ValueFromPipeline)]
[string]
$word
)
begin {
$bopomofo = "ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄓㄔㄕㄖㄗㄘㄙㄧㄨㄩㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦ".ToCharArray();
}
process {
if ($word -in $bopomofo) {
return $word;
}
$result = Invoke-RestMethod ("https://www.moedict.tw/uni/" + $word) -Proxy "http://127.0.0.1:10809";
$other = $result.heteronyms.definitions.def
| ForEach-Object { return [regex]::Matches($_, '「(.+?)」的異體字。') }
| ForEach-Object { return $_.Groups[1].Value }
| Foreach-Object { return Get-Bopomofo $_};
Write-Host $result.heteronyms.definitions.def;
[string[]] $result = $result.heteronyms.bopomofo | Where-Object { $_ }
$result += $other;
return $result
}
}
function Get-Rhyme {
param (
[Parameter(Mandatory, ValueFromPipeline)]
[string]
$bopomofo
)
process {
$keep = "[^ㄓㄔㄕㄖㄗㄘㄙㄧㄨㄩㄚㄛㄜㄝㄞㄟㄠㄡㄢㄣㄤㄥㄦ]";
$rhyme = [regex]::Replace($bopomofo, $keep, '');
$remove = "^[ㄓㄔㄕㄖㄗㄘㄙ]?(.+)$";
$rhyme = [regex]::Replace($rhyme, $remove, '$1');
return $rhyme;
}
}
function Check {
param (
[Parameter(Mandatory, ValueFromPipeline)]
$srt
)
begin {
$nonRhyme = "[ㄅㄆㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒ]";
}
process {
$id = $srt.reading_id;
$reading = [regex]::Replace($srt.reading, $nonRhyme, '');
$sonet_head = $srt.head_symbol;
$sonet_tail = $srt.tail_symbol;
try {
$moe_head = Get-Bopomofo ($reading[0]) | Get-Rhyme | Get-Unique;
$moe_tail = Get-Bopomofo ($reading[-1]) | Get-Rhyme | Get-Unique;
}
catch {
Write-Host "? [$id] ${reading}: $($reading[0]) $sonet_head($moe_head), $($reading[-1]) $sonet_tail($moe_tail)" -ForegroundColor Yellow;
continue;
}
if (($sonet_head -notin $moe_head) -or ($sonet_tail -notin $moe_tail)) {
Write-Host "- [$id] ${reading}:" -NoNewline -ForegroundColor Red;
Write-Host " $($reading[0]) $sonet_head" -NoNewline;
if ($sonet_head -notin $moe_head) {
Write-Host "($moe_head)" -NoNewline -ForegroundColor Red;
}
Write-Host ", $($reading[-1]) $sonet_tail" -NoNewline;
if ($sonet_tail -notin $moe_tail) {
Write-Host "($moe_tail)" -NoNewline -ForegroundColor Red;
}
Write-Host
}
else {
Write-Host "o [$id] ${reading}: $($reading[0]) $sonet_head($moe_head), $($reading[-1]) $sonet_tail($moe_tail)" -ForegroundColor DarkGray;
}
}
}
if ($SrtJson.Length -ne 0) {
$script:SrtPanels = [SrtPanel[]](Get-Content $SrtJson | ConvertFrom-Json)
}
$script:SrtPanels | Check
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment