このサイト(https://qiita.com/kurehajime/items/a922d42dff5e0f03d32c)にある駄洒落を検出するプログラムを実装したく、GitHubから落として実行したのですがエラーが出ます!
"github.com/ikawaha/kagome/tokenizer"がインストールされていないのは理解できるのですが、どのようにしてインストールすれば良いのか理解できないため、教えてくださると助かります!!!
GO言語
1package dajarep 2 3import ( 4 "math" 5 "regexp" 6 "strings" 7 8 "github.com/ikawaha/kagome/tokenizer" 9) 10 11func init() { 12 tokenizer.SysDic() 13} 14 15//単語 16type word struct { 17 str string 18 kana string 19 wtype string 20} 21 22//文章 23type sentence struct { 24 str string 25 kana string 26 words []word 27} 28 29//Dajarep :駄洒落を返す 30func Dajarep(text string) (dajares []string, debugStrs []string) { 31 sentences := getSentences(text) 32 for i := 0; i < len(sentences); i++ { 33 if ok, kana := isDajare(sentences[i]); ok == true { 34 dajares = append(dajares, sentences[i].str) 35 debugStrs = append(debugStrs, kana) 36 } 37 } 38 return dajares, debugStrs 39} 40 41//駄洒落かどうかを評価する。 42func isDajare(sen sentence) (bool, string) { 43 words := sen.words 44 for i := 0; i < len(words); i++ { 45 w := words[i] 46 if w.wtype == "名詞" && len([]rune(w.kana)) > 1 { 47 rStr := regexp.MustCompile(w.str) 48 rKana := regexp.MustCompile(fixWord(w.kana)) 49 hitStr := rStr.FindAllString(sen.str, -1) 50 hitKana := rKana.FindAllString(sen.kana, -1) 51 hitKana2 := rKana.FindAllString(fixSentence(sen.kana), -1) 52 //ある単語における 原文の一致文字列数<フリガナでの一致文字列数 → 駄洒落の読みが存在 53 if len(hitStr) < int(math.Max(float64(len(hitKana)), float64(len(hitKana2)))) { 54 return true, w.kana 55 } 56 } 57 } 58 return false, "" 59} 60 61//置き換え可能な文字を考慮した正規表現を返す。 62func fixWord(text string) string { 63 text = strings.Replace(text, "ッ", "[ツッ]?", -1) 64 text = strings.Replace(text, "ァ", "[アァ]?", -1) 65 text = strings.Replace(text, "ィ", "[イィ]?", -1) 66 text = strings.Replace(text, "ゥ", "[ウゥ]?", -1) 67 text = strings.Replace(text, "ェ", "[エェ]?", -1) 68 text = strings.Replace(text, "ォ", "[オォ]?", -1) 69 text = strings.Replace(text, "ズ", "[ズヅ]", -1) 70 text = strings.Replace(text, "ヅ", "[ズヅ]", -1) 71 text = strings.Replace(text, "ヂ", "[ジヂ]", -1) 72 text = strings.Replace(text, "ジ", "[ジヂ]", -1) 73 re := regexp.MustCompile("([アカサタナハマヤラワャ])ー") 74 text = re.ReplaceAllString(text, "$1[アァ]?") 75 re = regexp.MustCompile("([イキシチニヒミリ])ー") 76 text = re.ReplaceAllString(text, "$1[イィ]?") 77 re = regexp.MustCompile("([ウクスツヌフムユルュ])ー") 78 text = re.ReplaceAllString(text, "$1[ウゥ]?") 79 re = regexp.MustCompile("([エケセテネへメレ])ー") 80 text = re.ReplaceAllString(text, "$1[エェ]?") 81 re = regexp.MustCompile("([オコソトノホモヨロヲョ])ー") 82 text = re.ReplaceAllString(text, "$1[ウゥオォ]?") 83 text = strings.Replace(text, "ャ", "[ヤャ]", -1) 84 text = strings.Replace(text, "ュ", "[ユュ]", -1) 85 text = strings.Replace(text, "ョ", "[ヨョ]", -1) 86 text = strings.Replace(text, "ー", "[ー]?", -1) 87 return text 88} 89 90//本文から省略可能文字を消したパターンを返す。 91func fixSentence(text string) string { 92 text = strings.Replace(text, "ッ", "", -1) 93 text = strings.Replace(text, "ー", "", -1) 94 text = strings.Replace(text, "、", "", -1) 95 text = strings.Replace(text, ",", "", -1) 96 text = strings.Replace(text, " ", "", -1) 97 text = strings.Replace(text, " ", "", -1) 98 return text 99} 100 101//テキストからsentenceオブジェクトを作る。 102func getSentences(text string) []sentence { 103 var sentences []sentence 104 t := tokenizer.New() 105 106 text = strings.Replace(text, "。", "\n", -1) 107 text = strings.Replace(text, ".", "\n", -1) 108 text = strings.Replace(text, "?", "?\n", -1) 109 text = strings.Replace(text, "!", "!\n", -1) 110 text = strings.Replace(text, "?", "?\n", -1) 111 text = strings.Replace(text, "!", "!\n", -1) 112 senstr := strings.Split(text, "\n") 113 114 for i := 0; i < len(senstr); i++ { 115 tokens := t.Tokenize(senstr[i]) 116 var words []word 117 var kana string 118 for j := 0; j < len(tokens); j++ { 119 tk := tokens[j] 120 ft := tk.Features() 121 if len(ft) > 7 { 122 w := word{str: ft[6], 123 kana: ft[7], 124 wtype: ft[0], 125 } 126 words = append(words, w) 127 kana += ft[7] 128 } 129 } 130 sentences = append(sentences, 131 sentence{ 132 str: senstr[i], 133 words: words, 134 kana: kana, 135 }) 136 } 137 return sentences 138}
ターミナル
[vagrant@localhost dajarep]$ ls LICENSE README.md cmd dajarep.go dajarep_test.go makefile test.txt [vagrant@localhost dajarep]$ go build dajarep.go dajarep.go:8:2: cannot find package "github.com/ikawaha/kagome/tokenizer" in any of: /usr/lib/golang/src/github.com/ikawaha/kagome/tokenizer (from $GOROOT) /home/vagrant/go/src/github.com/ikawaha/kagome/tokenizer (from $GOPATH)
回答2件
あなたの回答
tips
プレビュー