Details (original message): Implement the intent core, conformance suite, catalog alignment, examples, and the intentctl diagnostics CLI.
52 lines
1.4 KiB
Go
52 lines
1.4 KiB
Go
package intent
|
|
|
|
import "strings"
|
|
|
|
// Segmenter 多意图分句扩展点:把一段输入拆成可独立识别的子句。
|
|
// 模块不自动调用;宿主在需要多意图处理时先分句、再逐句识别。
|
|
type Segmenter interface {
|
|
Segment(input string) []string
|
|
}
|
|
|
|
// RuleSegmenter 规则分句:按分隔符切分。确定性、无 I/O。
|
|
type RuleSegmenter struct {
|
|
// Delims 分隔符 rune 集合。零值使用默认中英文句读与换行。
|
|
Delims []rune
|
|
}
|
|
|
|
// NewRuleSegmenter 返回使用默认分隔符(,。;!?、,.!?; 与换行)的分句器。
|
|
func NewRuleSegmenter() *RuleSegmenter {
|
|
return &RuleSegmenter{Delims: []rune(",。;!?、,.!?;\n")}
|
|
}
|
|
|
|
// Segment 按分隔符切分并去除空白子句;无有效子句时原样返回单元素。
|
|
func (s *RuleSegmenter) Segment(input string) []string {
|
|
delimSet := s.Delims
|
|
if len(delimSet) == 0 {
|
|
delimSet = NewRuleSegmenter().Delims
|
|
}
|
|
set := make(map[rune]bool, len(delimSet))
|
|
for _, r := range delimSet {
|
|
set[r] = true
|
|
}
|
|
var out []string
|
|
var cur strings.Builder
|
|
for _, r := range input {
|
|
if set[r] {
|
|
if seg := strings.TrimSpace(cur.String()); seg != "" {
|
|
out = append(out, seg)
|
|
}
|
|
cur.Reset()
|
|
continue
|
|
}
|
|
cur.WriteRune(r)
|
|
}
|
|
if seg := strings.TrimSpace(cur.String()); seg != "" {
|
|
out = append(out, seg)
|
|
}
|
|
if len(out) == 0 {
|
|
return []string{input}
|
|
}
|
|
return out
|
|
}
|