Instantly share code, notes, and snippets.

Embed
What would you like to do?
AIを作りたい2
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
$('#submit').on("click", function() {
$("#list").empty();
$("#list").append($("<ul></ul>").append('<li>解析丸:「' + $("#reply").val() + '」を解析中…</li>'));
$.ajax({
url: "http://127.0.0.1:5000/robot",
type:'POST',
dataType: 'json',
data : {"reply": $("#reply").val()},
timeout:10000,
}).done(function(data) {
$("#list").append(
$('<table border="1"></table>').append(
$("<thead></thead>").append(
$("<tr></tr>")
.append($("<th></th>").text(""))
.append($("<th></th>").text("表層形"))
.append($("<th></th>").text("品詞"))
.append($("<th></th>").text("品詞細分類1"))
.append($("<th></th>").text("品詞細分類2"))
.append($("<th></th>").text("品詞細分類3"))
.append($("<th></th>").text("活用型"))
.append($("<th></th>").text("活用形"))
.append($("<th></th>").text("原形"))
.append($("<th></th>").text("読み"))
.append($("<th></th>").text("発音"))
)
).append(
$('<tbody id="result"></tbody>')
)
)
$.each( data, function( key, value ) {
$("#result").append(
$("<tr></tr>")
.append($("<td></td>").text(key + 1))
.append($("<td></td>").text(value.surface))
.append($("<td></td>").text(value.partSpeech))
.append($("<td></td>").text(value.partSpeechSubdivision1))
.append($("<td></td>").text(value.partSpeechSubdivision2))
.append($("<td></td>").text(value.partSpeechSubdivision3))
.append($("<td></td>").text(value.inflType))
.append($("<td></td>").text(value.inflForm))
.append($("<td></td>").text(value.baseForm))
.append($("<td></td>").text(value.reading))
.append($("<td></td>").text(value.phonetic))
)
// $("#result").append('<tr>' + value.surface + '</tr>');
});
$('#submit').prop("disabled", false);
}).fail(function(XMLHttpRequest, textStatus, errorThrown) {
alert("error");
$('#submit').prop("disabled", false);
})
});
});
</script>
<table border="0">
<tr>
<td align="right"><b> 返答:</b></td>
<td><input type="text" name="reply" id="reply"></td>
</tr>
</table>
<input type="button" value="Submit" id="submit"/>
<div id="list">
<ul></ul>
</div>
<div id="detail"></div>
</body>
</html>
from flask import Flask, render_template, request
import json
from janome.tokenizer import Tokenizer
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html', title='解析丸')
@app.route('/robot', methods=['POST'])
def robot():
name = "解析丸"
tokens = Tokenizer().tokenize(request.form['reply'])
responsDatas = []
for token in tokens:
responsDatas.append({'surface': token.surface, #表層名
'partSpeech': token.part_of_speech.split(',')[0], # 品詞
'partSpeechSubdivision1': token.part_of_speech.split(',')[1], # 品詞細分類1
'partSpeechSubdivision2': token.part_of_speech.split(',')[2],# 品詞細分類2
'partSpeechSubdivision3': token.part_of_speech.split(',')[3],# 品詞細分類3
'inflType': token.infl_type, # 活用型
'inflForm': token.infl_form,# 活用形
'baseForm': token.base_form,# 原形
'reading': token.reading, # 読み
'phonetic': token.phonetic # 発音
})
return json.dumps(responsDatas, ensure_ascii=False)
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment