正文
Private Sub UserForm_Initialize()
Dim catDict As Object
Dim it
Dim i As Long
Set catDict = GetListofCategories()
For Each it In catDict.keys
lbCategory.AddItem it
Next it
lbCategory.AddItem "其他"
For i = 0 To lbCategory.ListCount - 1
lbCategory.Selected(i) = True
Next i
tbSelectedQuestion.Text = Selection.Value
End Sub
Function GetListofCategories()
Dim question As Range
Dim dict As Object
Dim wsQnA As Worksheet
Set dict = CreateObject("Scripting.Dictionary")
Set wsQnA = GetQnAWorksheet
For Each question In wsQnA.Range("C:C").SpecialCells(xlCellTypeConstants)
If question.Row > 1 Then
If Not dict.Exists(question.Value) Then
dict.Add question.Value, 1
End If
End If
Next question
Set GetListofCategories = dict
End Function
Function GetQnAWorksheet() As Worksheet
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Sheets
If ws.Name Like "知识库*" And ws.Visible Then
Set GetQnAWorksheet = ws
Exit Function
End If
Next ws
MsgBox "没有找到知识库工作表!", vbCritical + vbOKOnly, "错误"
Set GetQnAWorksheet = Nothing
End Function
删除不必要的词
Function RemoveStopWords(sentence As String) As Collection
If IsEmpty(stopWords) Then stopWords = Split("的;是;如何;什么;所有;?;-;,;", ";")
Dim stopR As Range
Dim r As Variant
Dim col As Collection
Set col = New Collection
Dim w
For Each w In Split(sentence, " ")
If Not (IsNumeric(Trim(w))) Then
w = Trim(w)
If Len(w) > 0 Then col.Add w
End If
Next w
Dim i
For Each r In stopWords
For i = col.Count To 1 Step -1
If UCase(col(i)) = UCase(r) Then
col.Remove i
End If
Next i
Next r
Set RemoveStopWords =