Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code should work:</p> <pre><code>public static class StemmingUtilities { private class Node { public char? Value { get; private set; } public Node Parent { get; private set; } public List&lt;Node&gt; Children { get; private set; } public Node(char? c, Node parent) { this.Value = c; this.Parent = parent; this.Children = new List&lt;Node&gt;(); } } public static string GetRegex(IEnumerable&lt;string&gt; tokens) { var root = new Node(null,null); foreach (var token in tokens) { var current = root; for (int i = 0; i &lt; token.Length; i++) { char c = token[i]; var node = current.Children.FirstOrDefault(x =&gt; x.Value.Value == c); if (node == null) { node = new Node(c,current); current.Children.Add(node); } current = node; } } return BuildRexp(root); } private static string BuildRexp(Node root) { string s = ""; bool addBracket = root.Children.Count &gt; 1; // uncomment the following line to avoid first brakets wrapping (occurring in case of multiple root's children) // addBracket = addBracket &amp;&amp; (root.Parent != null); if (addBracket) s += "("; for(int i = 0; i &lt; root.Children.Count; i++) { var child = root.Children[i]; s += child.Value; s += BuildRexp(child); if (i &lt; root.Children.Count - 1) s += "|"; } if (addBracket) s += ")"; return s; } } </code></pre> <p><strong>Usage:</strong></p> <pre><code>var toStem1 = new[] { "what", "why", "where", "when", "which" }; string reg1 = StemmingUtilities.GetRegex(toStem1); // reg1 = "wh(at|y|e(re|n)|ich)" string[] toStem2 = new[] { "why", "abc", "what", "where", "apple", "when" }; string reg2 = StemmingUtilities.GetRegex(toStem2); // reg2 = "(wh(y|at|e(re|n))|a(bc|pple))" </code></pre> <p><strong>EDIT:</strong><br> to get <code>reg2 = "wh(y|at|e(re|n))|a(bc|pple)"</code> i.e. without the first wrapping brackets, just uncomment the marked line in <code>BuildRexp</code> method.</p>
 

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