将LISP数据导入RapidMiner(CSV,...)

时间:2011-11-30 10:45:48

标签: csv lisp rapidminer

我有LISP形式的数据,我需要在RapidMiner中处理它们。我是LISP和RapidMiner的新手。 RapidMiner不接受LISP(我猜它是因为它是编程语言)所以我可能需要以某种方式将LISP表单转换为CSV或类似的东西。代码的一个小例子:

(def-instance Adelphi
   (state newyork)
   (control private)
   (no-of-students thous:5-10)
   ...)
(def-instance Arizona-State
   (state arizona)
   (control state)
   (no-of-students thous:20+)
   ...)
(def-instance Boston-College
   (state massachusetts)
   (location suburban)
   (control private:roman-catholic)
   (no-of-students thous:5-10)
   ...)

我真的很感激任何建议。

1 个答案:

答案 0 :(得分:3)

您可以利用Lisp用户可以使用Lisp的解析器这一事实。此数据的一个问题是某些值包含冒号,在Common Lisp中使用包名称分隔符。我制作了一些有用的Common Lisp代码来解决你的问题,但是我必须通过定义合适的包来解决上面提到的问题。

这是代码,当然必须扩展(遵循已经使用过的相同模式)代码中你在问题中留下的所有内容:

(defpackage #:thous
  (:export #:5-10 #:20+))
(defpackage #:private
  (:export #:roman-catholic))

(defstruct (college (:conc-name nil))
  (name "")
  (state "")
  (location "")
  (control "")
  (no-of-students ""))

(defun data->college (name data)
  (let ((college (make-college :name (write-to-string name :case :capitalize))))
    (loop for (key value) in data
       for string = (remove #\| (write-to-string value :case :downcase))
       do (case key
            (state (setf (state college) string))
            (location (setf (location college) string))
            (control (setf (control college) string))
            (no-of-students (setf (no-of-students college) string))))
    college))

(defun read-data (stream)
  (loop for (def-instance name . data) = (read stream nil nil)
     while def-instance
     collect (data->college name data)))

(defun print-college-as-csv (college stream)
  (format stream
          "~a~{,~a~}~%"
          (name college)
          (list (state college)
                (location college)
                (control college)
                (no-of-students college))))

(defun data->csv (in out)
  (let ((header (make-college :name "College"
                              :state "state"
                              :location "location"
                              :control "control"
                              :no-of-students "no-of-students")))
    (print-college-as-csv header out)
    (dolist (college (read-data in))
      (print-college-as-csv college out))))

(defun data-file-to-csv (input-file output-file)
  (with-open-file (in input-file)
   (with-open-file (out output-file
                        :direction :output
                        :if-does-not-exist :create
                        :if-exists :supersede)
     (data->csv in out))))

主要功能是data-file-to-csv,加载此代码后,可以在Common Lisp REPL中使用(data-file-to-csv "path-to-input-file" "path-to-output-file")调用。

编辑:一些额外的想法

实际上更容易,而不是为冒号的所有值添加包定义,以进行正则表达式搜索并替换数据以在所有值周围添加引号(“)。这将使Lisp将它们解析为字符串在这种情况下,可以删除行for string = (remove #\| (write-to-string value :case :downcase)),并在string语句的所有行中将value替换为case

由于数据的高度规律性,实际上甚至不需要正确解析Lisp定义。相反,您可以使用正则表达式提取数据。一种特别适用于基于正则表达式的文本文件转换的语言应该适用于该作业,如AWK或Perl。