import express from 'express'
import _ from 'lodash'
import fs from 'fs'
import { spawn } from 'child_process'

const wordsRelationshipPath = '../data/words_relationship.json'
const ldaTopicPath = '../data/lda/topic.json'
const ldaDocInfoPath = '../data/lda/doc.json'

const wordsRelationship = JSON.parse(fs.readFileSync(wordsRelationshipPath))
const ldaDocInfo = JSON.parse(fs.readFileSync(ldaDocInfoPath))
const ldaTopic = JSON.parse(fs.readFileSync(ldaTopicPath))

const apiRouter = express.Router()

apiRouter.get('/words-relationship/:limit', (req, res) => {
  res.json(_.slice(wordsRelationship, 0, req.params.limit))
})

apiRouter.get('/lda-topic', (req, res) => {
  res.json(ldaTopic)
})

apiRouter.get('/lda-doc', (req, res) => {
  res.json(ldaDocInfo)
})

const uint8ArrayToString = (data) => {
  return String.fromCharCode.apply(null, data)
}

apiRouter.post('/lda-predict', (req, res) => {
  const process = spawn('python', ['../process/LDA_predict.py', ...req.body])
  process.stdout.on('data', function (data) {
    'use strict'
    res.send(uint8ArrayToString(data))
  })
})

export default apiRouter

@@ -342,130 +342,8 @@
 th))
-%0Aconst ldaDocInfo = JSON.parse(fs.readFileSync(ldaDocInfoPath))%0Aconst ldaTopic = JSON.parse(fs.readFileSync(ldaTopicPath))
 %0A%0Aco
@@ -546,28 +546,32 @@
  =%3E %7B%0A  res.
-json
+sendFile
 (ldaTopic)%0A%7D
@@ -567,16 +567,20 @@
 ldaTopic
+Path
 )%0A%7D)%0A%0Aap
@@ -625,20 +625,24 @@
 %7B%0A  res.
-json
+sendFile
 (ldaDocI
@@ -644,16 +644,20 @@
 aDocInfo
+Path
 )%0A%7D)%0A%0Aco
@@ -868,16 +868,16 @@
 .body%5D)%0A
+
   proces
@@ -918,25 +918,8 @@
 ) %7B%0A
-    'use strict'%0A
     

use file