Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So... I got bored and needed something to do:</p> <pre><code>topics = { "Project", "Mary", "Mr. Moore", "Project Omega"} pcall ( require , 'luarocks.require' ) require 'lpeg' local locale = lpeg.locale ( ) local endofstring = -lpeg.P(1) local endoftoken = (locale.space+locale.punct)^1 table.sort ( topics , function ( a , b ) return #a &gt; #b end ) -- Sort by word length (longest first) local topicpattern = lpeg.P ( false ) for i = 1, #topics do topicpattern = topicpattern + topics [ i ] end function wikify ( input ) local topicsleft = { } for i = 1 , #topics do topicsleft [ topics [ i ] ] = true end local makelink = function ( topic ) if topicsleft [ topic ] then topicsleft [ topic ] = nil return "[[" .. topic .. "]]" else return topic end end local patt = lpeg.Ct ( ( lpeg.Cs ( ( topicpattern / makelink ) )* #(-locale.alnum+endofstring) -- Match topics followed by something thats not alphanumeric + lpeg.C ( ( lpeg.P ( 1 ) - endoftoken )^0 * endoftoken ) -- Skip tokens that aren't topics )^0 * endofstring -- Match adfinum until end of string ) return table.concat ( patt:match ( input ) ) end print(wikify("Mary and Mr. Moore work together on Project Omega. Mr. Moore hates both Mary and Project Omega, but Mary simply loves the Project.")..'"') print(wikify("Mary and Mr. Moore work on Project Omegality. Mr. Moore hates Mary and Project Omega, but Mary loves the Projectaaa.")..'"') </code></pre> <p>I start off my making a pattern which matches all the different topics; we want to match the longest topics first, so sort the table by word length from longest to shortest. Now we need to make a list of the topics we haven't seen in the current input. makelink quotes/links the topic if we haven't seen it already, otherwise leaves it be.</p> <p>Now for the actual lpeg stuff:</p> <ul> <li><code>lpeg.Ct</code> packs all our captures into a table (to be concated together for output)</li> <li><code>topicpattern / makelink</code> captures a topic, and passes in through our makelink function.</li> <li><code>lpeg.Cs</code> substitutes the result of makelink back in where the match of the topic was.</li> <li><code>+ lpeg.C ( ( lpeg.P ( 1 ) - locale.space )^0 * locale.space^1 )</code> if we didn't match a topic, skip a word (that is, not spaces followed by a space)</li> <li><code>^0</code> repeat.</li> </ul> <p>Hope thats what you wanted :)</p> <p>Daurn</p> <p>Note: Edited code, description no longer correct</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. VO
      singulars
      1. This table or related slice is empty.
    2. 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