<commit_before>package sokudoku

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"time"

	"github.com/mattn/go-tty"
)

func Run(wait int) error {
	p, err := phraseInit()
	if err != nil {
		return err
	}
	defer p.Destroy()

	// Get a line from stdin.
	reader := bufio.NewScanner(os.Stdin)
	line := make(chan string, 1024)
	go func() {
		for reader.Scan() {
			line <- reader.Text()
		}
		close(line)
	}()

	// Parse a line.
	word := make(chan string, 1024)
	go func() {
		for {
			l, ok := <-line
			if !ok {
				break
			}
			res, err := p.Parse(l)
			if err != nil {
				break
			}
			for _, w := range res {
				word <- w
			}
		}
		close(word)
	}()

	// Output a word.
	oerr := outputWord(word, wait)
	if oerr != nil {
		return oerr
	}

	return reader.Err()
}

func outputWord(word <-chan string, wait int) error {
	if DEBUG {
		//-tags debug
		for {
			w, ok := <-word
			if !ok {
				break
			}
			fmt.Println("   ", w)
			time.Sleep(time.Duration(wait) * time.Millisecond)
		}
	} else {
		tty, err := tty.Open()
		if err != nil {
			return err
		}
		defer func() {
			for {
				fmt.Println("\n\n  Exit. Please press ENTER.")
				r, _ := tty.ReadRune()
				if r == 13 {
					break
				}
			}
			tty.Close()
		}()

		key_queue := make(chan rune)
		go func() {
			for {
				r, _ := tty.ReadRune()
				key_queue <- r
			}
		}()

		fmt.Println("    .")

		// 停止フラグ
		stop := false
	loop:
		for {
			if stop {
				// in pause
				r := <-key_queue
				if r == 32 {
					// pressed Space
					stop = false
				}
			} else {
				select {
				case r := <-key_queue:
					switch r {
					case 3: // Ctrl-C
						break loop
					case 91: // [
						wait = int(math.Max(float64(wait)*0.9, 10))
					case 93: // ]
						wait = int(math.Min(float64(wait)*1.1, 2000))
					case 32: // Space
						// TODO puase
						stop = true
					}
				case w, ok := <-word:
					if !ok {
						break loop
					}
					fmt.Printf("\r%s                        ", w)
					time.Sleep(time.Duration(wait) * time.Millisecond)
				}
			}
		}
	}
	return nil
}<commit_msg>fixed comments<commit_after>package sokudoku

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"time"

	"github.com/mattn/go-tty"
)

func Run(wait int) error {
	p, err := phraseInit()
	if err != nil {
		return err
	}
	defer p.Destroy()

	// Get a line from stdin.
	reader := bufio.NewScanner(os.Stdin)
	line := make(chan string, 1024)
	go func() {
		for reader.Scan() {
			line <- reader.Text()
		}
		close(line)
	}()

	// Parse a line.
	word := make(chan string, 1024)
	go func() {
		for {
			l, ok := <-line
			if !ok {
				break
			}
			res, err := p.Parse(l)
			if err != nil {
				break
			}
			for _, w := range res {
				word <- w
			}
		}
		close(word)
	}()

	// Output a word.
	oerr := outputWord(word, wait)
	if oerr != nil {
		return oerr
	}

	return reader.Err()
}

func outputWord(word <-chan string, wait int) error {
	if DEBUG {
		//-tags debug
		for {
			w, ok := <-word
			if !ok {
				break
			}
			fmt.Println("   ", w)
			time.Sleep(time.Duration(wait) * time.Millisecond)
		}
	} else {
		tty, err := tty.Open()
		if err != nil {
			return err
		}
		defer func() {
			for {
				fmt.Println("\n\n  Exit. Please press ENTER.")
				r, _ := tty.ReadRune()
				if r == 13 {
					break
				}
			}
			tty.Close()
		}()

		key_queue := make(chan rune)
		go func() {
			for {
				r, _ := tty.ReadRune()
				key_queue <- r
			}
		}()

		fmt.Println("    .")

		// pause flag
		stop := false
	loop:
		for {
			if stop {
				// in pause
				r := <-key_queue
				if r == 32 {
					// pressed Space
					stop = false
				}
			} else {
				select {
				case r := <-key_queue:
					switch r {
					case 3: // Ctrl-C
						break loop
					case 91: // [
						wait = int(math.Max(float64(wait)*0.9, 10))
					case 93: // ]
						wait = int(math.Min(float64(wait)*1.1, 2000))
					case 32: // Space
						stop = true
					}
				case w, ok := <-word:
					if !ok {
						break loop
					}
					fmt.Printf("\r%s                        ", w)
					time.Sleep(time.Duration(wait) * time.Millisecond)
				}
			}
		}
	}
	return nil
}
{'fraction_non_alphanumeric': 0.16197530864197532, 'fraction_numerical': 0.016296296296296295, 'mean_word_length': 1.744579945799458, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6088144', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1600, 'n_words': 404}