Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You might want to take a look at <a href="https://github.com/rhdunn/cainteoir-engine/blob/0c283e798c8141a65060c5e92f462646c2689644/tests/dictionary.py" rel="nofollow">https://github.com/rhdunn/cainteoir-engine/blob/0c283e798c8141a65060c5e92f462646c2689644/tests/dictionary.py</a>.</p> <p>I wrote this to support regular expressions in text-to-speech pronunciation dictionaries, but the regex expanding logic is self-contained. You can use it like:</p> <pre><code>import dictionary words, endings = dictionary.expand_expression('colou?r', {}) print words </code></pre> <p>Here, the second parameter is for references (i.e. named blocks) and the endings is for, e.g. <code>look{s,ed,ing}</code></p> <p>How it works ...</p> <p><code>lex_expression</code> splits the string into tokens delimited by the regex tokens <code>[]&lt;&gt;|(){}?</code>. Thus, <code>a(b|cd)efg</code> becomes <code>['a', '(', 'b', '|', 'cd', ')', 'efg']</code>. This makes it easier to parse the regex.</p> <p>The <code>parse_XYZ_expr</code> functions (along with the top-level <code>parse_expr</code>) parse the regex elements, constructing an object hierarchy that represents the regex. These objects are:</p> <ul> <li>Literal -- a literal sequence of one or more characters</li> <li>Choice -- any of the sub-expressions in the sequence (i.e. '|')</li> <li>Optional -- either the result of the expression, or not (i.e. <code>a?</code>)</li> <li>Sequence -- the sub-expressions in order</li> </ul> <p>Thus, <code>ab(cd|e)?</code> is represented as <code>Sequence(Literal('ab'), Optional(Choice(Literal('cd'), Literal('e'))))</code>.</p> <p>These classes support an <code>expand</code> method that has the form <code>expr.expand(words) =&gt; expanded</code>, e.g.:</p> <pre><code>expr = Optional('cd') print expr.expand(['ab', 'ef']) </code></pre> <p>results in:</p> <pre><code>ab abcd ef efcd </code></pre>
 

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