我正在寻找一种方法,使用 Access DB 内的 Visual Basic 中的函数来删除停用词。
今天我只是做了几次替换,但我知道这不是正确的方法,因为我不知道是将停用词作为单词还是在单词内删除。
任何帮助都会很棒,我只是找不到任何方法在 VB 上执行此操作。
最佳答案
好吧,你的意思是这样的,对吧?
OutputString = Replace("They answered the question", "the", "")
这会替换短语中所有出现的“the”,包括单词“These”的一部分。
最简单的解决方案是在要替换的单词前后添加空格:
OutputString = Replace("They answered the question", " the ", "")
这适用于我上面示例中的短语,但当该单词出现在短语的开头或结尾时,它就不起作用。
对于这些情况,您需要做更多的事情。像这样的事情:
Public Function RemoveStopWords( _
ByVal Phrase As String, _
ByVal WordToRemove As String _
) As String
Dim RetVal As String
Dim Tmp As String
'remove the word in the middle of the phrase
RetVal = Replace(Phrase, " " & WordToRemove & " ", " ")
'remove the word at the beginning
Tmp = WordToRemove & " "
If Left(RetVal, Len(Tmp)) = Tmp Then
RetVal = Mid(RetVal, Len(Tmp) + 1)
End If
'remove the word at the end
Tmp = " " & WordToRemove
If Right(RetVal, Len(Tmp)) = Tmp Then
RetVal = Left(RetVal, Len(RetVal) - Len(Tmp))
End If
RemoveStopWords = RetVal
End Function
只要短语中的单词始终用空格分隔,此方法就有效。
当除了空格之外还可以有其他分隔符时,您必须做更多的事情。
例如,您可以循环遍历分隔符列表并为每个分隔符执行函数,而不是对函数中的空格进行硬编码。
我现在不会将其显示为代码,但您已经明白了。
关于ms-access - 如何使用 Visual Basic 从字符串中删除停用词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9489130/