2012-02-28 53 views
2

我正在尋找一種方法來在Access數據庫中使用Visual Basic中的函數刪除停用詞。如何使用Visual Basic從字符串中刪除停止詞?

今天我只是做了幾個替換,但我知道這不是正確的方式,因爲我不知道我是否將停止詞作爲一個詞或一個詞刪除。

任何幫助將是偉大的,我只是無法找到任何方式在VB上做到這一點。

+1

你的意思是VBA,即與MS Access一起使用。如何包含空格,例如「* in *」?它會錯過開始和結束的wprds,但可能就足夠了。 – Fionnuala 2012-02-28 20:44:14

回答

2

好的,你的意思是這樣的,對吧?

OutputString = Replace("They answered the question", "the", "") 

這將從短語中取代所有出現的「the」,包括單詞「他們」的一部分。

最簡單的解決辦法是把空格前後的單詞後更換:

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 

只要短語中的單詞總是用空格分隔,這就有效。
當可以有其他分隔符而不是空白時,您必須做更多。

例如,您可以循環使用分隔符列表並對每個分隔符執行函數,而不是對函數中的空格進行硬編碼。
我現在不會將此代碼顯示爲代碼,但您明白了。

+0

哇你真正理解我的問題,我感謝你的回答。由於我有很多停用詞,我會做一個while循環來使這個函數遍歷一個數組! – user1238765 2012-02-28 22:00:34

+3

@ user1238765:很高興提供幫助。如果我的回答對你有幫助,這將是很好,如果你[接受](http://stackoverflow.com/faq#howtoask)它! – 2012-02-28 22:20:49

相關問題