RSS

F# to Parse UserIDs, Urls, and Hash Tags from Text

20 Jun

Here is a quick set of code to return tuples holding a unique list of twitter style UserIds, Hash Tags, and Urls from a block of text. This also takes advantage of the async workflow constructs in F# and Active Pattern matching.

 
    let (|Matches|_|) (pat:string) (inp:string) =
        let m = Regex.Matches(inp, pat) in
        if m.Count > 0 then
            Some ([ for g in m -> g.Value ])
        else
            None

    let getUrls txt =
        // Regex for URLs
        let linkPat = "(http:\/\/\S+)"
        match txt with
        | Matches linkPat urls -> urls
        | _ -> []

    let getTags txt =
        // Regex for Hash Tags
        let linkPat = "[#]+([A-Za-z0-9-_]+)"
        match txt with
        | Matches linkPat tags -> tags
        | _ -> []

    let getUsers txt =
        // Regex for Users
        let linkPat = "[@]+([A-Za-z0-9-_]+)"
        match txt with
        | Matches linkPat users -> users
        | _ -> []

    let parseTxtForTokens txt =
        let opUrl = async {
                let urls = getUrls txt
                return urls
        }

        let opTags = async {
                let tags = getTags txt
                return tags
        }

        let opUsers = async {
                let usrs = getUsers txt
                return usrs
        }

        let items = Async.Parallel [opUrl; opTags; opUsers] |>  Async.RunSynchronously |> Array.toList
        (items.[0], items.[1], items.[2])

Advertisement
 

About chrisriz

I love technology. Especially that the promotes value in the form of entertainment and/or education. Also - I love watching UFC, eating healthy, and exercising.
Leave a comment

Posted by on June 20, 2011 in Software Development

 

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.