RTF を削除した後、ファイルを解析して CSV に変換するのはそれほど難しくありませんでした。2009 年の ICD-9 の疾病と処置コードをすべて含む解析済みファイルは次のとおり
です: http://www.jacotay.com/files/Disease_and_ProcedureCodes_Parsed.zip /files/RTFApp.zip
基本的に、これは 2 段階のプロセスです。CDC FTP サイトからファイルを取得し、そこから RTF を削除してから、RTF のないファイルを選択して CSV ファイルに解析します。ここでのコードは、結果を取得する必要があったのは 1 回だけだったので、かなり大雑把です。
外部リンクがダウンした場合の解析アプリのコードは次のとおりです (ファイル名を選択してボタンをクリックして実行できるフォームへのバックエンド)
Public Class Form1
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
Dim p As New OpenFileDialog With {.CheckFileExists = True, .Multiselect = False}
Dim pResult = p.ShowDialog()
If pResult = Windows.Forms.DialogResult.Cancel OrElse pResult = Windows.Forms.DialogResult.Abort Then
Exit Sub
End If
txtFileName.Text = p.FileName
End Sub
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim pFile = New IO.FileInfo(txtFileName.Text)
Dim FileText = IO.File.ReadAllText(pFile.FullName)
FileText = RemoveRTF(FileText)
IO.File.WriteAllText(Replace(pFile.FullName, pFile.Extension, "_fixed" & pFile.Extension), FileText)
End Sub
Function RemoveRTF(ByVal rtfText As String)
Dim rtBox As System.Windows.Forms.RichTextBox = New System.Windows.Forms.RichTextBox
'// Get the contents of the RTF file. Note that when it is
'// stored in the string, it is encoded as UTF-16.
rtBox.Rtf = rtfText
Dim plainText = rtBox.Text
Return plainText
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pFile = New IO.FileInfo(txtFileName.Text)
Dim FileText = IO.File.ReadAllText(pFile.FullName)
Dim DestFileLine As String = ""
Dim DestFileText As New System.Text.StringBuilder
'Need to parse at lines with numbers, lines with all caps are thrown away until next number
FileText = Strings.Replace(FileText, vbCr, "")
Dim pFileLines = FileText.Split(vbLf)
Dim CurCode As String = ""
For Each pLine In pFileLines
If pLine.Length = 0 Then
Continue For
End If
pLine = pLine.Replace(ChrW(9), " ")
pLine = pLine.Trim
Dim NonCodeLine As Boolean = False
If IsNumeric(pLine.Substring(0, 1)) OrElse (pLine.Length > 3 AndAlso (pLine.Substring(0, 1) = "E" OrElse pLine.Substring(0, 1) = "V") AndAlso IsNumeric(pLine.Substring(1, 1))) Then
Dim SpacePos As Int32
SpacePos = InStr(pLine, " ")
Dim NewCode As String
NewCode = ""
If SpacePos >= 3 Then
NewCode = Strings.Left(pLine, SpacePos - 1)
End If
If SpacePos < 3 OrElse Strings.Mid(pLine, SpacePos - 1, 1) = "." OrElse InStr(NewCode, "-") > 0 Then
NonCodeLine = True
Else
If CurCode <> "" Then
DestFileLine = Strings.Replace(DestFileLine, ",", ",")
DestFileLine = Strings.Replace(DestFileLine, """", """).Trim
DestFileText.AppendLine(CurCode & ",""" & DestFileLine & """")
CurCode = ""
DestFileLine = ""
End If
CurCode = NewCode
DestFileLine = Strings.Mid(pLine, SpacePos + 1)
End If
Else
NonCodeLine = True
End If
If NonCodeLine = True AndAlso CurCode <> "" Then 'If we are not on a code keep going, otherwise check it
Dim pReg As New System.Text.RegularExpressions.Regex("[a-z]")
Dim pRegCaps As New System.Text.RegularExpressions.Regex("[A-Z]")
If pReg.IsMatch(pLine) OrElse pLine.Length <= 5 OrElse pRegCaps.IsMatch(pLine) = False OrElse (Strings.Left(pLine, 3) = "NOS" OrElse Strings.Left(pLine, 2) = "IQ") Then
DestFileLine &= " " & pLine
Else 'Is all caps word
DestFileLine = Strings.Replace(DestFileLine, ",", ",")
DestFileLine = Strings.Replace(DestFileLine, """", """).Trim
DestFileText.AppendLine(CurCode & ",""" & DestFileLine & """")
CurCode = ""
DestFileLine = ""
End If
End If
Next
If CurCode <> "" Then
DestFileLine = Strings.Replace(DestFileLine, ",", ",")
DestFileLine = Strings.Replace(DestFileLine, """", """).Trim
DestFileText.AppendLine(CurCode & ",""" & DestFileLine & """")
CurCode = ""
DestFileLine = ""
End If
IO.File.WriteAllText(Replace(pFile.FullName, pFile.Extension, "_parsed" & pFile.Extension), DestFileText.ToString)
End Sub
クラス終了