|
| 1 | +import 'bootstrap/dist/css/bootstrap.min.css'; |
| 2 | +import React, {useEffect, useState} from "react"; |
| 3 | +import './App.css'; |
| 4 | +import {Button, Col, Form, Row} from "react-bootstrap"; |
| 5 | +import ReactJson from 'react-json-view'; |
| 6 | + |
| 7 | +function NERContext() { |
| 8 | + |
| 9 | + const [running, setRunning] = useState({ |
| 10 | + "arabic": { "start": null, "end": null, "isRunning": false, isLoaded: false }, |
| 11 | + "english-kbp": { "start": null, "end": null, "isRunning": false, isLoaded: false }, |
| 12 | + "english": { "start": null, "end": null, "isRunning": false, isLoaded: false }, |
| 13 | + "chinese": { "start": null, "end": null, "isRunning": false, isLoaded: false }, |
| 14 | + "french": { "start": null, "end": null, "isRunning": false, isLoaded: false }, |
| 15 | + "german": { "start": null, "end": null, "isRunning": false, isLoaded: false }, |
| 16 | + "spanish": { "start": null, "end": null, "isRunning": false, isLoaded: false } |
| 17 | + }) |
| 18 | + |
| 19 | + const [language, setLanguage] = useState("en"); |
| 20 | + const [content, setContent] = useState(""); |
| 21 | + const [namedEntities, setNamedEntities] = useState(""); |
| 22 | + const [nerError, setNerError] = useState({ code: null, description: null, value: null, properties: {}}); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + let uri = '/exist/restxq/stanford/nlp/logs'; |
| 26 | + |
| 27 | + fetch(uri) |
| 28 | + .then((response) => response.json()) |
| 29 | + .then( |
| 30 | + (result) => { |
| 31 | + setRunning(result.running); |
| 32 | + }, |
| 33 | + (error) => { |
| 34 | + |
| 35 | + } |
| 36 | + ) |
| 37 | + |
| 38 | + }, []) |
| 39 | + |
| 40 | + // @ts-ignore |
| 41 | + function handleChange(e) { |
| 42 | + if (e.target.name === 'language') { |
| 43 | + setLanguage(e.target.value); |
| 44 | + } else { |
| 45 | + setContent(e.target.value); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + function handleSubmit() { |
| 50 | + const requestOptions = { |
| 51 | + method: 'POST', |
| 52 | + headers: { 'Content-Type': 'application/json' }, |
| 53 | + body: JSON.stringify({ |
| 54 | + language: language, |
| 55 | + text: content |
| 56 | + }) |
| 57 | + }; |
| 58 | + |
| 59 | + fetch("/exist/restxq/Stanford/ner", requestOptions) |
| 60 | + .then(res => res.json()) |
| 61 | + .then( |
| 62 | + (result) => { |
| 63 | + if (result.text) { |
| 64 | + setNamedEntities(result.text); |
| 65 | + } else { |
| 66 | + setNerError(result); |
| 67 | + } |
| 68 | + }, |
| 69 | + (error) => { |
| 70 | + |
| 71 | + } |
| 72 | + ) |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <div style={{padding: 35}}> |
| 78 | + <Form> |
| 79 | + <Row className={'mb-3'}> |
| 80 | + <Col md={4}> |
| 81 | + <Form.Group> |
| 82 | + <Form.Label>Select language</Form.Label> |
| 83 | + <Form.Select name="language" onChange={handleChange}> |
| 84 | + <option value="en" disabled={!running.english.isLoaded}>English</option> |
| 85 | + <option value="ar" disabled={!running.arabic.isLoaded}>Arabic</option> |
| 86 | + <option value="zh" disabled={!running.chinese.isLoaded}>Chinese</option> |
| 87 | + <option value="fr" disabled={!running.french.isLoaded}>French</option> |
| 88 | + <option value="de" disabled={!running.german.isLoaded}>German</option> |
| 89 | + <option value="es" disabled={!running.spanish.isLoaded}>Spanish</option> |
| 90 | + </Form.Select> |
| 91 | + </Form.Group> |
| 92 | + </Col> |
| 93 | + </Row> |
| 94 | + <Form.Group as={Row} className={'mb-3'}> |
| 95 | + <Form.Label>Text to find named entities</Form.Label> |
| 96 | + <Form.Control as="textarea" rows={10} onChange={handleChange} /> |
| 97 | + </Form.Group> |
| 98 | + <Form.Group as={Row} className={'mb-3'}> |
| 99 | + <Col sm={2}> |
| 100 | + <Button type={'submit'} onClick={handleSubmit}>Submit</Button> |
| 101 | + </Col> |
| 102 | + </Form.Group> |
| 103 | + </Form> |
| 104 | + <Row> |
| 105 | + <div>Results</div> |
| 106 | + <hr/> |
| 107 | + <div dangerouslySetInnerHTML={{__html: namedEntities}}></div> |
| 108 | + <div>{ |
| 109 | + nerError.code ? |
| 110 | + <> |
| 111 | + <div><b>Code</b> <span>{nerError.code}</span></div> |
| 112 | + <div><b>Description</b> <span>{nerError.description}</span></div> |
| 113 | + <div><b>Value</b> <span>{nerError.value}</span></div> |
| 114 | + <ReactJson src={nerError.properties} /> |
| 115 | + </> |
| 116 | + : null |
| 117 | + }</div> |
| 118 | + <hr/> |
| 119 | + </Row> |
| 120 | + </div> |
| 121 | + ) |
| 122 | + |
| 123 | +} |
| 124 | + |
| 125 | +export default NERContext; |
0 commit comments