Skip to main content

實做Domain功能-anki-2

Anki

回傳的卡片,我也可以想像他有以下功能

Note

1. 檢查指定欄位是否產生了聲音

在第一項需求中,我們要判斷指定欄位有沒有聲音,這個判斷方式只要判斷文字內有沒有

[sound:xxxxx]

就可以判斷了 而像這種字串判斷的內容,最簡單的作法就是交給chatgpt來幫我們歸納這個邏輯,提示詞如下

希望可以判斷 [sound:test.mp3] 
其中的test.mp3會自由的改變
希望可以用正規表達式來判斷這段文字是否存在
語言使用golang

最終得出了這樣的程式碼

func (n Note) HasSound(s string) bool {  
value := n.Fields[s].Value
pattern := `\[sound:([^\]]+)\]`
re := regexp.MustCompile(pattern)
matches := re.FindAllStringSubmatch(value, -1)
return matches != nil
}

2. 檢查指定欄位是否有值了 這個就簡單很多了

func (n Note) HasValue(s string) bool {  
value := n.Fields[s].Value
return value != ""
}
  1. 取得指定欄位的輸出(把一些不相干的內容移除,例如[[Japanese Pitch Accent]]的讀音標記) 第三個我們一樣先分析需求 如果透過[Japanese Pitch Accent]產生的內容會像以下內容
エアコン<br><!-- user_accent_start --><br><hr><br><svg class=\\\"pitch\\\" width=\\\"172px\\\" height=\\\"75px\\\" viewBox=\\\"0 0 172 75\\\"><text x=\\\"5\\\" y=\\\"67.5\\\" style=\\\"font-size:20px;font-family:sans-serif;fill:#000;\\\">エ</text><text x=\\\"40\\\" y=\\\"67.5\\\" style=\\\"font-size:20px;font-family:sans-serif;fill:#000;\\\">ア</text><text x=\\\"75\\\" y=\\\"67.5\\\" style=\\\"font-size:20px;font-family:sans-serif;fill:#000;\\\">コ</text><text x=\\\"110\\\" y=\\\"67.5\\\" style=\\\"font-size:20px;font-family:sans-serif;fill:#000;\\\">ン</text><path d=\\\"m 16,30 35,-25\\\" style=\\\"fill:none;stroke:#000;stroke-width:1.5;\\\"></path><path d=\\\"m 51,5 35,0\\\" style=\\\"fill:none;stroke:#000;stroke-width:1.5;\\\"></path><path d=\\\"m 86,5 35,0\\\" style=\\\"fill:none;stroke:#000;stroke-width:1.5;\\\"></path><path d=\\\"m 121,5 35,0\\\" style=\\\"fill:none;stroke:#000;stroke-width:1.5;\\\"></path><circle r=\\\"5\\\" cx=\\\"16\\\" cy=\\\"30\\\" style=\\\"opacity:1;fill:#000;\\\"></circle><circle r=\\\"5\\\" cx=\\\"51\\\" cy=\\\"5\\\" style=\\\"opacity:1;fill:#000;\\\"></circle><circle r=\\\"5\\\" cx=\\\"86\\\" cy=\\\"5\\\" style=\\\"opacity:1;fill:#000;\\\"></circle><circle r=\\\"5\\\" cx=\\\"121\\\" cy=\\\"5\\\" style=\\\"opacity:1;fill:#000;\\\"></circle><circle r=\\\"5\\\" cx=\\\"156\\\" cy=\\\"5\\\" style=\\\"opacity:1;fill:#000;\\\"></circle><circle r=\\\"3.25\\\" cx=\\\"156\\\" cy=\\\"5\\\" style=\\\"opacity:1;fill:#fff;\\\"></circle></svg><!-- user_accent_end -->

而我將這些內容放到chatgpt中,請他分離出內容,他給出的解方是

  1. 使用正規表達式來刪除 和 之間的內容
  2. 刪除HTML標籤

這麼做完之後就可以順利取得需要的內容

func (n Note) GetValue(s string) string {  
value := n.Fields[s].Value

// 使用正規表達式來刪除 <!-- user_accent_start --> 和 <!-- user_accent_end --> 之間的內容
pattern := `<!-- user_accent_start -->(.*?)<!-- user_accent_end -->`
re := regexp.MustCompile(pattern)
result := re.ReplaceAllString(value, "")

// 刪除HTML標籤
reHTML := regexp.MustCompile(`<[^>]+>`)
result = reHTML.ReplaceAllString(result, "")

return result
}

完整的程式碼如下 https://github.com/kevinyay945/anki-support/blob/v0.2.1