Django에서 형태소 해석과 마르코프 연쇄를 해본다(개선 여지대 개미)
소개
훨씬 오래 전 트위터 데이터를 얻고 마르코프 체인. 라는 것을 했는데, Django에 짜넣어 보려고 해 보았습니다. 결과를 먼저 말하면 문제는 큰 개미입니다. 현시점에서의 작업 도중 경과까지를 남겨 두려고 생각합니다.
과거의 기사는 이쪽으로부터
Twitter 검색으로 정보를 모아 형태소 해석하고 마르코프 연쇄로 문장을 생성해 트윗.
Python3에서는 .has_key를 사용할 수 없으므로,
if markov.has_key(w):
라는 처리는
if w in markov:
그리고 다시 쓰는 이미지입니다.
Python3에서 MeCab 사용 준비
$ brew install mecab
$ brew install mecab-ipadic
$ pip install mecab-python3
이런 식으로 설치해 보았습니다.
코드
예를 들어 DMM18 금 동영상 RSS를 받으러갑니다.
장고로 만드는 간단한 RSS 리더
상기와 거의 같은 일을 하고 있으므로 참고에 부디.
제목과 설명을 가져와 저장합니다.
views.pyimport feedparser
import MeCab
import random
import re
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
from django.http import HttpResponse
from django.shortcuts import (render, redirect,)
def index(request):
url = 'http://www.dmm.co.jp/digital/videoa/-/list/rss/=/sort=date/'
feeder = feedparser.parse(url)
for entry in feeder['entries']:
lists = entry['description'] + entry['title']
f = open('text.txt', 'w')
f.write(lists)
f.flush()
f.close()
f = open('text.txt', 'r')
mecab_read = f.read()
f.close()
tagger = MeCab.Tagger('-Owakati')
wordlist = tagger.parse(mecab_read)
wordlist = wordlist.rstrip(' \n').split(' ')
f = open('l.txt', 'w')
f.write(str(wordlist))
f.close()
markov = {}
w = ''
for x in wordlist:
if w:
if w in markov:
new_list = markov[w]
else:
new_list =[]
new_list.append(x)
markov[w] = new_list
w = x
choice_words = wordlist[0]
sentence = ''
count = 0
while count < 20:
choice_words = random.choice(wordlist)
sentence += choice_words
count += 1
sentence = sentence.split(' ', 1)[0]
p = re.compile('[!-/:-@[-`{-~]')
sus = p.sub('', sentence)
context = {
'wordlist': wordlist,
'sus': sus,
}
return render(request,'index.html',context)
index.html{% extends "base.html" %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-12">
<p class="1">{{ wordlist }}</p>
<p class="2">{{ sus }}</p>
</div>
</div>
</div>
{% endblock %}
결과
이런 빌어 먹을 결과로 끝났습니다.
디스크립션에는 금액이 들어가 버리고 있으므로 제거해 두지 않으면 안 되네요・・・.
여배우명도 컷 해 두지 않으면 의미 모르게 되는 것도 알았습니다.
정진합니다.
원래 하고 싶었던 것
AV타이틀을 마르코프 연쇄로 만들고 싶었던 것입니다.
Reference
이 문제에 관하여(Django에서 형태소 해석과 마르코프 연쇄를 해본다(개선 여지대 개미)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Gen6/items/853bb3e36a091c761acf
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
if markov.has_key(w):
if w in markov:
$ brew install mecab
$ brew install mecab-ipadic
$ pip install mecab-python3
이런 식으로 설치해 보았습니다.
코드
예를 들어 DMM18 금 동영상 RSS를 받으러갑니다.
장고로 만드는 간단한 RSS 리더
상기와 거의 같은 일을 하고 있으므로 참고에 부디.
제목과 설명을 가져와 저장합니다.
views.pyimport feedparser
import MeCab
import random
import re
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
from django.http import HttpResponse
from django.shortcuts import (render, redirect,)
def index(request):
url = 'http://www.dmm.co.jp/digital/videoa/-/list/rss/=/sort=date/'
feeder = feedparser.parse(url)
for entry in feeder['entries']:
lists = entry['description'] + entry['title']
f = open('text.txt', 'w')
f.write(lists)
f.flush()
f.close()
f = open('text.txt', 'r')
mecab_read = f.read()
f.close()
tagger = MeCab.Tagger('-Owakati')
wordlist = tagger.parse(mecab_read)
wordlist = wordlist.rstrip(' \n').split(' ')
f = open('l.txt', 'w')
f.write(str(wordlist))
f.close()
markov = {}
w = ''
for x in wordlist:
if w:
if w in markov:
new_list = markov[w]
else:
new_list =[]
new_list.append(x)
markov[w] = new_list
w = x
choice_words = wordlist[0]
sentence = ''
count = 0
while count < 20:
choice_words = random.choice(wordlist)
sentence += choice_words
count += 1
sentence = sentence.split(' ', 1)[0]
p = re.compile('[!-/:-@[-`{-~]')
sus = p.sub('', sentence)
context = {
'wordlist': wordlist,
'sus': sus,
}
return render(request,'index.html',context)
index.html{% extends "base.html" %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-12">
<p class="1">{{ wordlist }}</p>
<p class="2">{{ sus }}</p>
</div>
</div>
</div>
{% endblock %}
결과
이런 빌어 먹을 결과로 끝났습니다.
디스크립션에는 금액이 들어가 버리고 있으므로 제거해 두지 않으면 안 되네요・・・.
여배우명도 컷 해 두지 않으면 의미 모르게 되는 것도 알았습니다.
정진합니다.
원래 하고 싶었던 것
AV타이틀을 마르코프 연쇄로 만들고 싶었던 것입니다.
Reference
이 문제에 관하여(Django에서 형태소 해석과 마르코프 연쇄를 해본다(개선 여지대 개미)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Gen6/items/853bb3e36a091c761acf
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import feedparser
import MeCab
import random
import re
import sys, codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
from django.http import HttpResponse
from django.shortcuts import (render, redirect,)
def index(request):
url = 'http://www.dmm.co.jp/digital/videoa/-/list/rss/=/sort=date/'
feeder = feedparser.parse(url)
for entry in feeder['entries']:
lists = entry['description'] + entry['title']
f = open('text.txt', 'w')
f.write(lists)
f.flush()
f.close()
f = open('text.txt', 'r')
mecab_read = f.read()
f.close()
tagger = MeCab.Tagger('-Owakati')
wordlist = tagger.parse(mecab_read)
wordlist = wordlist.rstrip(' \n').split(' ')
f = open('l.txt', 'w')
f.write(str(wordlist))
f.close()
markov = {}
w = ''
for x in wordlist:
if w:
if w in markov:
new_list = markov[w]
else:
new_list =[]
new_list.append(x)
markov[w] = new_list
w = x
choice_words = wordlist[0]
sentence = ''
count = 0
while count < 20:
choice_words = random.choice(wordlist)
sentence += choice_words
count += 1
sentence = sentence.split(' ', 1)[0]
p = re.compile('[!-/:-@[-`{-~]')
sus = p.sub('', sentence)
context = {
'wordlist': wordlist,
'sus': sus,
}
return render(request,'index.html',context)
{% extends "base.html" %}
{% block body %}
<div class="container">
<div class="row">
<div class="col-md-12">
<p class="1">{{ wordlist }}</p>
<p class="2">{{ sus }}</p>
</div>
</div>
</div>
{% endblock %}
이런 빌어 먹을 결과로 끝났습니다.
디스크립션에는 금액이 들어가 버리고 있으므로 제거해 두지 않으면 안 되네요・・・.
여배우명도 컷 해 두지 않으면 의미 모르게 되는 것도 알았습니다.
정진합니다.
원래 하고 싶었던 것
AV타이틀을 마르코프 연쇄로 만들고 싶었던 것입니다.
Reference
이 문제에 관하여(Django에서 형태소 해석과 마르코프 연쇄를 해본다(개선 여지대 개미)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Gen6/items/853bb3e36a091c761acf
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(Django에서 형태소 해석과 마르코프 연쇄를 해본다(개선 여지대 개미)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Gen6/items/853bb3e36a091c761acf텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)