How to find a reserved word in a sentence?

5

I'm creating a routine to search for reserved words in a text (people's names). I have already tested with Contains() and IndexOf() . It works well most of the time. However in some words the return does not satisfy. Example: CORALINA returns the reserved word ORAL .

Below the code:

    public JsonResult GetPalavrasReservada1(string frase)
    {
        var palavras = db.TBL_PALAVRA_RESERVADA.Where(pal => pal.PLR_ATIVO == 0).Select(i => new {i.PLR_DESC}).ToList();

        var palavrareservada = "";

        for (int i = 0; i < palavras.Count; i++)
        {
            if (frase.ToUpper().Contains(palavras[i].PLR_DESC.ToString()))
            {
                return Json(palavras[i].PLR_DESC.ToString());
            }
        }

        return Json(palavrareservada);
    }

I'm using logic or wrong methods?

    
asked by anonymous 01.11.2017 / 17:50

2 answers

0

I would use Regex with pattern \d to check for whole words in the sentence.

Regex.IsMatch("CAROLINE", @"\bORAL\b",RegexOptions.IgnoreCase); // false
Regex.IsMatch("Discurso oral", @"\bORAL\b",RegexOptions.IgnoreCase); // true
Regex.IsMatch("Nada consta contra", @"\bNada Consta\b",RegexOptions.IgnoreCase); // true

In your code, it would look like this:

if ( Regex.IsMatch(frase, "\b" + palavras[i].PLR_DESC.ToString() + "\b",RegexOptions.IgnoreCase ))
    
01.11.2017 / 21:05
1

If you find a reserved word, the algorithm for and does not find others, is this what you want?

If you just want to get whole words you have to break all the words and do the verification. But this is not so simple unless you control the content of the text earlier and can ensure it does not have certain patterns. If you can do this break the sentence just give a

01.11.2017 / 18:03