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 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
(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.
Recommended Posts