package main
import (
"./fileio"
"fmt"
"os"
"strings"
)
type Morph struct {
surface string
base string
pos string
pos1 string
}
func parseSentence(lines []string) [][]Morph {
sentences := make([][]Morph, 0)
sentence := make([]Morph, 0)
for _, line := range lines {
if line == "EOS" {
if len(sentence) > 0 {
sentences = append(sentences, sentence)
sentence = make([]Morph, 0)
}
continue
}
if line[0] == '*' {
continue
}
//表層形\t品詞,品詞細分類1,品詞細分類2,品詞細分類3,活用形,活用型,原形,読み,発音
word := strings.Split(line, "\t")
words := strings.Split(word[1], ",")
morpheme := Morph{
surface: word[0],
base: words[6],
pos: words[0],
pos1: words[1],
}
sentence = append(sentence, morpheme)
}
return sentences
}
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: main <filepath>")
os.Exit(1)
}
lines := fileio.ReadFileAllLines(os.Args[1])
sentences := parseSentence(lines)
fmt.Println(sentences[2])
}
package fileio
import (
"bufio"
"fmt"
"io"
"os"
)
func WriteFileAllLines(fileName string, lines []string) {
file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range lines {
fmt.Fprintln(w, line)
}
w.Flush()
}
func ReadAllLine(r io.Reader) []string {
sc := bufio.NewScanner(r)
lines := []string{}
for sc.Scan() {
lines = append(lines, sc.Text())
}
return lines
}
func ReadFileAllLines(fileName string) []string {
fp, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer fp.Close()
lines := ReadAllLine(fp)
return lines
}
#!/bin/bash
mkdir fileio
mv fileio.go fileio
go run main.go