OCRで読み取ったものをRでXML文書にする

OCRで読み取った文書をTEI準拠XMLにしたい。OCRの結果には、ゴミのような記号や不要なスペースが入ってきてしまったりします。それをきれいにして、TEIヘッダをつけるくらいのところまでを自動でできるようにしたいと思います。授業で教わったことを思いだしながら、見よう見まねでRのコードを書いてみました。日本語の文書は今のところあつかっていないので、ローマ字の文書を念頭において書きました。

必要なものは、Rが使える環境とOCRで読み取った文書(ローマ字)です。材料の文書は、ひとつのTEI文書にまとめたいものごとに、ひとつのファイルにします。たとえば、1ページごとにファイルを作っていたら、それぞれ別のTEI文書として成形されてしまいます。

最初の行で、材料の文書を指定してください("foo.txt"とあるところ。拡張子は".txt"でなくても、何でも最終的にはxml文書になります)。コンマで区切って複数指定しても大丈夫です。それぞれ別々のTEI文書として成形されます。

やることはつぎのとおりです。

  • "^•°#<>«»"といった、ゴミのような記号を削除します。
  • アンダースコア("_")、タブ記号などをスペースに変換します。
  • コロンとセミコロン(;:)の前にスペースをひとつ入れます。
  • 余計なスペース(ふたつ以上連続しているもの。ハイフン、ピリオド、コンマ、改行記号の前。左括弧の後、右括弧の前)を削除します。
  • ダブルクオートを<q>タグにします。開始と終了のタグを揃えます。
  • "&"をエスケープ文字に変えます。

わたしの作業用につくったので、ほかのひとにとってはやりたくない処理も含まれているかもしれません(記号の削除やコロンの前にスペースを入れるなど)。

もちろん、このコードで文書の修正がすべて済むわけではまったくありません。あくまでも、作業の補助用のコードなので、やはり修正は自分の目で確認しながら行なう必要があります。また、<front>, <body>, <back>などのTEIのタグも、自分で入力しなければなりません。

一括でタグづけしたい単語を指定して変換するとか、そういったものをこのコードに書き加えていくといいと思います(タグの変換用のコードはまた別にしておいたほうがきれいかもしれません)。

初心者ですので、たいへん拙い技術しかありません。改善できる点などありましたら、教えていただけると幸いです。もっとスマートなやりかたがあるんじゃないかと思います。(gsubの連続…)



text <- c("foo.txt")

for (f in text){
cat(f, "is being processed...\n")
current_file <- scan(f, what="char", sep="\n", quiet=T)

current_file <- gsub("(\\\\|\\^|•|°|#|<|>|«|»)", "", current_file, perl=T) #delete "^" "*" "•"
current_file <- gsub("_", " ", current_file, perl=T) #replace "_" with a space
current_file <- gsub("\t+", " ", current_file, perl=T) #replace tabs with a space
current_file <- gsub("\\:", " \\:", current_file, perl=T) #insert a space before ":"
current_file <- gsub("\\;", " \\;", current_file, perl=T) #insert a space before ";"
current_file <- gsub(" + ", " ", current_file, perl=T) #remove extra space
current_file <- gsub(" +-", "-", current_file, perl=T) #remove a space before a hyphen
current_file <- gsub(" \\.", "\\.", current_file, perl=T) #erase a space before punctuation marks
current_file <- gsub(" \\,", "\\,", current_file, perl=T) #erase a space before punctuation marks
current_file <- gsub("\\.+", "\\.", current_file, perl=T) #erase repeated period
current_file <- gsub("\\( ", "\\(", current_file, perl=T) #erase a space after opening bracket
current_file <- gsub(" \\)", "\\)", current_file, perl=T) #erase a space before closing bracket
current_file <- gsub(" \n", "\n", current_file, perl=T) #erase a space before line feed

current_file <- gsub("\"", "<q>", current_file, perl=T) #replace double-quotation mark with <q>
current_file <- gsub("(<q>.*?)<q>", "\\1</q>", current_file, perl=T) #make closing </q> tag
current_file <- gsub("([^ ])(<q>)", "\\1 \\2", current_file, perl=T) #insert a space before <q> tag if not found
current_file <- gsub("<q> ", "<q>", current_file, perl=T) #erase an unnessesary space after <q>
current_file <- gsub("(</q>)([^ ])", "\\1 \\2", current_file, perl=T) #insert a space after </q> tag if not found
current_file <- gsub(" </q>", "</q>", current_file, perl=T) #erase an unnessesary space before </q>

current_file <- gsub("&", "&amp;", current_file, perl=T) #replace "&" with "&amp;"

head <- "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>\n<TEI>\n<teiHeader>\n <!--[TEI Header information]-->\n</teiHeader>\n<text>\n" #make a teiHeader
closer <- "\n</text>\n</TEI>"
tei_file <- append(head, current_file)
tei_file <- append(tei_file, closer)

filename <- gsub("(.*)\\..*", "\\1.xml", f, perl=T)
cat(tei_file, file=filename, sep="\n")
}