如何从指定的数组/列表中查找缺失的单词?

发布于 2025-01-09 17:27:51 字数 1158 浏览 0 评论 0原文

我有一个需要验证的单词列表是否包含在数百个文档中。

我想出了如何从文档中找到的列表中突出显示/着色单词(下面的代码)。

我需要知道缺少哪些单词。

Sub HighlightWords()
    Dim vWords As Variant
    Dim sWord As Variant

    vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")

    For Each sWord In vWords
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Highlight = wdYellow
        With Selection.Find
            .Text = sWord
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next sWord
End Sub

I have a list of words I need to verify are included in hundreds of documents.

I figured out how to highlight/color words from the list which are found within the document (code below).

I need to know which words are missing.

Sub HighlightWords()
    Dim vWords As Variant
    Dim sWord As Variant

    vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")

    For Each sWord In vWords
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Highlight = wdYellow
        With Selection.Find
            .Text = sWord
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    Next sWord
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

攀登最高峰 2025-01-16 17:27:51

以下代码将在消息框中显示缺少的术语。

Sub HighlightWords()
    Dim vWords As Variant
    Dim sWord As Variant
    Dim Missing As String

    vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")

    For Each sWord In vWords
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Replacement.Highlight = wdYellow
            .Text = sWord
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            If Not .Execute(Replace:=wdReplaceAll) Then Missing = Missing & sWord & ", "
        End With
    Next sWord
    If Not Missing = vbNullString Then _
        MsgBox "Missing terms:" & vbCr & vbCr & Left(Missing, Len(Missing) - 2)
End Sub

The following code will display the missing terms in a message box.

Sub HighlightWords()
    Dim vWords As Variant
    Dim sWord As Variant
    Dim Missing As String

    vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")

    For Each sWord In vWords
        With ActiveDocument.Content.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Replacement.Highlight = wdYellow
            .Text = sWord
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = True
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            If Not .Execute(Replace:=wdReplaceAll) Then Missing = Missing & sWord & ", "
        End With
    Next sWord
    If Not Missing = vbNullString Then _
        MsgBox "Missing terms:" & vbCr & vbCr & Left(Missing, Len(Missing) - 2)
End Sub
放飞的风筝 2025-01-16 17:27:51

下面将为您提供缺失单词作为集合或数组的列表:

Option Explicit

Public Sub FindMissingWords()
    Dim vWords() As Variant
    vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")
    
    Dim MissingWords As New Collection
    
    Dim sWord As Variant
    For Each sWord In vWords
        If Not IsKeywordInRange(sWord, ThisDocument.Range) Then
            MissingWords.Add sWord
        End If
    Next sWord
    
    If MissingWords.Count > 0 Then
        MsgBox "The following " & MissingWords.Count & " words are missing: " & Join(CollectionToArray(MissingWords), ", "), vbExclamation
    Else
        MsgBox "No words are missing.", vbInformation
    End If
End Sub

Public Function IsKeywordInRange(ByVal Keyword As String, ByVal InRange As Range) As Boolean
    'check if a keyword is in the range
    
    With InRange.Find
        .Text = Keyword
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
    IsKeywordInRange = InRange.Find.Execute
End Function

Public Function CollectionToArray(ByVal Coll As Collection, Optional ByVal StartIdx As Long, Optional ByVal Size As Long) As Variant
    ' Convert a collection into an array
    
    Dim Arr() As Variant
    Dim Ci As Variant   ' Collection element
    Dim i As Long
    
    ' Make sure Size is not less than collection size
    If Size < Coll.Count Then Size = Coll.Count
    
    ' Transfer collection to array
    ReDim Arr(StartIdx To StartIdx + Size - 1)
    For Each Ci In Coll
        Arr(i) = Ci
        i = i + 1
    Next

    CollectionToArray = Arr
End Function

请注意,将文档中关键字是否存在的测试外包到函数中可以使您的代码更具可读性,并且更容易测试或重用。

The following will give you a list of the missing words as collection or array:

Option Explicit

Public Sub FindMissingWords()
    Dim vWords() As Variant
    vWords = Array("SQL query", "Selenium", "Cucumber", "Rest-Assured", "Rest assured", "REST API", "TestNG", "SVN", "Subversion", "Maven", "IntelliJ", "Ecliipse", "Confluence", "JIRA", "Sauce Labs", "GitLab", "HTML", "XPATH", "CSS", "Object Oriented Programming", "Object-Orienting Programming", "OOP")
    
    Dim MissingWords As New Collection
    
    Dim sWord As Variant
    For Each sWord In vWords
        If Not IsKeywordInRange(sWord, ThisDocument.Range) Then
            MissingWords.Add sWord
        End If
    Next sWord
    
    If MissingWords.Count > 0 Then
        MsgBox "The following " & MissingWords.Count & " words are missing: " & Join(CollectionToArray(MissingWords), ", "), vbExclamation
    Else
        MsgBox "No words are missing.", vbInformation
    End If
End Sub

Public Function IsKeywordInRange(ByVal Keyword As String, ByVal InRange As Range) As Boolean
    'check if a keyword is in the range
    
    With InRange.Find
        .Text = Keyword
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    
    IsKeywordInRange = InRange.Find.Execute
End Function

Public Function CollectionToArray(ByVal Coll As Collection, Optional ByVal StartIdx As Long, Optional ByVal Size As Long) As Variant
    ' Convert a collection into an array
    
    Dim Arr() As Variant
    Dim Ci As Variant   ' Collection element
    Dim i As Long
    
    ' Make sure Size is not less than collection size
    If Size < Coll.Count Then Size = Coll.Count
    
    ' Transfer collection to array
    ReDim Arr(StartIdx To StartIdx + Size - 1)
    For Each Ci In Coll
        Arr(i) = Ci
        i = i + 1
    Next

    CollectionToArray = Arr
End Function

Note that outsourcing the test if a keyword is in the document into a function makes your code more readable and it is easier to test or re-use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文