#$CWD = Split-Path $MyInvocation.MyCommand.Path -parent [CmdletBinding()] param ( [Parameter(Mandatory=$true,Position=1)] [String] $input_file, [Parameter(Position=2)] [String] $output_file = (Split-Path $MyInvocation.MyCommand.Path -parent)+'\SoreOrderCreationPluginUserDic.xml' ) #$input_file = '.\input\yuumei_msime.txt' #$input_file = '.\input\nicoime\nicoime_msime.txt' #$output_file = $CWD+'\output\SoreOrderCreationPluginUserDic.xml' #$temp_dir = $env:TEMP+'\conv_dic' #$temp_dir $temp_file = [System.IO.Path]::GetTempFileName() #Write-Debug $temp_file ## (将来実装) xml設定読み込み ## (将来実装) 引数チェック ## ファイルチェック(テキストデータかどうか?) ## ファイル文字コード変換 Get-Content $input_file | Out-File -FilePath $temp_file -Encoding 'unicode' ## フォーマットごとのファイル変換方法設定 #### for MS-IME $SCRIPT:FormatXML = [xml]@' `t ! 1 0 '@ #$SCRIPT:FormatXML #($SCRIPT:FormatXML.format.type)[0].commentHead #$query = "/format/type" #$SCRIPT:FormatXML.SelectNodes($query) ## 変換後ファイルのスカドラさんユーザ辞書形式変換 function ConvTo-XML { [CmdletBinding()] param ( [Parameter(ValueFromPipeline=$true,Mandatory=$true,Position=1)] [String] $text, [Parameter(Position=2)] [String] $output, [Parameter(Position=3)] [String] $type = 'MSIME' ) begin { #$SCRIPT:FormatXML.format.type.psbase.ChildNodes[0] $OutputXML = New-Object System.Xml.XmlDocument [System.Xml.XmlDeclaration]$XmlDecl = $OutputXML.CreateXmlDeclaration("1.0","UTF-8",$Null) $rootElement = $OutputXML.Createelement("ArrayOfWordKanaPair") $rootElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") $rootElement.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") #$query = "/format/type[@name='$type']" #Write-Debug $query #$tempXML = $SCRIPT:FormatXML.SelectNodes($query) #$tempXML # Debug #$delimter = $tempXML.delimter.psbase.InnerText #$commentHead = $tempXML.commentHead.psbase.InnerText #$wordPosition = $tempXML.wordPosition.psbase.InnerText #$kanaPosition = $tempXML.kanaPosition.psbase.InnerText $delimter = "`t" $commentHead = "!" $wordPosition = 1 $kanaPosition = 0 } process { if ($text -notmatch "^$commentHead") { $textarray = $text.split("$delimter") # Write-Debug $textarray $word = $textarray[$wordPosition] $kana = $textarray[$kanaPosition] # Write-Debug $word # Write-Debug $kana $newWordKanaPair = $OutputXML.CreateElement("WordKanaPair") $newWordKanaPair.SetAttribute("Enabled", "true") $newWord = $OutputXML.CreateElement("Word") $newWord.InnerText = $word $newKana = $OutputXML.CreateElement("Kana") $newKana.InnerText = $kana [void]$newWordKanaPair.AppendChild($newWord) [void]$newWordKanaPair.AppendChild($newKana) [void]$rootElement.AppendChild($newWordKanaPair) } else { # Write-Debug $text } } end { $OutputXML.AppendChild($rootElement) $OutputXML.Save($output) } } ## ファイル保存 Get-Content $temp_file | Select-String -Pattern "\S" | ConvTo-XML -output $output_file -Debug #$ResultXML = [System.Xml.XmlDocument](Get-Content $temp_file | Select-String -Pattern "\S" | ConvTo-XML -Debug) #$ResultXML.Save($output_file) Remove-Item $temp_file