Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have added a space in every raw on the second column of my dictionary which is delimited by this: |

Now, how can I remove the duplicates in a sentence in VB.NET:


a a a and the best way to write write a paper is to buy a research paper.

the the best place to buy buy a paper is to go go online.



OK, where the words are coming from is from a ListView after performing an Autocompletion. The ListView provides the suggestions from the Dictionary. When you click on an item, it adds it to a RichTextBox on the current line.
Posted
Updated 17-Dec-15 2:14am
v5
Comments
Patrice T 23-Nov-15 19:05pm    
It is nice to show examples, but showing corrections too would be better.
Patrice T 23-Nov-15 19:14pm    
Reread your question, it is nonsense because of the untold.
What is doing a dictionary here ?
[no name] 23-Nov-15 19:17pm    
OK, one second I forgot to Add that I am Autocompleting from a ListView.
[no name] 23-Nov-15 19:23pm    
Check now.
[no name] 24-Nov-15 11:09am    
I think you people are negative, readers will see that your not helpful at all. Keep your ideas to yourselves then!

1 solution

This problem is based around duplicates AND their position. There are many ways to remove duplicates but the position factor changes things.

You will have to have some form of loop or aggregate function:
C#
const string input = @"a a a and the best way to write write a paper is to buy a research paper.

the the best place to buy buy a paper is to go go online."

string RemoveDuplicateWords(){
  List<string> resultRows = new List<string>();

  string[] rows = input.split('\n');

  foreach(var row in rows){

    resultRows.Add(row.Split(' ').Aggregate((current,next)=>current.Split(' ').Last==next?current:current+' '+next));
  } 
  
  return string.Join("\n",resultRows);
  
}
</string></string>



Some issues:

This does have some grammatical issues where two words may, and should appear together:

My Brother said that he hadn't had anything to eat, but in fact he had had dinner. <-correct

It is true that that that that that man used in that sentence is wrong. <- also correct The first and fourth (and sixth) 'that's are relative pronouns, the second and fifth 'that's are adjectives and the third 'that' is a noun. All 'that's in the previous sentence are nouns.
It can be written better:
It is true that that 'that' that that man used in that sentence is wrong. <- '' is allowed to clarify uncommon or uncertain nouns. The other 'that's stand as we have to assume that several men used several 'that's and we are pointing out the incorrect one.


Now: If anyone points out a use of the word 'that' that I have used incorrectly, this post might become so recursively meta that the universe will collapse in on itself.
YOU HAVE BEEN WARNED!

UPDATE:

And also in VB #^_^#

Converted with http://codeconverter.sharpdevelop.net/SnippetConverter.aspx[^]
VB
	Const  input As String = "a a a and the best way to write write a paper is to buy a research paper." & vbCr & vbLf & " " & vbCr & vbLf & "the the best place to buy buy a paper is to go go online."

Public Function RemoveDuplicateWords() As String

	Dim resultRows As New List(Of String)()

	Dim rows As String() = input.Split(ControlChars.Lf)

	For Each row As var In rows

		resultRows.Add(row.Split(" "C).Aggregate(Function(current, [next]) If(current.Split(" "C).Last = [next], current, current + " "C + [next])))
	Next

	Return String.Join(vbLf, resultRows)

End Function
 
Share this answer
 
v4
Comments
[no name] 17-Dec-15 9:08am    
Hi, how can we convert this into VB? I am not yet that good in C
Andy Lanng 17-Dec-15 9:13am    
updated ^_^
[no name] 17-Dec-15 9:21am    
Are you good with ignore functions from a contextmenustrip?
Andy Lanng 17-Dec-15 9:22am    
never heard of them :/
[no name] 17-Dec-15 9:23am    
OK, Nice time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900