[golang] 英文のテキストをセンテンス単位で改行する

カテゴリ: 未分類 | タグ: , ,

複数行で構成されている英文テキストのファイルを、センテンス単位(=ピリオド区切り)で、独立した行に変換するGoのプログラムです

入力テキスト例

Alice was beginning to get very tired of sitting by her sister on the bank, and of
having nothing to do. So she was considering in her own mind.

変換結果

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.
So she was considering in her own mind.

プログラム

package main

import (
    "fmt"
    "io/ioutil"
    "os"
)

func main() {
    lines := getFile("input2.txt")
    splitSentence(lines)
}

func getFile(filename string) string {
    lines, err := ioutil.ReadAll(os.Stdin)
    if err != nil {
        panic("failed opening file " + filename)
    }

    return string(lines)
}

func splitSentence(lines string) {
    skipFlg := false
    for _, r := range lines {
        if skipFlg {
            skipFlg = false
            continue
        }

        switch string(r) {
        case ".":
            fmt.Println(string(r))
            skipFlg = true
        case "\n":
            fmt.Print(" ")
        default:
            fmt.Print(string(r))
        }
    }
}

実行例

$ cat input.txt
Alice was beginning to get very tired of sitting by her sister on the bank, and of
having nothing to do. So she was considering in her own mind.

$ cat input.txt | go run main.go
Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do.
So she was considering in her own mind.

Amazonでおトクに買い物する方法
AmazonチャージでポイントGET


Amazonは買いもの前にAmazonギフト券をチャージしてポイントをゲットしないと損!

こちらもおススメ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です