1

我有 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)
   ...)

如果您有任何建议,我将不胜感激。

4

1 回答 1

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 "path-to-input-file" "path-to-output-file")主要功能是 data-file-to-csv,加载此代码后可以在 Common Lisp REPL 中调用。

编辑:一些额外的想法

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

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

于 2011-11-30T22:42:08.423 回答