-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq_topic.py
136 lines (117 loc) · 4.94 KB
/
q_topic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
# coding:utf-8
from urllib.request import urlopen
from urllib.parse import urlparse
import urllib
import re
import requests
import sys
import lxml.html
from numpy import *
#from AccesDB import AccessToDataBase
class titleName:
def __init__(self, state):
self.reply_y=str(["うん","ある","はい"])
self.reply_n=str(["ない","いいえ","ううん"])
self.state_counter = state
#self.db = AccessToDataBase()
def getUtterance(self, i):
c = self.state_counter
if c == 100:
if "君の名は" in i:
return ("いいですよ。", "君の名は。")
else:
return (random.choice(["なんでしょうか.", "すみません、聞き取れませんでした", "えっ?"]), None)
if c == 10:
self.state_counter = 11
q_first=["君の名は。の話をしない?","ねえねえ、君の名は。知ってる?"]
rand=random.choice(q_first)
return (rand, None)
if c == 11:
return ("よかった。すごく面白いよね!どこが好き?", "君の名は。")
if c == 0:
self.state_counter = 1
q_first=["何か好きなアニメとかある?","何かオススメのアニメとかある?"]
rand=random.choice(q_first)
return (rand, None)
elif c == 1:
if i in self.reply_y:
self.state_counter = 2
q_first=["タイトルはなんて言うやつ?","なんて名前なの?","何何?気になる!なんてやつ?"]
rand=random.choice(q_first)
return (rand, None)
elif i in self.reply_n:
self.state_counter = 3
q_first=["じゃあ何か好きなキャラクターを教えてくれる?","何か知ってるキャラクターの名前とか教えてよ"]
rand=random.choice(q_first)
return (rand, None)
else:
result = self.find_article(i)
return ("じゃあ"+result+"の話をしよう!"+result+"の好きなところとか聞きたいな!", result)
elif c == 3:
#キャラクター名から作品名を聞く
self.state_counter = 4
q_first=["ああ、それ何のキャラクターだっけ?"]
rand=random.choice(q_first)
return (rand, None)
elif c == 4:
result = self.find_article(i)
if result is None:
self.state_counter = 4
return ("うーんごめん、よくわからないや。何かヒントとかない?", None)
else:
return ("じゃあ"+result+"の話をしよう!"+result+"の好きなところとか聞きたいな!", result)
def find_article(self, i):
#https:
#//qiita.com/yagays/items/e59731b3930252b5f0c4
par_i=urllib.parse.quote(i)
print(par_i)
try:
u = urlopen('https://dic.pixiv.net/a/'+par_i)
t=u.read()
print('https://dic.pixiv.net/a/'+par_i)
a_title=i
except urllib.error.HTTPError as err:
#inputされた値の名前の記事がないときはその名前で検索をかける
print(err.code)
u = urlopen('https://dic.pixiv.net/search?query='+par_i)
t=u.read()
st=str(t)
a_point=st.find('div class="thumb"')
print("target point==",a_point)
if a_point==-1:
#検索結果がなかったら別のキーワードを問う
self.state_counter = 4
return "うーんごめん、よくわからないや。何かヒントとかない?"
result = self.find_article(i)
a_title=result
return a_title
else:
st_len=len(st)
print("text length==",st_len)
par_text=st[a_point-1:st_len]
char_n=st_len
while char_n>a_point:
char_n=char_n-1
a_position='<a href=".*?">'
a_url=re.search(a_position,par_text)
a_st=a_url.start()
a_end=a_url.end()
print("url position==",a_st,a_end)
a_tag=par_text[a_st+12:a_end-2]
break
art_u = urlopen('https://dic.pixiv.net/a/'+a_tag)
a_read=str(art_u.read())
print('https://dic.pixiv.net/a/'+a_tag)
a_title=urllib.parse.unquote(a_tag)
print(a_title + "が好きなんだ")
return a_title
if __name__ == "__main__":
dialog = titleName()
while True:
i = input(">> ")
respond, title = dialog.getUtterance(i)
print(respond)
if title is not None:
print("おわり")
break