Matlab @ 07 January 2011, “1 Comment”

Well some time ago, I was looking at ways to compile MATLAB code for non-MATLAB users and then I sidetracked somehow and wound up writing a small function that opens up a browser window to a random Wikipedia article related to some input topic; the relation is italicized as it is being ascertained in a rather dumb but convenient fashion, defined simply as whether the topic appears as a link in a Wikipedia article of the input.

The code, short descriptions of the component functions and a demo follow:

function randwiki(subject)
html=urlread(['http://en.wikipedia.org/wiki/' char(subject)],'get',{'term','urlread'});
expr='"/wiki/((?!File:)(?!Wikipedia:)(?!Special:)(?!Talk:)(?!Portal)(?!Main_Page)(?!Help:)(?!Digital_object_identifier).+?)"';
[match tokens]=regexp(html,expr,'match','tokens');
web(['http://www.wikipedia.org/wiki/' char(tokens{randi(length(tokens))})],'-browser')

urlread

The function takes some given input topic, concatenates it with the wiki directory and then stores the source of the webpage in a character array called html.

regexp

The function finds all matches to the regex pattern “/wiki/(.+?)” which do not contain any of the lookahead keywords (?!lookaheads), which I’ve determined to be generally unrelated, and stores the (.+?) in an array called tokens.

web

The function opens up a webpage to the address specified by the concatenation of the wiki directory and a random token.

demo