从字符串中获取子短语的所有组合

时间:2012-11-29 17:14:05

标签: vb.net string combinations

要进行某种“模糊”匹配,我需要从用户可以输入的字符串中提取子短语的所有变体。

我尝试过的几个例子似乎并没有完成整个工作 - 他们似乎只是循环并抓住并排的短语,但并不是全部分布 - 例如:

原始输入 - “重型电缆卷轴” - 在获得2个单词子短语时给出:

重型

责任电缆

电缆卷轴

但我也想要:

重型电缆

重卷轴

责任卷轴

因此在构建子短语时实际上“遗漏”了单词。理想情况下,我需要返回3&用于4字原始输入的2个单词子短语,以及4,3和& 2个用于5个字输入,依此类推。

For i As Integer = lst.Count To 1 Step -1 
    For j As Integer = 1 To i 
        Dim sb As New StringBuilder 
        sb.Append("( ") 
        For k As Integer = 0 To (lst.Count - i) 
            sb.Append(lst(j + k - 1) & ":" & j & ":" & k & ":" & i & " ") 
            Next 
        sb.Append(")") 
        rlst.Add(sb.ToString()) 
    Next 
Next

1 个答案:

答案 0 :(得分:1)

两个嵌套的for循环将为两个单词的组合

执行此操作
Dim s = "heavy duty cable reel".Split()

For i As Integer = 0 To s.Length - 2
    For k As Integer = i + 1 To s.Length - 1
        Console.WriteLine(s(i) + " " + s(k))
    Next
Next

输出

heavy duty
heavy cable
heavy reel
duty cable
duty reel
cable reel

<强>更新

这是一个版本,它可以获得从2到单词数量的所有短语长度 - 1.我为此创建了一个类。

Public Class PhraseBuilder
    Private words As String()
    Private phrases As List(Of String)

    Public Function GetList(ByVal initialPhrase As String) As List(Of String)
        words = initialPhrase.Split()
        phrases = New List(Of String)()
        For phraseLength As Integer = 2 To words.Length - 1
            AddPhrases("", 0, phraseLength)
        Next
        Return phrases
    End Function

    Private Sub AddPhrases(ByVal phrase As String, ByVal start As Integer,
                           ByVal wordCount As Integer)
        If wordCount >= 1 Then
            For i As Integer = start To words.Length - wordCount
                AddPhrases(phrase + " " + words(i), i + 1, wordCount - 1)
            Next i
        Else
            phrases.Add(phrase.TrimStart())
        End If
    End Sub
End Class

您可以使用

进行测试
Sub PrintPhrases()
    Dim phraseBuilder = New PhraseBuilder()
    Dim phrases = phraseBuilder.GetList("heavy duty cable reel")
    For Each phrase As String In phrases
        Console.WriteLine(phrase)
    Next
End Sub

结果将是

heavy duty
heavy cable
heavy reel
duty cable
duty reel
cable reel
heavy duty cable
heavy duty reel
heavy cable reel
duty cable reel

请注意,我在这里使用了递归。递归允许将for语句嵌套到任意深度。我没有使用StringBuilder,因为我们必须创建短语开头的副本。使用字符串可以更容易地完成这一操作,因为它们是不可变的。没有简单的方法来克隆StringBuilder