Skip to content

Commit

Permalink
🔥BREAKING CHANGES おパッケージインポート時に乱数の初期化をしないように変更いたしましたわ🔥 #71 (#79)
Browse files Browse the repository at this point in the history
* 🔥 BREAKING_CHANGES パッケージインポート時の乱数初期化をしないように変更 #71

* 💚 Exampleコードを追加

* fix comment

* fix example

* fix readme example

* fix
  • Loading branch information
jiro4989 committed Aug 14, 2022
1 parent ae72600 commit 141355a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
22 changes: 14 additions & 8 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,29 @@ $ ojosama -charcode sjis testdata/sample1_sjis.txt

Goのコードとして使う場合は以下のように使用します。

一部変換ロジックの中でランダムな選択を行うため、乱数のシード値を設定します。

[source,go]
----
package main
import (
"fmt"
"fmt"
"math/rand"
"time"
"github.com/jiro4989/ojosama"
"github.com/jiro4989/ojosama"
)
func main() {
s := "ハーブがありました!"
text, err := ojosama.Convert(s, nil)
if err != nil {
panic(err)
}
fmt.Println(text)
rand.Seed(time.Now().UnixNano())
s := "ハーブがありました!"
text, err := ojosama.Convert(s, nil)
if err != nil {
panic(err)
}
fmt.Println(text)
}
----

Expand Down
4 changes: 4 additions & 0 deletions cmd/ojosama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package main
import (
"fmt"
"io"
"math/rand"
"os"
"time"

"github.com/jiro4989/ojosama"
"golang.org/x/text/encoding/japanese"
Expand Down Expand Up @@ -113,6 +115,8 @@ func main() {
}

func run(s string, args *CmdArgs) (int, error) {
rand.Seed(time.Now().UnixNano())

text, err := ojosama.Convert(s, nil)
if err != nil {
return exitStatusConvertError, err
Expand Down
4 changes: 4 additions & 0 deletions examples/sample1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package main

import (
"fmt"
"math/rand"
"time"

"github.com/jiro4989/ojosama"
)

func main() {
rand.Seed(time.Now().UnixNano())

s := "ハーブがありました!"
text, err := ojosama.Convert(s, nil)
if err != nil {
Expand Down
18 changes: 8 additions & 10 deletions ojosama.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import (
"math/rand"
"regexp"
"strings"
"time"

"github.com/ikawaha/kagome-dict/ipa"
"github.com/ikawaha/kagome/v2/tokenizer"
Expand All @@ -54,9 +53,9 @@ import (

// ConvertOption はお嬢様変換時のオプショナルな設定。
type ConvertOption struct {
// 句点を!に変換する機能をOFFにする。句点を!に変換してしまうと変換元の文章
// のニュアンスを破壊する可能性があるため、オプションパラメータで無効にでき
// るようにする
// 句点を!に変換する機能をOFFにする。
// 句点を!に変換してしまうと変換元の文章のニュアンスを破壊する可能性があるため、
// オプションパラメータで無効にできるようにする
DisableKutenToExclamation bool

forceAppendLongNote forceAppendLongNote // 単体テスト用のパラメータ
Expand Down Expand Up @@ -86,17 +85,16 @@ var (
shuffleElementsKutenToExclamation = []string{"。", "。", "!", "❗"}
)

func init() {
rand.Seed(time.Now().UnixNano())
}

// Convert はテキストを壱百満天原サロメお嬢様風の口調に変換して返却する。
//
// 簡単に説明すると「ハーブですわ!」を「おハーブですわ~~!!!」と変換する。
// それ以外にもいくつかバリエーションがある。
//
// opt は挙動を微調整するためのオプショナルなパラメータ。不要であれば nil を渡せ
// ば良い。
// opt は挙動を微調整するためのオプショナルなパラメータ。
// 不要であれば nil を渡せば良い。
//
// 一部変換の途中でランダムに要素を選択するため、
// 呼び出し側で乱数のシードの初期化を行うこと。
func Convert(src string, opt *ConvertOption) (string, error) {
t, err := tokenizer.New(ipa.Dict(), tokenizer.OmitBosEos())
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions ojosama_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
package ojosama

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/jiro4989/ojosama/internal/chars"
"github.com/stretchr/testify/assert"
)

func ExampleConvert() {
rand.Seed(time.Now().UnixNano())
got, err := Convert("ハーブです", nil)

fmt.Println("got: " + got)
fmt.Println(fmt.Sprintf("err: %v", err))

// Output:
// got: おハーブですわ
// err: <nil>
}

func TestConvert(t *testing.T) {
opt := &ConvertOption{
DisableKutenToExclamation: true,
Expand Down

0 comments on commit 141355a

Please sign in to comment.