Book Image

Clojure for Data Science

By : Henry Garner
Book Image

Clojure for Data Science

By: Henry Garner

Overview of this book

Table of Contents (18 chapters)
Clojure for Data Science
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Preface
Index

Extracting the data


After you run the preceding script, the articles will be unzipped to the directory data/reuters-sgml. Each .sgm file in the extract contains around 1,000 short articles that have been wrapped in XML-style tags using Standard Generalized Markup Language (SGML). Rather than write our own parser for the format, we can make use of the one already written in the Lucene text indexer.

(:import [org.apache.lucene.benchmark.utils ExtractReuters])

(defn sgml->txt [in-path out-path]
  (let [in-file  (clojure.java.io/file in-path)
        out-file (clojure.java.io/file out-path)]
    (.extract (ExtractReuters. in-file out-file))))

Here we're making use of Clojure's Java interop to simply call the extract method on Lucene's ExtractReuters class. Each article is extracted as its own text file.

This code can be run by executing:

lein extract-reuters

on the command line within the project directory. The output will be a new directory, data/reuters-text, containing over 20,000 individual...