As a follow up on yesterdays post I'm going to show how easy it is to integrate some third party functionality in to a web application to provide "automatic tagging" using libots.

To do this, we're going to use the Chicken FFI. Now this example is really so short that there's no point in splitting it into multiple blocks, so here we go:

;; FFI example
(import foreign)

; Add the libots header
(foreign-declare "#include <libots-1/ots/libots.h>")

; Declare the function we're going to use
(define ots-text-topics
  (foreign-lambda c-string "ots_text_topics" c-string c-string int))

; And here's another useful one
(define ots-summarize
  (foreign-lambda* c-string* ((c-string str) (int percent))
                   "OtsArticle* article = ots_new_article();
                    ots_load_xml_dictionary(article, \"en\");
                    ots_parse_stream(str, strlen(str), article);
                    ots_grade_doc(article);
                    ots_highlight_doc(article, percent);
                    size_t outlen = 0;
                    char *text = ots_get_doc_text(article, &outlen);
                    ots_free_article(article);
                    C_return(text);"))

And now to use it (the automatic tagging part):

;; OTS tagging
(let* ((num-tags 3)
       (body "This is the post you want to tag")
       (tags (ots-text-topics body "en" num-tags)))
  (display (string-append "The tags found are: " tags)))

Now all that's left is to compile it:

csc code.scm `pkg-config --cflags --libs glib-2.0 libots-1 libxml-2.0`

That's really all there is to it!


(Maybe) Related posts: