C # How to avoid separation of names in .Split ()?

So basically I have this loop where every sentence in the processedSentencesList gets iterated and checked for words that exist in the entityString list. And each String entity found in each sentence is added to var valid_words.

But the entities "Harry Potter" and "Ford Card" are not added due to the sentence "sentence .Split ()".

How can I change this code so that existing objects with spaces are not divided into two words?

    List <string> entityString = new List<string>();
        entityString.Add("Harry Potter"); //A name which i do not want to split
        entityString.Add("Ford Car"); //A name which i do not want to split
        entityString.Add("Broom");
        entityString.Add("Ronald");

        List <string> processedSentencesList = new List<string>();
        processedSentencesList.Add("Harry Potter is a wizard");
        processedSentencesList.Add("Ronald had a Broom and a Ford Car");


        foreach (string sentence in processedSentencesList)
          {

                var words = sentence.Split(" ".ToCharArray()); 
                   //But it splits the names as well
                var valid_words = words.Where(w => 
                   entityStrings.Any(en_li => en_li.Equals(w)));
                    //And therefore my names do not get added to the valid_words list
          }

When it prints, I get the output right now:

Broom

Ronald

The result that I expect:

Harry Potter

Ford car

Broom

Ronald

In principle, objects with spaces between (2 or more words) are separated and therefore cannot be matched with existing objects. How to fix it?

+4
3

foreach :

List<String> valid_words = new List<String>();

foreach (string sentence in processedSentencesList)
{
    valid_words.AddRange(entityString.Where(en_li => sentence.Contains(en_li)));
}

valid_words = valid_words.Distinct().ToList();
+3

.

[A-Z]\S+(?:\s+[A-Z]\S+)?

DEMO

0

You can scroll through each element and use the String.Contains () method, which will not allow you to split search strings.

Example:

List<string> valid_words = new List<string>();

foreach (string sentence in processedSentencesList)
{
  foreach (string entity in entityString)
  {
    if (sentence.Contains(entity))
    {
      valid_words.Add(entity);
    }
  }
}
0
source

All Articles