2016-10-18 11 views
0

私はこの文を持っていると言うので、同じ項目を辞書に2回格納する必要があります: 'Hi Example Test Hi'これは1,2,3,1を出力する必要があります最初の位置を単語で保存する必要があります。ここで私が持っているコードは、これまでです:同じ項目の2つを辞書に格納する方法

If System.Text.RegularExpressions.Regex.IsMatch(userInput.Text, "^[a-zA-Z\s]+$") Then 

     Dim userString As String = userInput.Text 'Refers to the textbox's text. 
     Dim count As Integer 'Declares the count variable, which will be added to the dictionary as the word's position. 
     Dim d As New Dictionary(Of String, Integer) 'Declares the dictionary I will iterate through in the future. 
     Dim d2 As New Dictionary(Of String, Integer) 
     Dim wordString = userString.ToLower().Split(" "c) 'Puts the input into lower case and splits it apart by detecting each space after each word. 

     For Each word In wordString 

      If (d.ContainsKey(word)) Then 

      Else 
       d.Add(word, count) 'Adds each word to the dictionary along with the position. 
       count = count + 1 'Adds to the count variable to iterate through the dictionary. 
      End If 
     Next 

     For Each de In d 
      For Each dee In d2 
       output.Text &= de.Value + 1 & ", " & dee.Value + 1 & ", " & Environment.NewLine 'Gathers each word from the dictionary, referencing it by using 'de' and retrieving the key and value. 
      Next 
     Next 

    Else 

     MessageBox.Show("Your Sentence Is Invalid, It Must Only Contains Letters.") 'Displays a message if the sentence they inputted is invalid. 

    End If 

おかげで、

マット

+1

あなたの名前を付けてください。より良い変数..これは読むときには難しいですよねあなたは 'd'、' d2'、 'de'、' dee'を使います。 –

+1

私は辞書があなたの仕事に適していないと思う、そのキー値のペアを一度格納するため、私はその数と位置を単語を格納するカスタムクラスのリストを使用することをお勧めします... – Markus

答えて

0

多分このような何か:

Dim userString As String = "Hi Example Test Hi" 
Dim count As Integer = 1 'Declares the count variable, which will be added to the dictionary as the word's position. 
Dim d As New List(Of WordCountPos) 'Declares the dictionary I will iterate through in the future. 
Dim wordString = userString.ToLower().Split(" "c) 'Puts the input into lower case and splits it apart by detecting each space after each word. 

For Each word In wordString 

    If d.FindIndex(Function(x) String.Equals(x.Word, word)) <> -1 Then 

     d.Find(Function(x) String.Equals(x.Word, word)).Positions.Add(count) 

    Else 

     d.Add(New WordCountPos(word, 1, New List(Of Integer)({count}))) 'Adds each word to the dictionary along with the position 

    End If 

    count = count + 1 'Adds to the count variable to iterate through the dictionary. 

Next 

Dim output As String = "" 
For Each de In d 
    output = String.Concat(output, Environment.NewLine, "Word: ", de.Word, " Count: ", de.Count, ", Positions:", String.Join("|", de.Positions.ToArray)) 
Next 
Console.WriteLine(output) 

クラス:

Public Class WordCountPos 

    Public Sub New(w As String, c As String, pos As List(Of Integer)) 

     Me.wcp_w = w 
     Me.wcp_c = c 
     Me.wcp_pos = pos 

    End Sub 

    Private wcp_w As String 
    Public Property Word() As String 
     Get 
      Return wcp_w 
     End Get 
     Set(ByVal value As String) 
      wcp_w = value 
     End Set 
    End Property 

    Private wcp_c As Integer 
    Public Property Count() As Integer 
     Get 
      Return wcp_c 
     End Get 
     Set(ByVal value As Integer) 
      wcp_c = value 
     End Set 
    End Property 

    Private wcp_pos As List(Of Integer) 
    Public Property Positions() As List(Of Integer) 
     Get 
      Return wcp_pos 
     End Get 
     Set(ByVal value As List(Of Integer)) 
      wcp_pos = value 
     End Set 
    End Property 

End Class 
関連する問題