Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There may be a better way, but this is how I would go about solving the problem.</p> <p>You will need to create a tree structure that contains your dictionary of words to be replaced. The class may be something like:</p> <pre><code>public class Node { public Dictionary&lt;char, Node&gt; Children; public bool IsWord; } </code></pre> <p>Using a dictionary for the Children may not be the best choice, but it provides the easiest example here. Also, you will need a constructor to initialize the <code>Children</code> field. The <code>IsWord</code> field is used to deal with the possibility that a redacted "word" may be the prefix of another redacted "word". For example, if you want to remove both "red" and "redress".</p> <p>You will build the tree from each character in each of the replacement words. For example:</p> <pre><code>public void AddWord ( string word ) { // NOTE: this assumes word is non-null and contains at least one character... Node currentNode = Root; for (int iIndex = 0; iIndex &lt; word.Length; iIndex++) { if (currentNode.Children.ContainsKey(word[iIndex]))) { currentNode = currentNode.Children[word[iIndex]; continue; } Node newNode = new Node(); currentNode.Children.Add(word[iIndex], newNode); currentNode = newNode; } // finished, mark the last node as being a complete word.. currentNode.IsWord = true; } </code></pre> <p>You'll need to deal with case sensitivity somewhere in there. Also, you only need to build the tree once, afterwards you can use it from any number of threads without worrying about locking because you will be only reading from it. (Basically, I'm saying: store it in a static somewhere.)</p> <p>Now, when you are ready to remove words from your string you will need to do the following:</p> <ul> <li>Create a StringBuilder instance to store the result</li> <li>Parse through your source string, looking for the start and stop of a "word". How you define "word" will matter. For simplicity I would suggest starting with <code>Char.IsWhitespace</code> as defining word separators.</li> <li>Once you have determined that a range of character is a "word", starting from the root of the tree, locate the child node associated with the first character in "word".</li> <li>If you do not find a child node, the entire word is added to the <code>StringBuilder</code></li> <li>If you find a child node, you continue with the next character matching against Children of the current node, until you either run out of characters or out of nodes.</li> <li>If you reach the end of the "word", check the last node's <code>IsWord</code> field. If <code>true</code> the word is excluded, do not add it to the <code>StringBuilder</code>. If <code>IsWord</code> is <code>false</code>, the word is not replaced and you add it to the <code>StringBuilder</code></li> <li>Repeat until you have exhausted the input string.</li> </ul> <p>You will also need to add word separators to the <code>StringBuilder</code>, hopefully that will be obvious as you parse the input string. If you are careful to only use the start and stop indices within the input string, you should be able to parse the entire string without creating any garbage strings.</p> <p>When all of this is done, use <code>StringBuilder.ToString()</code> to get your final result.</p> <p>You may also need to consider Unicode surrogate codepoints, but you can <em>probably</em> get away without worrying about it.</p> <p>Beware, I typed this code here directly, so syntax errors, typos and other accidental misdirections are probably included. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload