[CENTOS] Create a simple textlint server

textlint/textlint: The pluggable natural language linter for text and markdown.

I will try to make inquiries with api.

file organization

Note that an error will occur if the app folder is the same as the package name to be installed later, such as textlint.

app/
  ├ .textlintrc //Setting
  ├ app.js      //web main
  └ a.py        //client

Package installation

It is appropriate that --save is good

$ npm init --yes
$ npm install --save-dev express
$ npm install --save-dev textlint
//Rules to check TODO
$ npm install --save-dev textlint-rule-no-todo
//Rules for checking consecutive kanji
$ npm install --save-dev textlint-rule-max-kanji-continuous-len

The current version is as follows.

$ node --version
v10.21.0
$ npm --version
6.14.4
$ rpm list
[email protected]
[email protected]
[email protected]
[email protected]

File

app.js


const TextLintEngine = require('textlint').TextLintEngine;
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

//Magic to parse json of post data
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());

//Wait at port 8080
app.listen(8080, () => {
    console.log('Running at Port 8080...');
});

app.post('/api/textlint', (req, res, next) => {
    const req_text = req.body.text;
    const engine = new TextLintEngine();
    engine.executeOnText(req_text).then(results => {
        res.json({
            messages: results[0].messages
        });
    });
});

//404 error for other requests
app.use((req, res) => {
    res.sendStatus(404);
});

See github for each rule for how to write

.textlintrc


{
  "filters": {},
  "rules": {
    "no-todo": true,
    "max-kanji-continuous-len": true,
  }
}

Apply lint to the string of the text variable

a.py


import json
import urllib.request

url = 'http://localhost:8080/api/textlint'
text = '''
TODO: this is TODO
1 2 3 4 5 6
'''.strip()

data = {"text": text}
headers = {
    'Content-Type': 'application/json',
}
req = urllib.request.Request(url, json.dumps(data).encode(), headers)
try:
    with urllib.request.urlopen(req) as res:
        body = json.load(res)
        json_str = json.dumps(body)
        print(json_str)
except:
    print("fail")

Start the server

$ node app.js
Running at Port 8080...

Try to request

$ python3 a.py | jq .
{
  "messages": [
    {
      "type": "lint",
      "ruleId": "no-todo",
      "message": "Found TODO: 'TODO: this is TODO'",
      "index": 0,
      "line": 1,
      "column": 1,
      "severity": 2
    },
    {
      "type": "lint",
      "ruleId": "max-kanji-continuous-len",
      "message": "6 or more kanji in a row:1 2 3 4 5 6",
      "index": 19,
      "line": 2,
      "column": 1,
      "severity": 2
    }
  ]
}

Of course you can also call it from the command line

$ curl -X POST -H "Content-Type: application/json" \
  -d '{"text":"TODO: this is TODO\n 1 2 3 4 5 6"}'  \
  localhost:8080/api/textlint | jq .
{
  "messages": [
    {
      "type": "lint",
      "ruleId": "no-todo",
      "message": "Found TODO: 'TODO: this is TODO'",
      "index": 0,
      "line": 1,
      "column": 1,
      "severity": 2
    },
    {
      "type": "lint",
      "ruleId": "max-kanji-continuous-len",
      "message": "6 or more kanji in a row:1 2 3 4 5 6",
      "index": 19,
      "line": 2,
      "column": 1,
      "severity": 2
    }
  ]
}

Reference information

Building a local server with Node \ .js and Express \ (1 ) ―Introduction of Node \ .js and npm― -Qiita Run textlint from code -Qiita Run textlint in nodejs / express environment [node \ .js -Textlint execution result is empty -Stack overflow](https://ja.stackoverflow.com/questions/60015/textlint-%E3%81%AE%E5%AE%9F % E8% A1% 8C% E7% B5% 90% E6% 9E% 9C% E3% 81% 8C% E7% A9% BA% E3% 81% AB% E3% 81% AA% E3% 82% 8B / 70543 ) Inquire to WebAPI after getting POST data with Node \ .js \ + Express -Qiita Understanding body \ -parser of node \ .js \ (express ) -Qiita

Recommended Posts

Create a simple textlint server
How to create a simple TCP server / client script
Create a Unix Domain Socket server
Write a super simple TCP server
Create a simple GUI app in Python
Build a simple WebDAV server on Linux
Create a simple web app with flask
Set up a simple HTTPS server in Python 3
Set up a simple HTTPS server with asyncio
Start a simple Python web server with Docker
Create a simple momentum investment model in Python
Set up a simple SMTP server in Python
Create a "Hello World" (HTTP) server with Tornado
Create a web server in Go language (net/http) (2)
Create a Python module
Create a Bootable LV
Create a Python environment
Create a slack bot
Set up a simple local server on your Mac
A simple Python HTTP server that supports Range Requests
Create a pseudo REST API server using GitHub Pages
Create a home music server with Centos8 + Universal Media Server
Create a web server in Go language (net / http) (1)
Create a fake Minecraft server in Python with Quarry
[Vagrant] Set up a simple API server with python
Create a Wox plugin (Python)
Create a local pypi repository
Create a function in Python
Create a dictionary in Python
A simple sample of pivot_table.
Django beginners create simple apps 3
Django beginners create simple apps 1
Launch a simple WEB server that can check the header
Create a homepage with django
How to specify a public directory Python simple HTTP server
[Python Kivy] How to create a simple pop up window
Create a python numpy array
Create a dummy data file
Django beginners create simple apps 2
Create a simple video analysis tool with python wxpython + openCV
Create a Django login screen
Create a simple Python development environment with VSCode & Docker Desktop
Create a heatmap with pyqtgraph
Create a classroom on Jupyterhub
Simple HTTP Server for python
Create a directory with python
Create a simple CRUD app using Django's generic class view
Django beginners create simple apps 5
Create a rudimentary ELF packer
I tried to create a server environment that runs on Windows 10
[C language] [Linux] Try to create a simple Linux command * Just add! !!
Overview of how to create a server socket and how to establish a client socket
I tried to create a simple credit score by logistic regression.
Rails users try to create a simple blog engine with Django
Create a simple scheduled batch using Docker's Python Image and parse-crontab
Create a simple Python development environment with VS Code and Docker
Learn Zundokokiyoshi using a simple RNN
Create a python GUI using tkinter
Create a DI Container in Python
Implementation of a simple particle filter
Creating a Flask server with Docker