From 6487541cd964c8e9fd4acb4756f040aa482f7d9a Mon Sep 17 00:00:00 2001 From: Hironsan Date: Mon, 14 Oct 2019 22:37:45 +0900 Subject: [PATCH] Update doc API --- frontend/api/db/docs.json | 379 ++++++++++++++++++------------------ frontend/api/db/labels.json | 18 +- frontend/api/routes/docs.js | 45 ++--- frontend/store/documents.js | 2 +- 4 files changed, 225 insertions(+), 219 deletions(-) diff --git a/frontend/api/db/docs.json b/frontend/api/db/docs.json index 6c1bef1a..e04f31ba 100644 --- a/frontend/api/db/docs.json +++ b/frontend/api/db/docs.json @@ -1,187 +1,192 @@ -[ - { - "id": 1, - "text": "CRICKET - LEICESTERSHIRE TAKE OVER AT TOP AFTER INNINGS VICTORY .", - "annotations": [ - { - "id": 5943, - "prob": 0.0, - "label": 1, - "start_offset": 10, - "end_offset": 24, - "user": 1, - "document": 1, - "text": "This text is for seq2seq testing" - } - ], - "meta": "{}", - "annotation_approver": null - }, - { - "id": 2, - "text": "LONDON 1996-08-30", - "annotations": [ - { - "id": 2, - "prob": 0.0, - "label": 1, - "start_offset": 0, - "end_offset": 6, - "user": 1, - "document": 2 - } - ], - "meta": "{}", - "annotation_approver": null - }, - { - "id": 3, - "text": "West Indian all-rounder Phil Simmons took four for 38 on Friday as Leicestershire beat Somerset by an innings and 39 runs in two days to take over at the head of the county championship .", - "annotations": [ - { - "id": 3, - "prob": 0.0, - "label": 2, - "start_offset": 0, - "end_offset": 11, - "user": 1, - "document": 3 - }, - { - "id": 5, - "prob": 0.0, - "label": 3, - "start_offset": 67, - "end_offset": 81, - "user": 1, - "document": 3 - }, - { - "id": 6, - "prob": 0.0, - "label": 3, - "start_offset": 87, - "end_offset": 95, - "user": 1, - "document": 3 - }, - { - "id": 4, - "prob": 0.0, - "label": 4, - "start_offset": 24, - "end_offset": 36, - "user": 1, - "document": 3 - } - ], - "meta": "{}", - "annotation_approver": null - }, - { - "id": 4, - "text": "Their stay on top , though , may be short-lived as title rivals Essex , Derbyshire and Surrey all closed in on victory while Kent made up for lost time in their rain-affected match against Nottinghamshire .", - "annotations": [ - { - "id": 7, - "prob": 0.0, - "label": 3, - "start_offset": 64, - "end_offset": 69, - "user": 1, - "document": 4 - }, - { - "id": 8, - "prob": 0.0, - "label": 3, - "start_offset": 72, - "end_offset": 82, - "user": 1, - "document": 4 - }, - { - "id": 9, - "prob": 0.0, - "label": 3, - "start_offset": 87, - "end_offset": 93, - "user": 1, - "document": 4 - }, - { - "id": 10, - "prob": 0.0, - "label": 3, - "start_offset": 125, - "end_offset": 129, - "user": 1, - "document": 4 - }, - { - "id": 11, - "prob": 0.0, - "label": 3, - "start_offset": 189, - "end_offset": 204, - "user": 1, - "document": 4 - } - ], - "meta": "{}", - "annotation_approver": null - }, - { - "id": 5, - "text": "After bowling Somerset out for 83 on the opening morning at Grace Road , Leicestershire extended their first innings by 94 runs before being bowled out for 296 with England discard Andy Caddick taking three for 83 .", - "annotations": [ - { - "id": 13, - "prob": 0.0, - "label": 1, - "start_offset": 60, - "end_offset": 70, - "user": 1, - "document": 5 - }, - { - "id": 15, - "prob": 0.0, - "label": 1, - "start_offset": 165, - "end_offset": 172, - "user": 1, - "document": 5 - }, - { - "id": 12, - "prob": 0.0, - "label": 3, - "start_offset": 14, - "end_offset": 22, - "user": 1, - "document": 5 - }, - { - "id": 14, - "prob": 0.0, - "label": 3, - "start_offset": 73, - "end_offset": 87, - "user": 1, - "document": 5 - }, - { - "id": 16, - "prob": 0.0, - "label": 4, - "start_offset": 181, - "end_offset": 193, - "user": 1, - "document": 5 - } - ], - "meta": "{}", - "annotation_approver": null - } -] \ No newline at end of file +{ + "count": 3250, + "next": "http://127.0.0.1:8000/v1/projects/2/docs?limit=5&offset=5", + "previous": null, + "results": [ + { + "id": 4, + "text": "CRICKET - LEICESTERSHIRE TAKE OVER AT TOP AFTER INNINGS VICTORY .", + "annotations": [ + { + "id": 5, + "prob": 0.0, + "label": 6, + "start_offset": 10, + "end_offset": 24, + "user": 1, + "document": 4, + "text": "This text is for seq2seq testing." + } + ], + "meta": "{}", + "annotation_approver": null + }, + { + "id": 5, + "text": "LONDON 1996-08-30", + "annotations": [ + { + "id": 6, + "prob": 0.0, + "label": 4, + "start_offset": 0, + "end_offset": 6, + "user": 1, + "document": 5 + } + ], + "meta": "{}", + "annotation_approver": null + }, + { + "id": 6, + "text": "West Indian all-rounder Phil Simmons took four for 38 on Friday as Leicestershire beat Somerset by an innings and 39 runs in two days to take over at the head of the county championship .", + "annotations": [ + { + "id": 7, + "prob": 0.0, + "label": 5, + "start_offset": 0, + "end_offset": 11, + "user": 1, + "document": 6 + }, + { + "id": 9, + "prob": 0.0, + "label": 6, + "start_offset": 67, + "end_offset": 81, + "user": 1, + "document": 6 + }, + { + "id": 10, + "prob": 0.0, + "label": 6, + "start_offset": 87, + "end_offset": 95, + "user": 1, + "document": 6 + }, + { + "id": 8, + "prob": 0.0, + "label": 7, + "start_offset": 24, + "end_offset": 36, + "user": 1, + "document": 6 + } + ], + "meta": "{}", + "annotation_approver": null + }, + { + "id": 7, + "text": "Their stay on top , though , may be short-lived as title rivals Essex , Derbyshire and Surrey all closed in on victory while Kent made up for lost time in their rain-affected match against Nottinghamshire .", + "annotations": [ + { + "id": 11, + "prob": 0.0, + "label": 6, + "start_offset": 64, + "end_offset": 69, + "user": 1, + "document": 7 + }, + { + "id": 12, + "prob": 0.0, + "label": 6, + "start_offset": 72, + "end_offset": 82, + "user": 1, + "document": 7 + }, + { + "id": 13, + "prob": 0.0, + "label": 6, + "start_offset": 87, + "end_offset": 93, + "user": 1, + "document": 7 + }, + { + "id": 14, + "prob": 0.0, + "label": 6, + "start_offset": 125, + "end_offset": 129, + "user": 1, + "document": 7 + }, + { + "id": 15, + "prob": 0.0, + "label": 6, + "start_offset": 189, + "end_offset": 204, + "user": 1, + "document": 7 + } + ], + "meta": "{}", + "annotation_approver": null + }, + { + "id": 8, + "text": "After bowling Somerset out for 83 on the opening morning at Grace Road , Leicestershire extended their first innings by 94 runs before being bowled out for 296 with England discard Andy Caddick taking three for 83 .", + "annotations": [ + { + "id": 17, + "prob": 0.0, + "label": 4, + "start_offset": 60, + "end_offset": 70, + "user": 1, + "document": 8 + }, + { + "id": 19, + "prob": 0.0, + "label": 4, + "start_offset": 165, + "end_offset": 172, + "user": 1, + "document": 8 + }, + { + "id": 16, + "prob": 0.0, + "label": 6, + "start_offset": 14, + "end_offset": 22, + "user": 1, + "document": 8 + }, + { + "id": 18, + "prob": 0.0, + "label": 6, + "start_offset": 73, + "end_offset": 87, + "user": 1, + "document": 8 + }, + { + "id": 20, + "prob": 0.0, + "label": 7, + "start_offset": 181, + "end_offset": 193, + "user": 1, + "document": 8 + } + ], + "meta": "{}", + "annotation_approver": null + } + ] +} \ No newline at end of file diff --git a/frontend/api/db/labels.json b/frontend/api/db/labels.json index f45494c4..ef9bcc20 100644 --- a/frontend/api/db/labels.json +++ b/frontend/api/db/labels.json @@ -1,34 +1,34 @@ [ { - "id": 1, + "id": 4, "text": "LOC", "prefix_key": null, "suffix_key": "l", - "background_color": "#fdabe3", - "text_color": "#000000" + "background_color": "#7c20e0", + "text_color": "#ffffff" }, { - "id": 2, + "id": 5, "text": "MISC", "prefix_key": null, "suffix_key": "m", - "background_color": "#c89b7a", + "background_color": "#fbb028", "text_color": "#000000" }, { - "id": 3, + "id": 6, "text": "ORG", "prefix_key": null, "suffix_key": "o", - "background_color": "#ed8df6", + "background_color": "#e6d176", "text_color": "#000000" }, { - "id": 4, + "id": 7, "text": "PER", "prefix_key": null, "suffix_key": "p", - "background_color": "#651f62", + "background_color": "#6a74b9", "text_color": "#ffffff" } ] \ No newline at end of file diff --git a/frontend/api/routes/docs.js b/frontend/api/routes/docs.js index 898ab560..f3090a70 100644 --- a/frontend/api/routes/docs.js +++ b/frontend/api/routes/docs.js @@ -1,13 +1,14 @@ const fs = require('fs') const express = require('express') const router = express.Router() -let db = JSON.parse(fs.readFileSync('./api/db/docs.json', 'utf8')) +const db = JSON.parse(fs.readFileSync('./api/db/docs.json', 'utf8')) // Get doc list. router.get('/', (req, res) => { const q = req.query.q if (q) { - res.json(db.filter(item => item.text.toLowerCase().includes(q.toLowerCase()))) + // res.json(db.filter(item => item.text.toLowerCase().includes(q.toLowerCase()))) + res.json(db) } else { res.json(db) } @@ -16,7 +17,7 @@ router.get('/', (req, res) => { // Create a doc. router.post('/', (req, res) => { const doc = { - id: db.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1, + id: db.results.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1, text: req.body.text } res.json(doc) @@ -25,12 +26,12 @@ router.post('/', (req, res) => { // Upload a file. router.post('/upload', (req, res) => { const doc = { - id: db.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1, + id: db.results.reduce((x, y) => { return x.id > y.id ? x : y }).id + 1, text: 'Uploaded Document', meta: JSON.stringify({}), annotations: [] } - db.push(doc) + db.results.push(doc) res.json(doc) }) @@ -41,10 +42,10 @@ router.get('/download', (req, res) => { // Update a document partially. router.patch('/:docId', (req, res) => { - const docIndex = db.findIndex(item => item.id === parseInt(req.params.docId)) + const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId)) if (docIndex !== -1) { - Object.assign(db[docIndex], req.body) - res.json(db[docIndex]) + Object.assign(db.results[docIndex], req.body) + res.json(db.results[docIndex]) } else { res.status(404).json({ detail: 'Not found.' }) } @@ -52,7 +53,7 @@ router.patch('/:docId', (req, res) => { // Get a doc. router.get('/:docId', (req, res) => { - const doc = db.find(item => item.id === parseInt(req.params.docId)) + const doc = db.results.find(item => item.id === parseInt(req.params.docId)) if (doc) { res.json(doc) } else { @@ -62,10 +63,10 @@ router.get('/:docId', (req, res) => { // Update a doc. router.put('/:docId', (req, res) => { - const docIndex = db.findIndex(item => item.id === parseInt(req.params.docId)) + const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId)) if (docIndex !== -1) { - db[docIndex] = req.body - res.json(db[docIndex]) + db.results[docIndex] = req.body + res.json(db.results[docIndex]) } else { res.status(404).json({ detail: 'Not found.' }) } @@ -73,9 +74,9 @@ router.put('/:docId', (req, res) => { // Delete a doc. router.delete('/:docId', (req, res, next) => { - const doc = db.find(item => item.id === parseInt(req.params.docId)) + const doc = db.results.find(item => item.id === parseInt(req.params.docId)) if (doc) { - db = db.filter(item => item.id !== parseInt(req.params.docId)) + db.results = db.results.filter(item => item.id !== parseInt(req.params.docId)) res.json(doc) } else { res.status(404).json({ detail: 'Not found.' }) @@ -84,7 +85,7 @@ router.delete('/:docId', (req, res, next) => { // Add an annotation. router.post('/:docId/annotations', (req, res, next) => { - const doc = db.find(item => item.id === parseInt(req.params.docId)) + const doc = db.results.find(item => item.id === parseInt(req.params.docId)) if (doc) { const annotation = { id: Math.floor(Math.random() * 10000), @@ -104,12 +105,12 @@ router.post('/:docId/annotations', (req, res, next) => { // Delete an annotation. router.delete('/:docId/annotations/:annotationId', (req, res, next) => { - const doc = db.find(item => item.id === parseInt(req.params.docId)) - const docIndex = db.findIndex(item => item.id === parseInt(req.params.docId)) + const doc = db.results.find(item => item.id === parseInt(req.params.docId)) + const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId)) if (doc) { const annotation = doc.annotations.find(item => item.id === parseInt(req.params.annotationId)) doc.annotations = doc.annotations.filter(item => item.id !== parseInt(req.params.annotationId)) - db[docIndex] = doc + db.results[docIndex] = doc res.json(annotation) } else { res.status(404).json({ detail: 'Not found.' }) @@ -118,12 +119,12 @@ router.delete('/:docId/annotations/:annotationId', (req, res, next) => { // Update an annotation. router.patch('/:docId/annotations/:annotationId', (req, res, next) => { - const docIndex = db.findIndex(item => item.id === parseInt(req.params.docId)) + const docIndex = db.results.findIndex(item => item.id === parseInt(req.params.docId)) if (docIndex !== -1) { - const doc = db[docIndex] + const doc = db.results[docIndex] const annotationIndex = doc.annotations.findIndex(item => item.id === parseInt(req.params.annotationId)) - Object.assign(db[docIndex].annotations[annotationIndex], req.body) - res.json(db[docIndex].annotations[annotationIndex]) + Object.assign(db.results[docIndex].annotations[annotationIndex], req.body) + res.json(db.results[docIndex].annotations[annotationIndex]) } else { res.status(404).json({ detail: 'Not found.' }) } diff --git a/frontend/store/documents.js b/frontend/store/documents.js index f42b8312..c1a9525f 100644 --- a/frontend/store/documents.js +++ b/frontend/store/documents.js @@ -104,7 +104,7 @@ export const actions = { commit('setLoading', true) return DocumentService.getDocumentList() .then((response) => { - commit('setDocumentList', response) + commit('setDocumentList', response.results) }) .catch((error) => { alert(error)