MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / segmentChinese

Function segmentChinese

packages/node-runtime/src/nlp/segmenter.ts:96–118  ·  view source on GitHub ↗
(
  text: string,
  options: { posFilterMode?: PosFilterMode; customPosTags?: string[]; dictType?: DictType } = {}
)

Source from the content-addressed store, hash-verified

94}
95
96function segmentChinese(
97 text: string,
98 options: { posFilterMode?: PosFilterMode; customPosTags?: string[]; dictType?: DictType } = {}
99): string[] {
100 const { posFilterMode = 'meaningful', customPosTags, dictType = 'default' } = options
101 const cleaned = cleanText(text)
102 if (!cleaned) return []
103
104 try {
105 const jieba = getJieba(dictType)
106 if (posFilterMode === 'all') return jieba.cut(cleaned, false)
107 const tagged = jieba.tag(cleaned)
108 const allowedTags = posFilterMode === 'custom' && customPosTags ? new Set(customPosTags) : MEANINGFUL_POS_TAGS
109 return tagged.filter((item) => allowedTags.has(item.tag)).map((item) => item.word)
110 } catch (error) {
111 console.error('[NLP] Chinese segmentation failed:', error)
112 try {
113 return getJieba('default').cut(cleanText(text), false)
114 } catch {
115 return cleaned.split('')
116 }
117 }
118}
119
120function segmentEnglish(text: string): string[] {
121 const cleaned = cleanText(text)

Callers 1

segmentFunction · 0.85

Calls 4

cleanTextFunction · 0.90
getJiebaFunction · 0.85
tagMethod · 0.80
errorMethod · 0.65

Tested by

no test coverage detected