[PYTHON] Output the result of morphological analysis with Mecab to a WEB browser compatible with Sakura server / UTF-8

Overview

For a WEB browser that supports UTF-8 on the Sakura server Output the result of morphological analysis with Mecab.

The programming language will be explained using python2.7. I hope it will be helpful when you output the output result of Mecab on the WEB.

Installing Mecab itself and calling Mecab from Python See the previous article.

Install mecab on Sakura shared server and call it from python http://qiita.com/Jshirius/items/3a067486e2a693544c32

UTF-8 is used as the character code. The character code of Mecab is "euc-jp" by default, and I was addicted to outputting the result in UTF-8. Therefore, leave a note.

Prerequisites

Prerequisites In this article, the following directories are the Roots of the Web. /home/orehome/www/test

Where to put the program /home/orehome/www/test/cgi-bin

File name of the source file mecab_sample.py

Contents

(1) Write the source code Write the following source code.

mecab_sample.py



#!/usr/bin/env python
# coding: UTF-8

import MeCab

#Specify the location of the dictionary file with the full path (reading failed for some reason if it is a relative path)
userdic_path="-d /home/orehome/local/lib/mecab/dic/ipadic"
t = MeCab.Tagger("-Ochasen " + userdic_path)

text = u'Of the thighs and thighs'
#Convert to utf8
encoded_text = text.encode('utf-8')
meData = t.parse(encoded_text )

#Line feed code<br>Conversion to
meData = meData.replace("\n","<br>")


html_body="""
<html><body>
Input character example:<br>%s
<br><br>
Morphological analysis results<br>
%s
</body></html>"""



print "Content-type: text/html\n"
#Garbled characters in Chrome without the following meta tags
print "<meta charset=utf-8 />"
print html_body % (encoded_text,meData)

Place the created source code anywhere on the server. However, since the source code runs on cgi, please keep it under the cgi-bin directory.

In this example, the files are placed in the following locations: /home/orehome/www/test/cgi-bin/mecab_sample.py

(2) Change the access authority to enable Make access rights executable. If you forget this and access the WEB page, the browser will It turned white and took an hour to resolve ... chmod 755 mecab_sample.py

(3) Start the python web server Move to the next higher level of "cgi-bin" and enter the following command to run the server. For this article, go to / home / orehome / www / test and enter the following command:

python -m CGIHTTPServer

Here's how to keep the server running even after logging out nohup python -m CGIHTTPServer &

~~ * I don't know if the method of using nohup is correct when operating in a production environment. If anyone knows, please let me know! ~~

Added on April 18, 2017 When operating in a production environment, the following materials will be helpful! Try running Sakura and simple PYTHON CGI (Preferences) http://www.mwsoft.jp/programming/python/sakura/010_010.html

(4) WEB browser operation check My environment uses python2.7. Hit the URL from your browser.

http://domain_name:8000/cgi-bin/mecab_sample.py

If it is working properly, it will be displayed as follows.

スクリーンショット 2017-04-16 14.10.48.png

Recommended Posts

Output the result of morphological analysis with Mecab to a WEB browser compatible with Sakura server / UTF-8
Create a bot that only returns the result of morphological analysis with MeCab on Discord
I made a class to get the analysis result by MeCab in ndarray with python
How to output the output result of the Linux man command to a file
From the introduction of JUMAN ++ to morphological analysis of Japanese with Python
To output a value even in the middle of a cell with Jupyter Notebook
SSH login to the target server from Windows with a click of a shortcut
A memorandum about the warning of the pylint output result
Set the output destination of the execution result to Vim started as a modeless window
[Morphological analysis] How to add a new dictionary to Mecab
Output the output result of sklearn.metrics.classification_report as a CSV file
I tried to rewrite the WEB server of the normal Linux programming 1st edition with C ++ 14
How to display the regional mesh of the official statistics window (eStat) in a web browser
A Python script that allows you to check the status of the server from your browser
Build a speed of light web API server with Falcon
[Introduction to AWS] A memorandum of building a web server on AWS
[Python] Morphological analysis with MeCab
Fixed-point observation of specific data on the Web by automatically executing a Web browser on the server (Ubuntu16.04) (2) -Web scraping-
Save the result of the life game as a gif with python
[python, ruby] fetch the contents of a web page with selenium-webdriver
I made a web server with Raspberry Pi to watch anime
Feature extraction by TF method using the result of morphological analysis
[Introduction to StyleGAN] I played with "The Life of a Man" ♬
I want to output the beginning of the next month with Python
Output the contents of ~ .xlsx in the folder to HTML with Python
I played with Mecab (morphological analysis)!
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
[Introduction to Python] How to sort the contents of a list efficiently with list sort
[Image recognition] How to read the result of automatic annotation with VoTT
I wrote a corpus reader that reads the results of MeCab analysis
The story of developing a web application that automatically generates catchphrases [MeCab]
Hit a method of a class instance with the Python Bottle Web API
When you access the web server, L Chika (HTTPS compatible) with Arduino
Write the result of keyword search with ebaysdk to Google Spread Sheets
I tried to verify the result of A / B test by chi-square test
Summary of how to build a LAMP + Wordpress environment with Sakura VPS
Fixed-point observation of specific data on the Web by automatically executing a Web browser on the server (Ubuntu16.04) (3) ~ Cron automatic execution ~
The story of adding MeCab to ubuntu 16.04
Web server for browser testing with Mocha
How to pass the execution result of a shell command in a list in Python
[Python] From morphological analysis of CSV data to CSV output and graph display [GiNZA]
Feel free to write a test with nose (in the case of + gevent)
Try to solve the traveling salesman problem with a genetic algorithm (execution result)
[NNabla] How to get the output (variable) of the middle layer of a pre-built network
I failed to install django with pip, so a reminder of the solution
The story of making a web application that records extensive reading with Django
How to get a list of files in the same directory with python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
I tried to create a model with the sample of Amazon SageMaker Autopilot
[Introduction to Python] How to get the index of data with a for statement
The first artificial intelligence. I wanted to try natural language processing, so I will try morphological analysis using MeCab with python3.