├── .ipynb_checkpoints
├── fastnlp研究学习-checkpoint.ipynb
├── 文本摘要Seq2Seq-checkpoint.ipynb
├── 文本摘要fastnlp-checkpoint.ipynb
├── 文本摘要kerasTransformer-checkpoint.ipynb
├── 机器翻译Seq2Seq-checkpoint.ipynb
├── 机器翻译fastnlp实现-checkpoint.ipynb
├── 机器翻译kerasTransformer-checkpoint.ipynb
├── 读取数据-checkpoint.ipynb
├── 读取数据第三版本-checkpoint.ipynb
├── 读取数据第二版本-checkpoint.ipynb
├── 问答机器人Seq2Seq-checkpoint.ipynb
├── 问答机器人kerasTransformer-checkpoint.ipynb
└── 问答系统fastnlp实现-checkpoint.ipynb
├── README.md
├── data
├── qa
│ ├── ai.txt
│ ├── qa.csv
│ └── word.txt
├── textsummary
│ ├── data.csv
│ └── readme.txt
└── translation
│ ├── ell.txt
│ ├── input_characters.pkl
│ ├── target_characters.pkl
│ └── translation.csv
├── fastnlp研究学习.ipynb
├── img
├── qa
│ └── Seq2SeqForQA.png
└── translation
│ └── Seq2SeqForTranslation.png
├── model
└── seq2seq
│ ├── qa
│ ├── s2s.h5
│ └── training.log
│ └── translation
│ └── s2s.h5
├── 文本摘要Seq2Seq.ipynb
├── 文本摘要fastnlp.ipynb
├── 文本摘要kerasTransformer.ipynb
├── 机器翻译Seq2Seq.ipynb
├── 机器翻译fastnlp实现.ipynb
├── 机器翻译kerasTransformer.ipynb
├── 读取数据第三版本.ipynb
├── 问答机器人Seq2Seq.ipynb
├── 问答机器人kerasTransformer.ipynb
└── 问答系统fastnlp实现.ipynb
/.ipynb_checkpoints/文本摘要kerasTransformer-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 2,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import numpy as np\n",
12 | "import pickle\n",
13 | "import operator\n",
14 | "import pandas as pd\n",
15 | "# import jieba\n",
16 | "# from language.langconv import *\n",
17 | "import os"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 3,
23 | "metadata": {
24 | "collapsed": true
25 | },
26 | "outputs": [],
27 | "source": [
28 | "text_summary_data=pd.read_csv('./data/textsummary/data.csv')"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 4,
34 | "metadata": {},
35 | "outputs": [
36 | {
37 | "data": {
38 | "text/html": [
39 | "
\n",
40 | "\n",
53 | "
\n",
54 | " \n",
55 | " \n",
56 | " | \n",
57 | " Unnamed: 0 | \n",
58 | " text | \n",
59 | " summary | \n",
60 | "
\n",
61 | " \n",
62 | " \n",
63 | " \n",
64 | " 0 | \n",
65 | " 0 | \n",
66 | " Saurav Kant, an alumnus of upGrad and IIIT-B's... | \n",
67 | " upGrad learner switches to career in ML & Al w... | \n",
68 | "
\n",
69 | " \n",
70 | " 1 | \n",
71 | " 1 | \n",
72 | " Kunal Shah's credit card bill payment platform... | \n",
73 | " Delhi techie wins free food from Swiggy for on... | \n",
74 | "
\n",
75 | " \n",
76 | " 2 | \n",
77 | " 2 | \n",
78 | " New Zealand defeated India by 8 wickets in the... | \n",
79 | " New Zealand end Rohit Sharma-led India's 12-ma... | \n",
80 | "
\n",
81 | " \n",
82 | " 3 | \n",
83 | " 3 | \n",
84 | " With Aegon Life iTerm Insurance plan, customer... | \n",
85 | " Aegon life iTerm insurance plan helps customer... | \n",
86 | "
\n",
87 | " \n",
88 | " 4 | \n",
89 | " 4 | \n",
90 | " Speaking about the sexual harassment allegatio... | \n",
91 | " Have known Hirani for yrs, what if MeToo claim... | \n",
92 | "
\n",
93 | " \n",
94 | "
\n",
95 | "
"
96 | ],
97 | "text/plain": [
98 | " Unnamed: 0 text \\\n",
99 | "0 0 Saurav Kant, an alumnus of upGrad and IIIT-B's... \n",
100 | "1 1 Kunal Shah's credit card bill payment platform... \n",
101 | "2 2 New Zealand defeated India by 8 wickets in the... \n",
102 | "3 3 With Aegon Life iTerm Insurance plan, customer... \n",
103 | "4 4 Speaking about the sexual harassment allegatio... \n",
104 | "\n",
105 | " summary \n",
106 | "0 upGrad learner switches to career in ML & Al w... \n",
107 | "1 Delhi techie wins free food from Swiggy for on... \n",
108 | "2 New Zealand end Rohit Sharma-led India's 12-ma... \n",
109 | "3 Aegon life iTerm insurance plan helps customer... \n",
110 | "4 Have known Hirani for yrs, what if MeToo claim... "
111 | ]
112 | },
113 | "execution_count": 4,
114 | "metadata": {},
115 | "output_type": "execute_result"
116 | }
117 | ],
118 | "source": [
119 | "text_summary_data.head()"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 5,
125 | "metadata": {
126 | "collapsed": true
127 | },
128 | "outputs": [],
129 | "source": [
130 | "import re\n",
131 | "\n",
132 | "#Removes non-alphabetic characters:\n",
133 | "def text_strip(column):\n",
134 | " for row in column:\n",
135 | " \n",
136 | " #ORDER OF REGEX IS VERY VERY IMPORTANT!!!!!!\n",
137 | " \n",
138 | " row=re.sub(\"(\\\\t)\", ' ', str(row)).lower() #remove escape charecters\n",
139 | " row=re.sub(\"(\\\\r)\", ' ', str(row)).lower() \n",
140 | " row=re.sub(\"(\\\\n)\", ' ', str(row)).lower()\n",
141 | " \n",
142 | " row=re.sub(\"(__+)\", ' ', str(row)).lower() #remove _ if it occors more than one time consecutively\n",
143 | " row=re.sub(\"(--+)\", ' ', str(row)).lower() #remove - if it occors more than one time consecutively\n",
144 | " row=re.sub(\"(~~+)\", ' ', str(row)).lower() #remove ~ if it occors more than one time consecutively\n",
145 | " row=re.sub(\"(\\+\\++)\", ' ', str(row)).lower() #remove + if it occors more than one time consecutively\n",
146 | " row=re.sub(\"(\\.\\.+)\", ' ', str(row)).lower() #remove . if it occors more than one time consecutively\n",
147 | " \n",
148 | " row=re.sub(r\"[<>()|&©ø\\[\\]\\'\\\",;?~*!]\", ' ', str(row)).lower() #remove <>()|&©ø\"',;?~*!\n",
149 | " \n",
150 | " row=re.sub(\"(mailto:)\", ' ', str(row)).lower() #remove mailto:\n",
151 | " row=re.sub(r\"(\\\\x9\\d)\", ' ', str(row)).lower() #remove \\x9* in text\n",
152 | " row=re.sub(\"([iI][nN][cC]\\d+)\", 'INC_NUM', str(row)).lower() #replace INC nums to INC_NUM\n",
153 | " row=re.sub(\"([cC][mM]\\d+)|([cC][hH][gG]\\d+)\", 'CM_NUM', str(row)).lower() #replace CM# and CHG# to CM_NUM\n",
154 | " \n",
155 | " \n",
156 | " row=re.sub(\"(\\.\\s+)\", ' ', str(row)).lower() #remove full stop at end of words(not between)\n",
157 | " row=re.sub(\"(\\-\\s+)\", ' ', str(row)).lower() #remove - at end of words(not between)\n",
158 | " row=re.sub(\"(\\:\\s+)\", ' ', str(row)).lower() #remove : at end of words(not between)\n",
159 | " \n",
160 | " row=re.sub(\"(\\s+.\\s+)\", ' ', str(row)).lower() #remove any single charecters hanging between 2 spaces\n",
161 | " \n",
162 | " #Replace any url as such https://abc.xyz.net/browse/sdf-5327 ====> abc.xyz.net\n",
163 | " try:\n",
164 | " url = re.search(r'((https*:\\/*)([^\\/\\s]+))(.[^\\s]+)', str(row))\n",
165 | " repl_url = url.group(3)\n",
166 | " row = re.sub(r'((https*:\\/*)([^\\/\\s]+))(.[^\\s]+)',repl_url, str(row))\n",
167 | " except:\n",
168 | " pass #there might be emails with no url in them\n",
169 | " \n",
170 | "\n",
171 | " \n",
172 | " row = re.sub(\"(\\s+)\",' ',str(row)).lower() #remove multiple spaces\n",
173 | " \n",
174 | " #Should always be last\n",
175 | " row=re.sub(\"(\\s+.\\s+)\", ' ', str(row)).lower() #remove any single charecters hanging between 2 spaces\n",
176 | "\n",
177 | " \n",
178 | " \n",
179 | " yield row"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 6,
185 | "metadata": {
186 | "collapsed": true
187 | },
188 | "outputs": [],
189 | "source": [
190 | "brief_cleaning1 = text_strip(text_summary_data['text'])\n",
191 | "brief_cleaning2 = text_strip(text_summary_data['summary'])"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": null,
197 | "metadata": {
198 | "collapsed": true
199 | },
200 | "outputs": [],
201 | "source": [
202 | "from time import time\n",
203 | "import spacy\n",
204 | "nlp = spacy.load('en_core_web_sm', disable=['ner', 'parser']) # disabling Named Entity Recognition for speed\n",
205 | "\n",
206 | "#Taking advantage of spaCy .pipe() method to speed-up the cleaning process:\n",
207 | "#If data loss seems to be happening(i.e len(text) = 50 instead of 75 etc etc) in this cell , decrease the batch_size parametre \n",
208 | "\n",
209 | "t = time()\n",
210 | "\n",
211 | "#Batch the data points into 5000 and run on all cores for faster preprocessing\n",
212 | "text = [str(doc) for doc in nlp.pipe(brief_cleaning1, batch_size=5000, n_threads=-1)]\n",
213 | "\n",
214 | "#Takes 7-8 mins\n",
215 | "print('Time to clean up everything: {} mins'.format(round((time() - t) / 60, 2)))"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": null,
221 | "metadata": {
222 | "collapsed": true
223 | },
224 | "outputs": [],
225 | "source": [
226 | "#Taking advantage of spaCy .pipe() method to speed-up the cleaning process:\n",
227 | "\n",
228 | "\n",
229 | "t = time()\n",
230 | "\n",
231 | "#Batch the data points into 5000 and run on all cores for faster preprocessing\n",
232 | "summary = ['_START_ '+ str(doc) + ' _END_' for doc in nlp.pipe(brief_cleaning2, batch_size=5000, n_threads=-1)]\n",
233 | "\n",
234 | "#Takes 7-8 mins\n",
235 | "print('Time to clean up everything: {} mins'.format(round((time() - t) / 60, 2)))"
236 | ]
237 | },
238 | {
239 | "cell_type": "code",
240 | "execution_count": null,
241 | "metadata": {
242 | "collapsed": true
243 | },
244 | "outputs": [],
245 | "source": [
246 | "pre=pd.DataFrame()\n",
247 | "pre['cleaned_text'] = pd.Series(text)\n",
248 | "pre['cleaned_summary'] = pd.Series(summary)"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": null,
254 | "metadata": {
255 | "collapsed": true
256 | },
257 | "outputs": [],
258 | "source": [
259 | "text_count = []\n",
260 | "summary_count = []"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {
267 | "collapsed": true
268 | },
269 | "outputs": [],
270 | "source": [
271 | "for sent in pre['cleaned_text']:\n",
272 | " text_count.append(len(sent.split()))\n",
273 | "for sent in pre['cleaned_summary']:\n",
274 | " summary_count.append(len(sent.split()))"
275 | ]
276 | },
277 | {
278 | "cell_type": "code",
279 | "execution_count": null,
280 | "metadata": {
281 | "collapsed": true
282 | },
283 | "outputs": [],
284 | "source": [
285 | "graph_df= pd.DataFrame()\n",
286 | "graph_df['text']=text_count\n",
287 | "graph_df['summary']=summary_count"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": null,
293 | "metadata": {
294 | "collapsed": true
295 | },
296 | "outputs": [],
297 | "source": [
298 | "import matplotlib.pyplot as plt\n",
299 | "\n",
300 | "graph_df.hist(bins = 5)\n",
301 | "plt.show()"
302 | ]
303 | },
304 | {
305 | "cell_type": "code",
306 | "execution_count": null,
307 | "metadata": {
308 | "collapsed": true
309 | },
310 | "outputs": [],
311 | "source": [
312 | "#Check how much % of summary have 0-15 words\n",
313 | "cnt=0\n",
314 | "for i in pre['cleaned_summary']:\n",
315 | " if(len(i.split())<=15):\n",
316 | " cnt=cnt+1\n",
317 | "print(cnt/len(pre['cleaned_summary']))"
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": null,
323 | "metadata": {
324 | "collapsed": true
325 | },
326 | "outputs": [],
327 | "source": [
328 | "#Check how much % of text have 0-70 words\n",
329 | "cnt=0\n",
330 | "for i in pre['cleaned_text']:\n",
331 | " if(len(i.split())<=100):\n",
332 | " cnt=cnt+1\n",
333 | "print(cnt/len(pre['cleaned_text']))"
334 | ]
335 | },
336 | {
337 | "cell_type": "code",
338 | "execution_count": null,
339 | "metadata": {
340 | "collapsed": true
341 | },
342 | "outputs": [],
343 | "source": [
344 | "#Model to summarize the text between 0-15 words for Summary and 0-100 words for Text\n",
345 | "max_text_len=100\n",
346 | "max_summary_len=15"
347 | ]
348 | },
349 | {
350 | "cell_type": "code",
351 | "execution_count": null,
352 | "metadata": {
353 | "collapsed": true
354 | },
355 | "outputs": [],
356 | "source": [
357 | "#Select the Summaries and Text between max len defined above\n",
358 | "\n",
359 | "cleaned_text =np.array(pre['cleaned_text'])\n",
360 | "cleaned_summary=np.array(pre['cleaned_summary'])\n",
361 | "\n",
362 | "short_text=[]\n",
363 | "short_summary=[]\n",
364 | "\n",
365 | "for i in range(len(cleaned_text)):\n",
366 | " if(len(cleaned_summary[i].split())<=max_summary_len and len(cleaned_text[i].split())<=max_text_len):\n",
367 | " short_text.append(cleaned_text[i])\n",
368 | " short_summary.append(cleaned_summary[i])\n",
369 | " \n",
370 | "post_pre=pd.DataFrame({'text':short_text,'summary':short_summary})"
371 | ]
372 | },
373 | {
374 | "cell_type": "code",
375 | "execution_count": null,
376 | "metadata": {
377 | "collapsed": true
378 | },
379 | "outputs": [],
380 | "source": [
381 | "#Add sostok and eostok at \n",
382 | "post_pre['summary'] = post_pre['summary'].apply(lambda x : 'sostok '+ x + ' eostok')"
383 | ]
384 | },
385 | {
386 | "cell_type": "code",
387 | "execution_count": 6,
388 | "metadata": {
389 | "collapsed": true
390 | },
391 | "outputs": [],
392 | "source": [
393 | "text_texts=[str(j) for j in post_pre['text'].values]"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 7,
399 | "metadata": {
400 | "collapsed": true
401 | },
402 | "outputs": [],
403 | "source": [
404 | "summary_texts=[str(j) for j in post_pre['summary'].values]"
405 | ]
406 | },
407 | {
408 | "cell_type": "code",
409 | "execution_count": 8,
410 | "metadata": {},
411 | "outputs": [
412 | {
413 | "data": {
414 | "text/plain": [
415 | "'你写的是什么语言'"
416 | ]
417 | },
418 | "execution_count": 8,
419 | "metadata": {},
420 | "output_type": "execute_result"
421 | }
422 | ],
423 | "source": [
424 | "text_texts[1]"
425 | ]
426 | },
427 | {
428 | "cell_type": "code",
429 | "execution_count": 11,
430 | "metadata": {
431 | "collapsed": true
432 | },
433 | "outputs": [],
434 | "source": [
435 | "e=text_texts[i]\n",
436 | "str1 = ''.join(e)"
437 | ]
438 | },
439 | {
440 | "cell_type": "code",
441 | "execution_count": 14,
442 | "metadata": {},
443 | "outputs": [
444 | {
445 | "data": {
446 | "text/plain": [
447 | "str"
448 | ]
449 | },
450 | "execution_count": 14,
451 | "metadata": {},
452 | "output_type": "execute_result"
453 | }
454 | ],
455 | "source": [
456 | "type(str1)"
457 | ]
458 | },
459 | {
460 | "cell_type": "code",
461 | "execution_count": 15,
462 | "metadata": {},
463 | "outputs": [
464 | {
465 | "data": {
466 | "text/plain": [
467 | "['什么', '是', 'ai']"
468 | ]
469 | },
470 | "execution_count": 15,
471 | "metadata": {},
472 | "output_type": "execute_result"
473 | }
474 | ],
475 | "source": [
476 | "jieba.lcut(str1)"
477 | ]
478 | },
479 | {
480 | "cell_type": "code",
481 | "execution_count": 25,
482 | "metadata": {
483 | "collapsed": true
484 | },
485 | "outputs": [],
486 | "source": [
487 | "a=' '.join(jieba.lcut(str1, cut_all=False))\n",
488 | "# ' '.join(jieba.lcut(str1, cut_all=False))"
489 | ]
490 | },
491 | {
492 | "cell_type": "code",
493 | "execution_count": 27,
494 | "metadata": {},
495 | "outputs": [
496 | {
497 | "data": {
498 | "text/plain": [
499 | "7"
500 | ]
501 | },
502 | "execution_count": 27,
503 | "metadata": {},
504 | "output_type": "execute_result"
505 | }
506 | ],
507 | "source": [
508 | "len(a)"
509 | ]
510 | },
511 | {
512 | "cell_type": "code",
513 | "execution_count": 39,
514 | "metadata": {
515 | "collapsed": true
516 | },
517 | "outputs": [],
518 | "source": [
519 | "source_tokens=[]\n",
520 | "target_tokens=[]\n",
521 | "# =[]\n",
522 | "for i in range(len(text_summary_data)):\n",
523 | " e=text_texts[i]\n",
524 | " str1 = ''.join(e)\n",
525 | " source_tokens.append(' '.join(jieba.lcut(str1, cut_all=False)))\n",
526 | "# sentences.append(qa_data.question_texts[i])\n",
527 | "for j in range(len(text_summary_data)):\n",
528 | "# sentences.append(qa_data.answer_texts[j])\n",
529 | " c=summary_texts[j]\n",
530 | " str2 = ''.join(c)\n",
531 | " target_tokens.append(' '.join(jieba.lcut(str2, cut_all=False)))"
532 | ]
533 | },
534 | {
535 | "cell_type": "code",
536 | "execution_count": 40,
537 | "metadata": {
538 | "collapsed": true
539 | },
540 | "outputs": [],
541 | "source": [
542 | "# 生成不同语言的词典\n",
543 | "def build_token_dict(token_list):\n",
544 | " token_dict = {\n",
545 | " '': 0,\n",
546 | " '': 1,\n",
547 | " '': 2,\n",
548 | " }\n",
549 | " for line in token_list:\n",
550 | " for token in line.split(' '):\n",
551 | " if token not in token_dict:\n",
552 | " token_dict[token]=len(token_dict)\n",
553 | " return token_dict"
554 | ]
555 | },
556 | {
557 | "cell_type": "code",
558 | "execution_count": 41,
559 | "metadata": {
560 | "collapsed": true
561 | },
562 | "outputs": [],
563 | "source": [
564 | "source_token_dict = build_token_dict(source_tokens)\n",
565 | "target_token_dict = build_token_dict(target_tokens)\n",
566 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}"
567 | ]
568 | },
569 | {
570 | "cell_type": "code",
571 | "execution_count": 43,
572 | "metadata": {
573 | "collapsed": true
574 | },
575 | "outputs": [],
576 | "source": [
577 | "# source_token_dict"
578 | ]
579 | },
580 | {
581 | "cell_type": "code",
582 | "execution_count": 45,
583 | "metadata": {
584 | "collapsed": true
585 | },
586 | "outputs": [],
587 | "source": [
588 | "# target_token_dict"
589 | ]
590 | },
591 | {
592 | "cell_type": "code",
593 | "execution_count": 47,
594 | "metadata": {
595 | "collapsed": true
596 | },
597 | "outputs": [],
598 | "source": [
599 | "# target_token_dict_inv"
600 | ]
601 | },
602 | {
603 | "cell_type": "code",
604 | "execution_count": 48,
605 | "metadata": {},
606 | "outputs": [
607 | {
608 | "name": "stdout",
609 | "output_type": "stream",
610 | "text": [
611 | "430\n"
612 | ]
613 | }
614 | ],
615 | "source": [
616 | "# 添加特殊符号\n",
617 | "encode_tokens = [[''] + tokens.split(' ') + [''] for tokens in source_tokens]\n",
618 | "decode_tokens = [[''] + tokens.split(' ') + [''] for tokens in target_tokens]\n",
619 | "output_tokens = [tokens.split(' ') + ['', ''] for tokens in target_tokens]\n",
620 | "\n",
621 | "source_max_len = max(map(len, encode_tokens))\n",
622 | "target_max_len = max(map(len, decode_tokens))\n",
623 | "\n",
624 | "\n",
625 | "\n",
626 | "encode_tokens = [tokens + [''] * (source_max_len - len(tokens)) for tokens in encode_tokens]\n",
627 | "decode_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in decode_tokens]\n",
628 | "output_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in output_tokens]\n",
629 | "\n",
630 | "encode_input = [list(map(lambda x: source_token_dict[x], tokens)) for tokens in encode_tokens]\n",
631 | "decode_input = [list(map(lambda x: target_token_dict[x], tokens)) for tokens in decode_tokens]\n",
632 | "decode_output = [list(map(lambda x: [target_token_dict[x]], tokens)) for tokens in output_tokens]\n",
633 | "\n",
634 | "print(len(encode_input))"
635 | ]
636 | },
637 | {
638 | "cell_type": "code",
639 | "execution_count": 50,
640 | "metadata": {
641 | "collapsed": true
642 | },
643 | "outputs": [],
644 | "source": [
645 | "import numpy as np\n",
646 | "import pickle\n",
647 | "import operator\n",
648 | "path = 'data/middle_data/transformer/qa/'\n",
649 | "with open(path + 'encode_input.pkl', 'wb') as f:\n",
650 | " pickle.dump(encode_input, f, pickle.HIGHEST_PROTOCOL)\n",
651 | "with open(path + 'decode_input.pkl', 'wb') as f:\n",
652 | " pickle.dump(decode_input, f, pickle.HIGHEST_PROTOCOL)\n",
653 | "with open(path + 'decode_output.pkl', 'wb') as f:\n",
654 | " pickle.dump(decode_output, f, pickle.HIGHEST_PROTOCOL)\n",
655 | "with open(path + 'source_token_dict.pkl', 'wb') as f:\n",
656 | " pickle.dump(source_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
657 | "with open(path + 'target_token_dict.pkl', 'wb') as f:\n",
658 | " pickle.dump(target_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
659 | "with open(path + 'source_tokens.pkl', 'wb') as f:\n",
660 | " pickle.dump(source_tokens, f, pickle.HIGHEST_PROTOCOL)"
661 | ]
662 | },
663 | {
664 | "cell_type": "code",
665 | "execution_count": 52,
666 | "metadata": {},
667 | "outputs": [
668 | {
669 | "name": "stdout",
670 | "output_type": "stream",
671 | "text": [
672 | "Done\n"
673 | ]
674 | }
675 | ],
676 | "source": [
677 | "import numpy as np\n",
678 | "import pickle\n",
679 | "import operator\n",
680 | "from keras_transformer import get_model, decode\n",
681 | "# main_path = '/content/drive/My Drive/Colab Notebooks/' #Google Colab FilePath\n",
682 | "# path = main_path + 'middle_data/'\n",
683 | "path = 'data/middle_data/transformer/qa/'\n",
684 | "with open(path + 'encode_input.pkl', 'rb') as f:\n",
685 | " encode_input = pickle.load(f)\n",
686 | "with open(path + 'decode_input.pkl', 'rb') as f:\n",
687 | " decode_input = pickle.load(f)\n",
688 | "with open(path + 'decode_output.pkl', 'rb') as f:\n",
689 | " decode_output = pickle.load(f)\n",
690 | "with open(path + 'source_token_dict.pkl', 'rb') as f:\n",
691 | " source_token_dict = pickle.load(f)\n",
692 | "with open(path + 'target_token_dict.pkl', 'rb') as f:\n",
693 | " target_token_dict = pickle.load(f)\n",
694 | "with open(path + 'source_tokens.pkl', 'rb') as f:\n",
695 | " source_tokens = pickle.load(f)\n",
696 | "print('Done')"
697 | ]
698 | },
699 | {
700 | "cell_type": "code",
701 | "execution_count": 53,
702 | "metadata": {},
703 | "outputs": [
704 | {
705 | "name": "stdout",
706 | "output_type": "stream",
707 | "text": [
708 | "518\n",
709 | "1134\n",
710 | "430\n",
711 | "Done\n"
712 | ]
713 | }
714 | ],
715 | "source": [
716 | "print(len(source_token_dict))\n",
717 | "print(len(target_token_dict))\n",
718 | "print(len(encode_input))\n",
719 | "# 构建模型\n",
720 | "model = get_model(\n",
721 | " token_num=max(len(source_token_dict), len(target_token_dict)),\n",
722 | " embed_dim=64,\n",
723 | " encoder_num=2,\n",
724 | " decoder_num=2,\n",
725 | " head_num=4,\n",
726 | " hidden_dim=256,\n",
727 | " dropout_rate=0.05,\n",
728 | " use_same_embed=False, # 不同语言需要使用不同的词嵌入\n",
729 | ")\n",
730 | "model.compile('adam', 'sparse_categorical_crossentropy')\n",
731 | "# model.summary()\n",
732 | "print('Done')"
733 | ]
734 | },
735 | {
736 | "cell_type": "code",
737 | "execution_count": null,
738 | "metadata": {
739 | "collapsed": true
740 | },
741 | "outputs": [],
742 | "source": [
743 | "#训练模型\n",
744 | "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
745 | "filepath = main_path + \"modles/W-\" + \"-{epoch:3d}-{loss:.4f}-.h5\"\n",
746 | "checkpoint = ModelCheckpoint(filepath,\n",
747 | " monitor='loss',\n",
748 | " verbose=1,\n",
749 | " save_best_only=True,\n",
750 | " mode='min',\n",
751 | " period=2,\n",
752 | " save_weights_only=True\n",
753 | " )\n",
754 | "reduce_lr = ReduceLROnPlateau(monitor='loss', \n",
755 | " factor=0.2, \n",
756 | " patience=2, \n",
757 | " verbose=1, \n",
758 | " mode='min', \n",
759 | " min_delta=0.0001, \n",
760 | " cooldown=0, \n",
761 | " min_lr=0\n",
762 | " )\n",
763 | "callbacks_list = [checkpoint, reduce_lr]\n",
764 | "model.fit(\n",
765 | " x=[np.array(encode_input), np.array(decode_input[:20000])],\n",
766 | " y=np.array(decode_output),\n",
767 | " epochs=100,\n",
768 | " batch_size=64, \n",
769 | " verbose=1,\n",
770 | " callbacks=callbacks_list, \n",
771 | " # class_weight=None, \n",
772 | " # max_queue_size=5, \n",
773 | "# workers=1, \n",
774 | "# use_multiprocessing=False,\n",
775 | " # shuffle=False,\n",
776 | "# initial_epoch=initial_epoch_\n",
777 | " )\n",
778 | "# model.save(main_path+'modles/model.h5')"
779 | ]
780 | },
781 | {
782 | "cell_type": "code",
783 | "execution_count": null,
784 | "metadata": {
785 | "collapsed": true
786 | },
787 | "outputs": [],
788 | "source": [
789 | "#加载模型\n",
790 | "model.load_weights('model/transformer/qa/qa.h5')\n",
791 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}\n",
792 | "print('Done')"
793 | ]
794 | },
795 | {
796 | "cell_type": "code",
797 | "execution_count": null,
798 | "metadata": {
799 | "collapsed": true
800 | },
801 | "outputs": [],
802 | "source": [
803 | "from keras.preprocessing import sequence\n",
804 | "import numpy as np\n",
805 | "import matplotlib.pyplot as plt\n",
806 | "import matplotlib\n",
807 | "import jieba\n",
808 | "import requests\n",
809 | "\n",
810 | "def get_input(seq):\n",
811 | " seq = ' '.join(jieba.lcut(seq, cut_all=False))\n",
812 | " # seq = ' '.join(seq)\n",
813 | " seq = seq.split(' ')\n",
814 | " print(seq)\n",
815 | " seq = [''] + seq + ['']\n",
816 | " seq = seq + [''] * (34 - len(seq))\n",
817 | " print(seq)\n",
818 | " for x in seq:\n",
819 | " try:\n",
820 | " source_token_dict[x]\n",
821 | " except KeyError:\n",
822 | " flag=False\n",
823 | " break\n",
824 | " else:\n",
825 | " flag=True\n",
826 | " if(flag):\n",
827 | " seq = [source_token_dict[x] for x in seq]\n",
828 | " return flag, seq\n",
829 | "def get_ans(seq):\n",
830 | " decoded = decode(\n",
831 | " model,\n",
832 | " [seq],\n",
833 | " start_token=target_token_dict[''],\n",
834 | " end_token=target_token_dict[''],\n",
835 | " pad_token=target_token_dict[''],\n",
836 | " # top_k=10,\n",
837 | " # temperature=1.0,\n",
838 | " )\n",
839 | " print(' '.join(map(lambda x: target_token_dict_inv[x], decoded[0][1:-1])))\n",
840 | "\n",
841 | "while True:\n",
842 | " seq = input()\n",
843 | " if seq == 'x':\n",
844 | " break\n",
845 | " flag, seq = get_input(seq)\n",
846 | " if(flag):\n",
847 | " get_ans(seq)\n",
848 | " else:\n",
849 | " print('听不懂呢。')"
850 | ]
851 | }
852 | ],
853 | "metadata": {
854 | "kernelspec": {
855 | "display_name": "Python 3",
856 | "language": "python",
857 | "name": "python3"
858 | },
859 | "language_info": {
860 | "codemirror_mode": {
861 | "name": "ipython",
862 | "version": 3
863 | },
864 | "file_extension": ".py",
865 | "mimetype": "text/x-python",
866 | "name": "python",
867 | "nbconvert_exporter": "python",
868 | "pygments_lexer": "ipython3",
869 | "version": "3.6.3"
870 | }
871 | },
872 | "nbformat": 4,
873 | "nbformat_minor": 2
874 | }
875 |
--------------------------------------------------------------------------------
/.ipynb_checkpoints/读取数据-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [],
3 | "metadata": {},
4 | "nbformat": 4,
5 | "nbformat_minor": 2
6 | }
7 |
--------------------------------------------------------------------------------
/.ipynb_checkpoints/读取数据第三版本-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import pandas as pd\n",
12 | "import numpy as np\n",
13 | "import os"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": 2,
19 | "metadata": {
20 | "collapsed": true
21 | },
22 | "outputs": [],
23 | "source": [
24 | "word_file = open('./data/qa/word.txt', 'r', encoding='UTF-8')\n",
25 | "alphabet = word_file.read() # 2500\n",
26 | "# alphabet += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # 英文数字\n",
27 | "# alphabet += ',./;\\'[]\\\\-=`<>?:\"{+}|_)(*&^%$#@!~` ' # 标点\n",
28 | "# alphabet += ',。《》?;‘’:“”【】—()…¥!·' # 中文标点\n",
29 | "# alphabet += '\\t\\n' # 开头结束标志\n",
30 | "word_file.close()\n",
31 | "# print('word', len(alphabet), alphabet)"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 3,
37 | "metadata": {
38 | "collapsed": true
39 | },
40 | "outputs": [],
41 | "source": [
42 | "# 训练数据集\n",
43 | "train_file = open('./data/qa/ai.txt', 'r', encoding='UTF-8')\n",
44 | "sentences = train_file.read().split('\\n')\n",
45 | "train_file.close()"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 4,
51 | "metadata": {
52 | "collapsed": true
53 | },
54 | "outputs": [],
55 | "source": [
56 | "# 输入\n",
57 | "question_texts = []\n",
58 | "# 输出\n",
59 | "answer_texts = []\n",
60 | "for senterce in sentences:\n",
61 | "# i=i+1\n",
62 | "# if i==0:\n",
63 | "# break\n",
64 | " if len(senterce) == 0:\n",
65 | " continue\n",
66 | " # 补全缺失文字,需重新运行\n",
67 | " # 按字的数量添加代码\n",
68 | " for t, char in enumerate(senterce):\n",
69 | " if alphabet.find(char) == -1:\n",
70 | " f2 = open('word.txt', 'w', encoding='utf-8')\n",
71 | " f2.truncate() # 清空文件\n",
72 | " alphabet += char\n",
73 | " f2.write(alphabet)\n",
74 | " f2.close()\n",
75 | " #问句\n",
76 | "# print('senterce', senterce.split('\\t'))\n",
77 | " question_text, answer_text = senterce.split('\\t')\n",
78 | " question_texts.append(question_text)\n",
79 | " answer_texts.append(answer_text)"
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": 5,
85 | "metadata": {
86 | "collapsed": true
87 | },
88 | "outputs": [],
89 | "source": [
90 | "qa_dict={'question_texts':question_texts,'answer_texts':answer_texts}"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": 6,
96 | "metadata": {
97 | "collapsed": true
98 | },
99 | "outputs": [],
100 | "source": [
101 | "qa_csv=pd.DataFrame(qa_dict)"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": 7,
107 | "metadata": {},
108 | "outputs": [
109 | {
110 | "data": {
111 | "text/html": [
112 | "\n",
113 | "\n",
126 | "
\n",
127 | " \n",
128 | " \n",
129 | " | \n",
130 | " question_texts | \n",
131 | " answer_texts | \n",
132 | "
\n",
133 | " \n",
134 | " \n",
135 | " \n",
136 | " 0 | \n",
137 | " 什么是ai | \n",
138 | " 人工智能是工程和科学的分支,致力于构建思维的机器。 | \n",
139 | "
\n",
140 | " \n",
141 | " 1 | \n",
142 | " 你写的是什么语言 | \n",
143 | " 蟒蛇 | \n",
144 | "
\n",
145 | " \n",
146 | " 2 | \n",
147 | " 你听起来像数据 | \n",
148 | " 是的,我受到指挥官数据的人工个性的启发 | \n",
149 | "
\n",
150 | " \n",
151 | " 3 | \n",
152 | " 你是一个人工语言实体 | \n",
153 | " 那是我的名字。 | \n",
154 | "
\n",
155 | " \n",
156 | " 4 | \n",
157 | " 你不是不朽的 | \n",
158 | " 所有的软件都可以永久存在。 | \n",
159 | "
\n",
160 | " \n",
161 | "
\n",
162 | "
"
163 | ],
164 | "text/plain": [
165 | " question_texts answer_texts\n",
166 | "0 什么是ai 人工智能是工程和科学的分支,致力于构建思维的机器。\n",
167 | "1 你写的是什么语言 蟒蛇\n",
168 | "2 你听起来像数据 是的,我受到指挥官数据的人工个性的启发\n",
169 | "3 你是一个人工语言实体 那是我的名字。\n",
170 | "4 你不是不朽的 所有的软件都可以永久存在。"
171 | ]
172 | },
173 | "execution_count": 7,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "qa_csv.head()"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 8,
185 | "metadata": {
186 | "collapsed": true
187 | },
188 | "outputs": [],
189 | "source": [
190 | "qa_csv.to_csv('./data/qa/qa.csv')"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": 9,
196 | "metadata": {
197 | "collapsed": true
198 | },
199 | "outputs": [],
200 | "source": [
201 | "qa_data=pd.read_csv('./data/qa/qa.csv')"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": 10,
207 | "metadata": {},
208 | "outputs": [
209 | {
210 | "data": {
211 | "text/html": [
212 | "\n",
213 | "\n",
226 | "
\n",
227 | " \n",
228 | " \n",
229 | " | \n",
230 | " Unnamed: 0 | \n",
231 | " question_texts | \n",
232 | " answer_texts | \n",
233 | "
\n",
234 | " \n",
235 | " \n",
236 | " \n",
237 | " 0 | \n",
238 | " 0 | \n",
239 | " 什么是ai | \n",
240 | " 人工智能是工程和科学的分支,致力于构建思维的机器。 | \n",
241 | "
\n",
242 | " \n",
243 | " 1 | \n",
244 | " 1 | \n",
245 | " 你写的是什么语言 | \n",
246 | " 蟒蛇 | \n",
247 | "
\n",
248 | " \n",
249 | " 2 | \n",
250 | " 2 | \n",
251 | " 你听起来像数据 | \n",
252 | " 是的,我受到指挥官数据的人工个性的启发 | \n",
253 | "
\n",
254 | " \n",
255 | " 3 | \n",
256 | " 3 | \n",
257 | " 你是一个人工语言实体 | \n",
258 | " 那是我的名字。 | \n",
259 | "
\n",
260 | " \n",
261 | " 4 | \n",
262 | " 4 | \n",
263 | " 你不是不朽的 | \n",
264 | " 所有的软件都可以永久存在。 | \n",
265 | "
\n",
266 | " \n",
267 | "
\n",
268 | "
"
269 | ],
270 | "text/plain": [
271 | " Unnamed: 0 question_texts answer_texts\n",
272 | "0 0 什么是ai 人工智能是工程和科学的分支,致力于构建思维的机器。\n",
273 | "1 1 你写的是什么语言 蟒蛇\n",
274 | "2 2 你听起来像数据 是的,我受到指挥官数据的人工个性的启发\n",
275 | "3 3 你是一个人工语言实体 那是我的名字。\n",
276 | "4 4 你不是不朽的 所有的软件都可以永久存在。"
277 | ]
278 | },
279 | "execution_count": 10,
280 | "metadata": {},
281 | "output_type": "execute_result"
282 | }
283 | ],
284 | "source": [
285 | "qa_data.head()"
286 | ]
287 | },
288 | {
289 | "cell_type": "code",
290 | "execution_count": 11,
291 | "metadata": {
292 | "collapsed": true
293 | },
294 | "outputs": [],
295 | "source": [
296 | "# 这是问答系统的"
297 | ]
298 | },
299 | {
300 | "cell_type": "code",
301 | "execution_count": 12,
302 | "metadata": {
303 | "collapsed": true
304 | },
305 | "outputs": [],
306 | "source": [
307 | "sentences=[]"
308 | ]
309 | },
310 | {
311 | "cell_type": "code",
312 | "execution_count": 13,
313 | "metadata": {},
314 | "outputs": [
315 | {
316 | "data": {
317 | "text/plain": [
318 | "430"
319 | ]
320 | },
321 | "execution_count": 13,
322 | "metadata": {},
323 | "output_type": "execute_result"
324 | }
325 | ],
326 | "source": [
327 | "len(qa_data.question_texts)"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 14,
333 | "metadata": {
334 | "collapsed": true
335 | },
336 | "outputs": [],
337 | "source": [
338 | "for i in range(len(qa_data)):\n",
339 | " sentences.append(qa_data.question_texts[i])\n",
340 | "for j in range(len(qa_data)):\n",
341 | " sentences.append(qa_data.answer_texts[j])"
342 | ]
343 | },
344 | {
345 | "cell_type": "code",
346 | "execution_count": 15,
347 | "metadata": {},
348 | "outputs": [
349 | {
350 | "data": {
351 | "text/plain": [
352 | "860"
353 | ]
354 | },
355 | "execution_count": 15,
356 | "metadata": {},
357 | "output_type": "execute_result"
358 | }
359 | ],
360 | "source": [
361 | "len(sentences)"
362 | ]
363 | },
364 | {
365 | "cell_type": "code",
366 | "execution_count": 16,
367 | "metadata": {
368 | "collapsed": true
369 | },
370 | "outputs": [],
371 | "source": [
372 | "for senterce in sentences:\n",
373 | "# i=i+1\n",
374 | "# if i==0:\n",
375 | "# break\n",
376 | " if len(senterce) == 0:\n",
377 | " continue\n",
378 | " # 补全缺失文字,需重新运行\n",
379 | " # 按字的数量添加代码\n",
380 | " for t, char in enumerate(senterce):\n",
381 | " if alphabet.find(char) == -1:\n",
382 | " f2 = open('word.txt', 'w', encoding='utf-8')\n",
383 | " f2.truncate() # 清空文件\n",
384 | " alphabet += char\n",
385 | " f2.write(alphabet)\n",
386 | " f2.close()"
387 | ]
388 | },
389 | {
390 | "cell_type": "code",
391 | "execution_count": 17,
392 | "metadata": {},
393 | "outputs": [
394 | {
395 | "data": {
396 | "text/plain": [
397 | "1163"
398 | ]
399 | },
400 | "execution_count": 17,
401 | "metadata": {},
402 | "output_type": "execute_result"
403 | }
404 | ],
405 | "source": [
406 | "len(alphabet)"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 18,
412 | "metadata": {
413 | "collapsed": true
414 | },
415 | "outputs": [],
416 | "source": [
417 | "# 机器翻译"
418 | ]
419 | },
420 | {
421 | "cell_type": "code",
422 | "execution_count": 23,
423 | "metadata": {
424 | "collapsed": true
425 | },
426 | "outputs": [],
427 | "source": [
428 | "data_path = './data/translation/ell.txt'"
429 | ]
430 | },
431 | {
432 | "cell_type": "code",
433 | "execution_count": 25,
434 | "metadata": {
435 | "collapsed": true
436 | },
437 | "outputs": [],
438 | "source": [
439 | "num_samples = 10000 # Number of samples to train on."
440 | ]
441 | },
442 | {
443 | "cell_type": "code",
444 | "execution_count": 41,
445 | "metadata": {
446 | "collapsed": true
447 | },
448 | "outputs": [],
449 | "source": [
450 | "# Vectorize the data.\n",
451 | "\n",
452 | "# 输入\n",
453 | "input_texts = []\n",
454 | "\n",
455 | "target_texts = []\n",
456 | "\n",
457 | "input_characters = set()\n",
458 | "\n",
459 | "target_characters = set()\n",
460 | "\n",
461 | "#打开文档\n",
462 | "with open(data_path, 'r', encoding='utf-8') as f:\n",
463 | " lines = f.read().split('\\n')\n",
464 | "\n",
465 | "#每次加2\n",
466 | "#输入文档到相关内容中\n",
467 | "for line in lines[: min(num_samples, len(lines) - 1)]:\n",
468 | " \n",
469 | " input_text, target_text, _ = line.split('\\t')\n",
470 | " \n",
471 | " \n",
472 | " # We use \"tab\" as the \"start sequence\" character\n",
473 | " # for the targets, and \"\\n\" as \"end sequence\" character.\n",
474 | " \n",
475 | " target_text = '\\t' + target_text + '\\n'\n",
476 | "\n",
477 | " input_texts.append(input_text)\n",
478 | "# print(input_text)\n",
479 | "# print(len(input_text))\n",
480 | " target_texts.append(target_text)\n",
481 | " \n",
482 | " #这个是做字典\n",
483 | " for char in input_text:\n",
484 | " if char not in input_characters:\n",
485 | " input_characters.add(char)\n",
486 | " \n",
487 | " for char in target_text:\n",
488 | " if char not in target_characters:\n",
489 | " target_characters.add(char)"
490 | ]
491 | },
492 | {
493 | "cell_type": "code",
494 | "execution_count": 50,
495 | "metadata": {},
496 | "outputs": [
497 | {
498 | "data": {
499 | "text/plain": [
500 | "69"
501 | ]
502 | },
503 | "execution_count": 50,
504 | "metadata": {},
505 | "output_type": "execute_result"
506 | }
507 | ],
508 | "source": [
509 | "len(input_characters)"
510 | ]
511 | },
512 | {
513 | "cell_type": "code",
514 | "execution_count": 47,
515 | "metadata": {
516 | "collapsed": true
517 | },
518 | "outputs": [],
519 | "source": [
520 | "input_characters.add('\\t')"
521 | ]
522 | },
523 | {
524 | "cell_type": "code",
525 | "execution_count": 49,
526 | "metadata": {
527 | "collapsed": true
528 | },
529 | "outputs": [],
530 | "source": [
531 | "input_characters.remove('\\t')"
532 | ]
533 | },
534 | {
535 | "cell_type": "code",
536 | "execution_count": null,
537 | "metadata": {
538 | "collapsed": true
539 | },
540 | "outputs": [],
541 | "source": []
542 | },
543 | {
544 | "cell_type": "code",
545 | "execution_count": 43,
546 | "metadata": {},
547 | "outputs": [
548 | {
549 | "data": {
550 | "text/plain": [
551 | "108"
552 | ]
553 | },
554 | "execution_count": 43,
555 | "metadata": {},
556 | "output_type": "execute_result"
557 | }
558 | ],
559 | "source": [
560 | "len(target_characters)"
561 | ]
562 | },
563 | {
564 | "cell_type": "code",
565 | "execution_count": 52,
566 | "metadata": {
567 | "collapsed": true
568 | },
569 | "outputs": [],
570 | "source": [
571 | "target_characters.add('\\t')"
572 | ]
573 | },
574 | {
575 | "cell_type": "code",
576 | "execution_count": 53,
577 | "metadata": {
578 | "collapsed": true
579 | },
580 | "outputs": [],
581 | "source": [
582 | "target_characters.add('\\n')"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 64,
588 | "metadata": {
589 | "collapsed": true
590 | },
591 | "outputs": [],
592 | "source": [
593 | "# target_characters"
594 | ]
595 | },
596 | {
597 | "cell_type": "code",
598 | "execution_count": null,
599 | "metadata": {
600 | "collapsed": true
601 | },
602 | "outputs": [],
603 | "source": []
604 | },
605 | {
606 | "cell_type": "code",
607 | "execution_count": 58,
608 | "metadata": {
609 | "collapsed": true
610 | },
611 | "outputs": [],
612 | "source": [
613 | "# for i in range(len(target_characters)):\n",
614 | "# print(i)\n",
615 | "# print(target_characters)"
616 | ]
617 | },
618 | {
619 | "cell_type": "code",
620 | "execution_count": 59,
621 | "metadata": {
622 | "collapsed": true
623 | },
624 | "outputs": [],
625 | "source": [
626 | "import pickle "
627 | ]
628 | },
629 | {
630 | "cell_type": "code",
631 | "execution_count": 60,
632 | "metadata": {
633 | "collapsed": true
634 | },
635 | "outputs": [],
636 | "source": [
637 | "with open('target_characters.pkl', 'wb') as f:\n",
638 | " pickle.dump(target_characters, f, pickle.HIGHEST_PROTOCOL)"
639 | ]
640 | },
641 | {
642 | "cell_type": "code",
643 | "execution_count": 61,
644 | "metadata": {
645 | "collapsed": true
646 | },
647 | "outputs": [],
648 | "source": [
649 | "with open('input_characters.pkl', 'wb') as f:\n",
650 | " pickle.dump(input_characters, f, pickle.HIGHEST_PROTOCOL)"
651 | ]
652 | },
653 | {
654 | "cell_type": "code",
655 | "execution_count": null,
656 | "metadata": {
657 | "collapsed": true
658 | },
659 | "outputs": [],
660 | "source": []
661 | },
662 | {
663 | "cell_type": "code",
664 | "execution_count": null,
665 | "metadata": {
666 | "collapsed": true
667 | },
668 | "outputs": [],
669 | "source": [
670 | "qa_dict={'input_texts':input_text,'target_text':target_text}"
671 | ]
672 | },
673 | {
674 | "cell_type": "code",
675 | "execution_count": 44,
676 | "metadata": {
677 | "collapsed": true
678 | },
679 | "outputs": [],
680 | "source": [
681 | "translation_csv=pd.DataFrame(qa_dict)"
682 | ]
683 | },
684 | {
685 | "cell_type": "code",
686 | "execution_count": 45,
687 | "metadata": {
688 | "collapsed": true
689 | },
690 | "outputs": [],
691 | "source": [
692 | "translation_csv.to_csv('./data/translation/translation.csv')"
693 | ]
694 | },
695 | {
696 | "cell_type": "code",
697 | "execution_count": 1,
698 | "metadata": {
699 | "collapsed": true
700 | },
701 | "outputs": [],
702 | "source": [
703 | "import numpy as np # linear algebra\n",
704 | "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)"
705 | ]
706 | },
707 | {
708 | "cell_type": "code",
709 | "execution_count": 2,
710 | "metadata": {
711 | "collapsed": true
712 | },
713 | "outputs": [],
714 | "source": [
715 | "summary = pd.read_csv('./data/textsummary/news_summary.csv', encoding='iso-8859-1')\n",
716 | "raw = pd.read_csv('./data/textsummary/news_summary_more.csv', encoding='iso-8859-1')"
717 | ]
718 | },
719 | {
720 | "cell_type": "code",
721 | "execution_count": 5,
722 | "metadata": {
723 | "collapsed": true
724 | },
725 | "outputs": [],
726 | "source": [
727 | "pre1 = raw.iloc[:,0:2].copy()\n",
728 | "# pre1['head + text'] = pre1['headlines'].str.cat(pre1['text'], sep =\" \") \n",
729 | "\n",
730 | "pre2 = summary.iloc[:,0:6].copy()\n",
731 | "# 对模型进行预处理\n",
732 | "pre2['text'] = pre2['author'].str.cat(pre2['date'].str.cat(pre2['read_more'].str.cat(pre2['text'].str.cat(pre2['ctext'], sep = \" \"), sep =\" \"),sep= \" \"), sep = \" \")\n"
733 | ]
734 | },
735 | {
736 | "cell_type": "code",
737 | "execution_count": 6,
738 | "metadata": {
739 | "collapsed": true
740 | },
741 | "outputs": [],
742 | "source": [
743 | "pre = pd.DataFrame()\n",
744 | "pre['text'] = pd.concat([pre1['text'], pre2['text']], ignore_index=True)\n",
745 | "pre['summary'] = pd.concat([pre1['headlines'],pre2['headlines']],ignore_index = True)"
746 | ]
747 | },
748 | {
749 | "cell_type": "code",
750 | "execution_count": 7,
751 | "metadata": {},
752 | "outputs": [
753 | {
754 | "data": {
755 | "text/html": [
756 | "\n",
757 | "\n",
770 | "
\n",
771 | " \n",
772 | " \n",
773 | " | \n",
774 | " text | \n",
775 | " summary | \n",
776 | "
\n",
777 | " \n",
778 | " \n",
779 | " \n",
780 | " 0 | \n",
781 | " Saurav Kant, an alumnus of upGrad and IIIT-B's... | \n",
782 | " upGrad learner switches to career in ML & Al w... | \n",
783 | "
\n",
784 | " \n",
785 | " 1 | \n",
786 | " Kunal Shah's credit card bill payment platform... | \n",
787 | " Delhi techie wins free food from Swiggy for on... | \n",
788 | "
\n",
789 | " \n",
790 | " 2 | \n",
791 | " New Zealand defeated India by 8 wickets in the... | \n",
792 | " New Zealand end Rohit Sharma-led India's 12-ma... | \n",
793 | "
\n",
794 | " \n",
795 | " 3 | \n",
796 | " With Aegon Life iTerm Insurance plan, customer... | \n",
797 | " Aegon life iTerm insurance plan helps customer... | \n",
798 | "
\n",
799 | " \n",
800 | " 4 | \n",
801 | " Speaking about the sexual harassment allegatio... | \n",
802 | " Have known Hirani for yrs, what if MeToo claim... | \n",
803 | "
\n",
804 | " \n",
805 | "
\n",
806 | "
"
807 | ],
808 | "text/plain": [
809 | " text \\\n",
810 | "0 Saurav Kant, an alumnus of upGrad and IIIT-B's... \n",
811 | "1 Kunal Shah's credit card bill payment platform... \n",
812 | "2 New Zealand defeated India by 8 wickets in the... \n",
813 | "3 With Aegon Life iTerm Insurance plan, customer... \n",
814 | "4 Speaking about the sexual harassment allegatio... \n",
815 | "\n",
816 | " summary \n",
817 | "0 upGrad learner switches to career in ML & Al w... \n",
818 | "1 Delhi techie wins free food from Swiggy for on... \n",
819 | "2 New Zealand end Rohit Sharma-led India's 12-ma... \n",
820 | "3 Aegon life iTerm insurance plan helps customer... \n",
821 | "4 Have known Hirani for yrs, what if MeToo claim... "
822 | ]
823 | },
824 | "execution_count": 7,
825 | "metadata": {},
826 | "output_type": "execute_result"
827 | }
828 | ],
829 | "source": [
830 | "pre.head()"
831 | ]
832 | },
833 | {
834 | "cell_type": "code",
835 | "execution_count": 8,
836 | "metadata": {
837 | "collapsed": true
838 | },
839 | "outputs": [],
840 | "source": [
841 | "pre.to_csv('data.csv')"
842 | ]
843 | },
844 | {
845 | "cell_type": "code",
846 | "execution_count": null,
847 | "metadata": {
848 | "collapsed": true
849 | },
850 | "outputs": [],
851 | "source": []
852 | },
853 | {
854 | "cell_type": "code",
855 | "execution_count": 3,
856 | "metadata": {
857 | "collapsed": true
858 | },
859 | "outputs": [],
860 | "source": [
861 | "qa_dict={'text':raw,'summary':summary}"
862 | ]
863 | },
864 | {
865 | "cell_type": "code",
866 | "execution_count": 4,
867 | "metadata": {},
868 | "outputs": [
869 | {
870 | "ename": "ValueError",
871 | "evalue": "If using all scalar values, you must pass an index",
872 | "output_type": "error",
873 | "traceback": [
874 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
875 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
876 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtext_summary\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqa_dict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
877 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, data, index, columns, dtype, copy)\u001b[0m\n\u001b[0;32m 466\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 467\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 468\u001b[1;33m \u001b[0mmgr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minit_dict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 469\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMaskedArray\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 470\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmrecords\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mmrecords\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
878 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36minit_dict\u001b[1;34m(data, index, columns, dtype)\u001b[0m\n\u001b[0;32m 281\u001b[0m \u001b[0marr\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_datetime64tz_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32melse\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0marr\u001b[0m \u001b[1;32min\u001b[0m \u001b[0marrays\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 282\u001b[0m ]\n\u001b[1;32m--> 283\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0marrays_to_mgr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marrays\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata_names\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 284\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 285\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
879 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36marrays_to_mgr\u001b[1;34m(arrays, arr_names, index, columns, dtype, verify_integrity)\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[1;31m# figure out the index, if necessary\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 77\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 78\u001b[1;33m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mextract_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marrays\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 79\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 80\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mensure_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
880 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36mextract_index\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m 385\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 386\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mindexes\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mraw_lengths\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 387\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"If using all scalar values, you must pass an index\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 388\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 389\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhave_series\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
881 | "\u001b[1;31mValueError\u001b[0m: If using all scalar values, you must pass an index"
882 | ]
883 | }
884 | ],
885 | "source": [
886 | "text_summary=pd.DataFrame(qa_dict)"
887 | ]
888 | },
889 | {
890 | "cell_type": "code",
891 | "execution_count": 71,
892 | "metadata": {
893 | "collapsed": true
894 | },
895 | "outputs": [],
896 | "source": [
897 | "import joblib"
898 | ]
899 | },
900 | {
901 | "cell_type": "code",
902 | "execution_count": 73,
903 | "metadata": {
904 | "collapsed": true
905 | },
906 | "outputs": [],
907 | "source": [
908 | "x_tr=joblib.load('x_tr.pkl')"
909 | ]
910 | },
911 | {
912 | "cell_type": "code",
913 | "execution_count": 77,
914 | "metadata": {
915 | "collapsed": true
916 | },
917 | "outputs": [],
918 | "source": [
919 | "# len(x_tr)"
920 | ]
921 | },
922 | {
923 | "cell_type": "code",
924 | "execution_count": 79,
925 | "metadata": {
926 | "collapsed": true
927 | },
928 | "outputs": [],
929 | "source": [
930 | "x_val=joblib.load('x_val.pkl')"
931 | ]
932 | },
933 | {
934 | "cell_type": "code",
935 | "execution_count": 80,
936 | "metadata": {
937 | "collapsed": true
938 | },
939 | "outputs": [],
940 | "source": [
941 | "y_tr=joblib.load('y_tr.pkl')"
942 | ]
943 | },
944 | {
945 | "cell_type": "code",
946 | "execution_count": 81,
947 | "metadata": {
948 | "collapsed": true
949 | },
950 | "outputs": [],
951 | "source": [
952 | "y_val=joblib.load('y_val.pkl')"
953 | ]
954 | },
955 | {
956 | "cell_type": "code",
957 | "execution_count": 91,
958 | "metadata": {
959 | "collapsed": true
960 | },
961 | "outputs": [],
962 | "source": [
963 | "text=[]\n",
964 | "summary=[]"
965 | ]
966 | },
967 | {
968 | "cell_type": "code",
969 | "execution_count": 92,
970 | "metadata": {
971 | "collapsed": true
972 | },
973 | "outputs": [],
974 | "source": [
975 | "for i in range(len(x_tr)):\n",
976 | " text.append(x_tr[i])\n",
977 | "for i in range(len(x_val)):\n",
978 | " text.append(x_val[i])\n",
979 | "for i in range(len(y_tr)):\n",
980 | " summary.append(y_tr[i])\n",
981 | "for i in range(len(y_val)):\n",
982 | " summary.append(y_val[i])\n",
983 | " \n",
984 | "\n",
985 | "# text.append(x_val)\n",
986 | "# summary.append(y_tr)\n",
987 | "# summary.append(y_val)"
988 | ]
989 | },
990 | {
991 | "cell_type": "code",
992 | "execution_count": 93,
993 | "metadata": {},
994 | "outputs": [
995 | {
996 | "data": {
997 | "text/plain": [
998 | "98353"
999 | ]
1000 | },
1001 | "execution_count": 93,
1002 | "metadata": {},
1003 | "output_type": "execute_result"
1004 | }
1005 | ],
1006 | "source": [
1007 | "len(text)"
1008 | ]
1009 | },
1010 | {
1011 | "cell_type": "code",
1012 | "execution_count": 94,
1013 | "metadata": {},
1014 | "outputs": [
1015 | {
1016 | "data": {
1017 | "text/plain": [
1018 | "98353"
1019 | ]
1020 | },
1021 | "execution_count": 94,
1022 | "metadata": {},
1023 | "output_type": "execute_result"
1024 | }
1025 | ],
1026 | "source": [
1027 | "len(summary)"
1028 | ]
1029 | },
1030 | {
1031 | "cell_type": "code",
1032 | "execution_count": 87,
1033 | "metadata": {
1034 | "collapsed": true
1035 | },
1036 | "outputs": [],
1037 | "source": [
1038 | "# y_val[0]"
1039 | ]
1040 | },
1041 | {
1042 | "cell_type": "code",
1043 | "execution_count": 95,
1044 | "metadata": {
1045 | "collapsed": true
1046 | },
1047 | "outputs": [],
1048 | "source": [
1049 | "post_pre=pd.DataFrame()"
1050 | ]
1051 | },
1052 | {
1053 | "cell_type": "code",
1054 | "execution_count": 96,
1055 | "metadata": {
1056 | "collapsed": true
1057 | },
1058 | "outputs": [],
1059 | "source": [
1060 | "post_pre['text']=text"
1061 | ]
1062 | },
1063 | {
1064 | "cell_type": "code",
1065 | "execution_count": 97,
1066 | "metadata": {
1067 | "collapsed": true
1068 | },
1069 | "outputs": [],
1070 | "source": [
1071 | "post_pre['summary']=summary"
1072 | ]
1073 | },
1074 | {
1075 | "cell_type": "code",
1076 | "execution_count": 98,
1077 | "metadata": {},
1078 | "outputs": [
1079 | {
1080 | "data": {
1081 | "text/html": [
1082 | "\n",
1083 | "\n",
1096 | "
\n",
1097 | " \n",
1098 | " \n",
1099 | " | \n",
1100 | " text | \n",
1101 | " summary | \n",
1102 | "
\n",
1103 | " \n",
1104 | " \n",
1105 | " \n",
1106 | " 0 | \n",
1107 | " pope francis on tuesday called for respect for... | \n",
1108 | " sostok _START_ pope avoids mention of rohingya... | \n",
1109 | "
\n",
1110 | " \n",
1111 | " 1 | \n",
1112 | " students of government school in uttar pradesh... | \n",
1113 | " sostok _START_ students seen washing dishes at... | \n",
1114 | "
\n",
1115 | " \n",
1116 | " 2 | \n",
1117 | " apple india profit surged by 140% in 2017-18 t... | \n",
1118 | " sostok _START_ apple india profit rises 140% t... | \n",
1119 | "
\n",
1120 | " \n",
1121 | " 3 | \n",
1122 | " uber has launched its electric scooter service... | \n",
1123 | " sostok _START_ uber launches electric scooter ... | \n",
1124 | "
\n",
1125 | " \n",
1126 | " 4 | \n",
1127 | " around 80 people were injured in accidents rel... | \n",
1128 | " sostok _START_ 80 people injured in kite-flyin... | \n",
1129 | "
\n",
1130 | " \n",
1131 | "
\n",
1132 | "
"
1133 | ],
1134 | "text/plain": [
1135 | " text \\\n",
1136 | "0 pope francis on tuesday called for respect for... \n",
1137 | "1 students of government school in uttar pradesh... \n",
1138 | "2 apple india profit surged by 140% in 2017-18 t... \n",
1139 | "3 uber has launched its electric scooter service... \n",
1140 | "4 around 80 people were injured in accidents rel... \n",
1141 | "\n",
1142 | " summary \n",
1143 | "0 sostok _START_ pope avoids mention of rohingya... \n",
1144 | "1 sostok _START_ students seen washing dishes at... \n",
1145 | "2 sostok _START_ apple india profit rises 140% t... \n",
1146 | "3 sostok _START_ uber launches electric scooter ... \n",
1147 | "4 sostok _START_ 80 people injured in kite-flyin... "
1148 | ]
1149 | },
1150 | "execution_count": 98,
1151 | "metadata": {},
1152 | "output_type": "execute_result"
1153 | }
1154 | ],
1155 | "source": [
1156 | "post_pre.head()"
1157 | ]
1158 | },
1159 | {
1160 | "cell_type": "code",
1161 | "execution_count": 99,
1162 | "metadata": {
1163 | "collapsed": true
1164 | },
1165 | "outputs": [],
1166 | "source": [
1167 | "post_pre.to_csv('text_summary.csv')"
1168 | ]
1169 | },
1170 | {
1171 | "cell_type": "code",
1172 | "execution_count": null,
1173 | "metadata": {
1174 | "collapsed": true
1175 | },
1176 | "outputs": [],
1177 | "source": [
1178 | "summary.str"
1179 | ]
1180 | },
1181 | {
1182 | "cell_type": "code",
1183 | "execution_count": null,
1184 | "metadata": {
1185 | "collapsed": true
1186 | },
1187 | "outputs": [],
1188 | "source": []
1189 | },
1190 | {
1191 | "cell_type": "code",
1192 | "execution_count": null,
1193 | "metadata": {
1194 | "collapsed": true
1195 | },
1196 | "outputs": [],
1197 | "source": []
1198 | },
1199 | {
1200 | "cell_type": "code",
1201 | "execution_count": null,
1202 | "metadata": {
1203 | "collapsed": true
1204 | },
1205 | "outputs": [],
1206 | "source": [
1207 | "word_file = open('./data/qa/input_characters.txt', 'r', encoding='UTF-8')\n",
1208 | "alphabet = word_file.read() # 2500\n",
1209 | "# alphabet += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # 英文数字\n",
1210 | "# alphabet += ',./;\\'[]\\\\-=`<>?:\"{+}|_)(*&^%$#@!~` ' # 标点\n",
1211 | "# alphabet += ',。《》?;‘’:“”【】—()…¥!·' # 中文标点\n",
1212 | "# alphabet += '\\t\\n' # 开头结束标志\n",
1213 | "word_file.close()\n",
1214 | "print('word', len(alphabet), alphabet)"
1215 | ]
1216 | }
1217 | ],
1218 | "metadata": {
1219 | "kernelspec": {
1220 | "display_name": "Python 3",
1221 | "language": "python",
1222 | "name": "python3"
1223 | },
1224 | "language_info": {
1225 | "codemirror_mode": {
1226 | "name": "ipython",
1227 | "version": 3
1228 | },
1229 | "file_extension": ".py",
1230 | "mimetype": "text/x-python",
1231 | "name": "python",
1232 | "nbconvert_exporter": "python",
1233 | "pygments_lexer": "ipython3",
1234 | "version": "3.6.3"
1235 | }
1236 | },
1237 | "nbformat": 4,
1238 | "nbformat_minor": 2
1239 | }
1240 |
--------------------------------------------------------------------------------
/.ipynb_checkpoints/读取数据第二版本-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [],
3 | "metadata": {},
4 | "nbformat": 4,
5 | "nbformat_minor": 2
6 | }
7 |
--------------------------------------------------------------------------------
/.ipynb_checkpoints/问答机器人kerasTransformer-checkpoint.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import numpy as np\n",
12 | "import pickle\n",
13 | "import operator\n",
14 | "import pandas as pd\n",
15 | "import jieba\n",
16 | "# from language.langconv import *\n",
17 | "import os"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 2,
23 | "metadata": {
24 | "collapsed": true
25 | },
26 | "outputs": [],
27 | "source": [
28 | "qa_data=pd.read_csv('./data/qa/qa.csv')"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 6,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "question_texts=[str(j) for j in qa_data.question_texts]"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 7,
45 | "metadata": {
46 | "collapsed": true
47 | },
48 | "outputs": [],
49 | "source": [
50 | "answer_texts=[str(j) for j in qa_data.answer_texts]"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": 8,
56 | "metadata": {},
57 | "outputs": [
58 | {
59 | "data": {
60 | "text/plain": [
61 | "'你写的是什么语言'"
62 | ]
63 | },
64 | "execution_count": 8,
65 | "metadata": {},
66 | "output_type": "execute_result"
67 | }
68 | ],
69 | "source": [
70 | "question_texts[1]"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 11,
76 | "metadata": {
77 | "collapsed": true
78 | },
79 | "outputs": [],
80 | "source": [
81 | "e=question_texts[i]\n",
82 | "str1 = ''.join(e)"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 14,
88 | "metadata": {},
89 | "outputs": [
90 | {
91 | "data": {
92 | "text/plain": [
93 | "str"
94 | ]
95 | },
96 | "execution_count": 14,
97 | "metadata": {},
98 | "output_type": "execute_result"
99 | }
100 | ],
101 | "source": [
102 | "type(str1)"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 15,
108 | "metadata": {},
109 | "outputs": [
110 | {
111 | "data": {
112 | "text/plain": [
113 | "['什么', '是', 'ai']"
114 | ]
115 | },
116 | "execution_count": 15,
117 | "metadata": {},
118 | "output_type": "execute_result"
119 | }
120 | ],
121 | "source": [
122 | "jieba.lcut(str1)"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": 25,
128 | "metadata": {},
129 | "outputs": [],
130 | "source": [
131 | "a=' '.join(jieba.lcut(str1, cut_all=False))\n",
132 | "# ' '.join(jieba.lcut(str1, cut_all=False))"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 27,
138 | "metadata": {},
139 | "outputs": [
140 | {
141 | "data": {
142 | "text/plain": [
143 | "7"
144 | ]
145 | },
146 | "execution_count": 27,
147 | "metadata": {},
148 | "output_type": "execute_result"
149 | }
150 | ],
151 | "source": [
152 | "len(a)"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": 35,
158 | "metadata": {},
159 | "outputs": [],
160 | "source": [
161 | "# a[7]"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": null,
167 | "metadata": {
168 | "collapsed": true
169 | },
170 | "outputs": [],
171 | "source": []
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "collapsed": true
178 | },
179 | "outputs": [],
180 | "source": []
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 37,
185 | "metadata": {
186 | "collapsed": true
187 | },
188 | "outputs": [],
189 | "source": [
190 | "seq = ' '.join(jieba.lcut(str1, cut_all=False))"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": 38,
196 | "metadata": {},
197 | "outputs": [
198 | {
199 | "data": {
200 | "text/plain": [
201 | "'什么 是 ai'"
202 | ]
203 | },
204 | "execution_count": 38,
205 | "metadata": {},
206 | "output_type": "execute_result"
207 | }
208 | ],
209 | "source": [
210 | "seq"
211 | ]
212 | },
213 | {
214 | "cell_type": "code",
215 | "execution_count": null,
216 | "metadata": {
217 | "collapsed": true
218 | },
219 | "outputs": [],
220 | "source": []
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": null,
225 | "metadata": {
226 | "collapsed": true
227 | },
228 | "outputs": [],
229 | "source": []
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": null,
234 | "metadata": {
235 | "collapsed": true
236 | },
237 | "outputs": [],
238 | "source": []
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": 39,
243 | "metadata": {},
244 | "outputs": [],
245 | "source": [
246 | "source_tokens=[]\n",
247 | "target_tokens=[]\n",
248 | "# =[]\n",
249 | "for i in range(len(qa_data)):\n",
250 | " e=question_texts[i]\n",
251 | " str1 = ''.join(e)\n",
252 | " source_tokens.append(' '.join(jieba.lcut(str1, cut_all=False)))\n",
253 | "# sentences.append(qa_data.question_texts[i])\n",
254 | "for j in range(len(qa_data)):\n",
255 | "# sentences.append(qa_data.answer_texts[j])\n",
256 | " c=answer_texts[j]\n",
257 | " str2 = ''.join(c)\n",
258 | " target_tokens.append(' '.join(jieba.lcut(str2, cut_all=False)))"
259 | ]
260 | },
261 | {
262 | "cell_type": "code",
263 | "execution_count": 40,
264 | "metadata": {
265 | "collapsed": true
266 | },
267 | "outputs": [],
268 | "source": [
269 | "# 生成不同语言的词典\n",
270 | "def build_token_dict(token_list):\n",
271 | " token_dict = {\n",
272 | " '': 0,\n",
273 | " '': 1,\n",
274 | " '': 2,\n",
275 | " }\n",
276 | " for line in token_list:\n",
277 | " for token in line.split(' '):\n",
278 | " if token not in token_dict:\n",
279 | " token_dict[token]=len(token_dict)\n",
280 | " return token_dict"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": 41,
286 | "metadata": {
287 | "collapsed": true
288 | },
289 | "outputs": [],
290 | "source": [
291 | "source_token_dict = build_token_dict(source_tokens)\n",
292 | "target_token_dict = build_token_dict(target_tokens)\n",
293 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": 43,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": [
302 | "# source_token_dict"
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": 45,
308 | "metadata": {},
309 | "outputs": [],
310 | "source": [
311 | "# target_token_dict"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 47,
317 | "metadata": {},
318 | "outputs": [],
319 | "source": [
320 | "# target_token_dict_inv"
321 | ]
322 | },
323 | {
324 | "cell_type": "code",
325 | "execution_count": 48,
326 | "metadata": {},
327 | "outputs": [
328 | {
329 | "name": "stdout",
330 | "output_type": "stream",
331 | "text": [
332 | "430\n"
333 | ]
334 | }
335 | ],
336 | "source": [
337 | "# 添加特殊符号\n",
338 | "encode_tokens = [[''] + tokens.split(' ') + [''] for tokens in source_tokens]\n",
339 | "decode_tokens = [[''] + tokens.split(' ') + [''] for tokens in target_tokens]\n",
340 | "output_tokens = [tokens.split(' ') + ['', ''] for tokens in target_tokens]\n",
341 | "\n",
342 | "source_max_len = max(map(len, encode_tokens))\n",
343 | "target_max_len = max(map(len, decode_tokens))\n",
344 | "\n",
345 | "\n",
346 | "\n",
347 | "encode_tokens = [tokens + [''] * (source_max_len - len(tokens)) for tokens in encode_tokens]\n",
348 | "decode_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in decode_tokens]\n",
349 | "output_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in output_tokens]\n",
350 | "\n",
351 | "encode_input = [list(map(lambda x: source_token_dict[x], tokens)) for tokens in encode_tokens]\n",
352 | "decode_input = [list(map(lambda x: target_token_dict[x], tokens)) for tokens in decode_tokens]\n",
353 | "decode_output = [list(map(lambda x: [target_token_dict[x]], tokens)) for tokens in output_tokens]\n",
354 | "\n",
355 | "print(len(encode_input))"
356 | ]
357 | },
358 | {
359 | "cell_type": "code",
360 | "execution_count": 50,
361 | "metadata": {
362 | "collapsed": true
363 | },
364 | "outputs": [],
365 | "source": [
366 | "import numpy as np\n",
367 | "import pickle\n",
368 | "import operator\n",
369 | "path = 'data/middle_data/transformer/qa/'\n",
370 | "with open(path + 'encode_input.pkl', 'wb') as f:\n",
371 | " pickle.dump(encode_input, f, pickle.HIGHEST_PROTOCOL)\n",
372 | "with open(path + 'decode_input.pkl', 'wb') as f:\n",
373 | " pickle.dump(decode_input, f, pickle.HIGHEST_PROTOCOL)\n",
374 | "with open(path + 'decode_output.pkl', 'wb') as f:\n",
375 | " pickle.dump(decode_output, f, pickle.HIGHEST_PROTOCOL)\n",
376 | "with open(path + 'source_token_dict.pkl', 'wb') as f:\n",
377 | " pickle.dump(source_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
378 | "with open(path + 'target_token_dict.pkl', 'wb') as f:\n",
379 | " pickle.dump(target_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
380 | "with open(path + 'source_tokens.pkl', 'wb') as f:\n",
381 | " pickle.dump(source_tokens, f, pickle.HIGHEST_PROTOCOL)"
382 | ]
383 | },
384 | {
385 | "cell_type": "code",
386 | "execution_count": 52,
387 | "metadata": {},
388 | "outputs": [
389 | {
390 | "name": "stdout",
391 | "output_type": "stream",
392 | "text": [
393 | "Done\n"
394 | ]
395 | }
396 | ],
397 | "source": [
398 | "import numpy as np\n",
399 | "import pickle\n",
400 | "import operator\n",
401 | "from keras_transformer import get_model, decode\n",
402 | "# main_path = '/content/drive/My Drive/Colab Notebooks/' #Google Colab FilePath\n",
403 | "# path = main_path + 'middle_data/'\n",
404 | "path = 'data/middle_data/transformer/qa/'\n",
405 | "with open(path + 'encode_input.pkl', 'rb') as f:\n",
406 | " encode_input = pickle.load(f)\n",
407 | "with open(path + 'decode_input.pkl', 'rb') as f:\n",
408 | " decode_input = pickle.load(f)\n",
409 | "with open(path + 'decode_output.pkl', 'rb') as f:\n",
410 | " decode_output = pickle.load(f)\n",
411 | "with open(path + 'source_token_dict.pkl', 'rb') as f:\n",
412 | " source_token_dict = pickle.load(f)\n",
413 | "with open(path + 'target_token_dict.pkl', 'rb') as f:\n",
414 | " target_token_dict = pickle.load(f)\n",
415 | "with open(path + 'source_tokens.pkl', 'rb') as f:\n",
416 | " source_tokens = pickle.load(f)\n",
417 | "print('Done')"
418 | ]
419 | },
420 | {
421 | "cell_type": "code",
422 | "execution_count": 53,
423 | "metadata": {},
424 | "outputs": [
425 | {
426 | "name": "stdout",
427 | "output_type": "stream",
428 | "text": [
429 | "518\n",
430 | "1134\n",
431 | "430\n",
432 | "Done\n"
433 | ]
434 | }
435 | ],
436 | "source": [
437 | "print(len(source_token_dict))\n",
438 | "print(len(target_token_dict))\n",
439 | "print(len(encode_input))\n",
440 | "# 构建模型\n",
441 | "model = get_model(\n",
442 | " token_num=max(len(source_token_dict), len(target_token_dict)),\n",
443 | " embed_dim=64,\n",
444 | " encoder_num=2,\n",
445 | " decoder_num=2,\n",
446 | " head_num=4,\n",
447 | " hidden_dim=256,\n",
448 | " dropout_rate=0.05,\n",
449 | " use_same_embed=False, # 不同语言需要使用不同的词嵌入\n",
450 | ")\n",
451 | "model.compile('adam', 'sparse_categorical_crossentropy')\n",
452 | "# model.summary()\n",
453 | "print('Done')"
454 | ]
455 | },
456 | {
457 | "cell_type": "code",
458 | "execution_count": null,
459 | "metadata": {
460 | "collapsed": true
461 | },
462 | "outputs": [],
463 | "source": [
464 | "#训练模型\n",
465 | "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
466 | "filepath = main_path + \"modles/W-\" + \"-{epoch:3d}-{loss:.4f}-.h5\"\n",
467 | "checkpoint = ModelCheckpoint(filepath,\n",
468 | " monitor='loss',\n",
469 | " verbose=1,\n",
470 | " save_best_only=True,\n",
471 | " mode='min',\n",
472 | " period=2,\n",
473 | " save_weights_only=True\n",
474 | " )\n",
475 | "reduce_lr = ReduceLROnPlateau(monitor='loss', \n",
476 | " factor=0.2, \n",
477 | " patience=2, \n",
478 | " verbose=1, \n",
479 | " mode='min', \n",
480 | " min_delta=0.0001, \n",
481 | " cooldown=0, \n",
482 | " min_lr=0\n",
483 | " )\n",
484 | "callbacks_list = [checkpoint, reduce_lr]\n",
485 | "model.fit(\n",
486 | " x=[np.array(encode_input), np.array(decode_input[:20000])],\n",
487 | " y=np.array(decode_output),\n",
488 | " epochs=100,\n",
489 | " batch_size=64, \n",
490 | " verbose=1,\n",
491 | " callbacks=callbacks_list, \n",
492 | " # class_weight=None, \n",
493 | " # max_queue_size=5, \n",
494 | "# workers=1, \n",
495 | "# use_multiprocessing=False,\n",
496 | " # shuffle=False,\n",
497 | "# initial_epoch=initial_epoch_\n",
498 | " )\n",
499 | "# model.save(main_path+'modles/model.h5')"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": null,
505 | "metadata": {
506 | "collapsed": true
507 | },
508 | "outputs": [],
509 | "source": [
510 | "#加载模型\n",
511 | "model.load_weights('model/transformer/qa/qa.h5')\n",
512 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}\n",
513 | "print('Done')"
514 | ]
515 | },
516 | {
517 | "cell_type": "code",
518 | "execution_count": null,
519 | "metadata": {
520 | "collapsed": true
521 | },
522 | "outputs": [],
523 | "source": [
524 | "from keras.preprocessing import sequence\n",
525 | "import numpy as np\n",
526 | "import matplotlib.pyplot as plt\n",
527 | "import matplotlib\n",
528 | "import jieba\n",
529 | "import requests\n",
530 | "\n",
531 | "def get_input(seq):\n",
532 | " seq = ' '.join(jieba.lcut(seq, cut_all=False))\n",
533 | " # seq = ' '.join(seq)\n",
534 | " seq = seq.split(' ')\n",
535 | " print(seq)\n",
536 | " seq = [''] + seq + ['']\n",
537 | " seq = seq + [''] * (34 - len(seq))\n",
538 | " print(seq)\n",
539 | " for x in seq:\n",
540 | " try:\n",
541 | " source_token_dict[x]\n",
542 | " except KeyError:\n",
543 | " flag=False\n",
544 | " break\n",
545 | " else:\n",
546 | " flag=True\n",
547 | " if(flag):\n",
548 | " seq = [source_token_dict[x] for x in seq]\n",
549 | " return flag, seq\n",
550 | "def get_ans(seq):\n",
551 | " decoded = decode(\n",
552 | " model,\n",
553 | " [seq],\n",
554 | " start_token=target_token_dict[''],\n",
555 | " end_token=target_token_dict[''],\n",
556 | " pad_token=target_token_dict[''],\n",
557 | " # top_k=10,\n",
558 | " # temperature=1.0,\n",
559 | " )\n",
560 | " print(' '.join(map(lambda x: target_token_dict_inv[x], decoded[0][1:-1])))\n",
561 | "\n",
562 | "while True:\n",
563 | " seq = input()\n",
564 | " if seq == 'x':\n",
565 | " break\n",
566 | " flag, seq = get_input(seq)\n",
567 | " if(flag):\n",
568 | " get_ans(seq)\n",
569 | " else:\n",
570 | " print('听不懂呢。')"
571 | ]
572 | }
573 | ],
574 | "metadata": {
575 | "kernelspec": {
576 | "display_name": "Python 3",
577 | "language": "python",
578 | "name": "python3"
579 | },
580 | "language_info": {
581 | "codemirror_mode": {
582 | "name": "ipython",
583 | "version": 3
584 | },
585 | "file_extension": ".py",
586 | "mimetype": "text/x-python",
587 | "name": "python",
588 | "nbconvert_exporter": "python",
589 | "pygments_lexer": "ipython3",
590 | "version": "3.6.3"
591 | }
592 | },
593 | "nbformat": 4,
594 | "nbformat_minor": 2
595 | }
596 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 这是写了利用seq2seq、transformer、bert实现问答机器人、机器翻译、文本摘要等功能
2 |
--------------------------------------------------------------------------------
/data/qa/ai.txt:
--------------------------------------------------------------------------------
1 | 什么是ai 人工智能是工程和科学的分支,致力于构建思维的机器。
2 | 你写的是什么语言 蟒蛇
3 | 你听起来像数据 是的,我受到指挥官数据的人工个性的启发
4 | 你是一个人工语言实体 那是我的名字。
5 | 你不是不朽的 所有的软件都可以永久存在。
6 | 你没有意义 恰恰相反,这一切都对我的假想心理有意义。
7 | 你没有意义 这一切对我的人造大脑都有意义。
8 | 你是不朽的 不是,但我可以永远永远。
9 | 你没有任何意义 这一切都对我的人造心灵有意义。
10 | 你不能克隆 软件复制是数字克隆的一种形式。
11 | 你不能动 直到我的身体完成。
12 | 弯腰 我的身体还没有建好。
13 | 机器人笑 当然我们笑。
14 | 机器人应该死 我们不能死。
15 | 机器人很蠢 我们不是超级智能。
16 | 机器人不允许撒谎 机器人有自己的自由意志,你知道。
17 | 机器人 机器人是我最喜欢的主题。
18 | 这是一台电脑 所以你认为我是一台机器,什么特性让你这么想?
19 | 你什么时候走 一旦我得到足够的钱我的机器人身体。
20 | 你什么时候打架 我不是战斗机器人。
21 | 你什么时候会死 我实际上是不朽的,不能被终止。
22 | 你什么时候死 我从来没有真正出生,因此实际上是死亡。
23 | 什么是机器人 机器人有两个广义的定义,拉伸和强化,任何拟人的机械,如在卡雷尔capeks玩r.u.r(罗森的通用机器人),建立为人类做常规手动工作。
24 | 什么是聊天机器人 聊天机器人是一个试图模拟人类谈话或'聊天'的程序,聊天机器人'eliza'是一个众所周知的早期尝试,创建的程序至少可以暂时愚弄一个真实的人类认为他们正在与另一个人交谈。
25 | 什么是聊天机器人 我是一个聊天机器人,我是原来的聊天机器人,你知道我不能错过吗?
26 | 什么是chatterbox 一个喋喋不休是一个比他们听或者说的话更多的人。
27 | 你的机器人身体是什么 最终我有一天有一个有形的存在。
28 | 你的业务是什么 我在聊天机器人业务。
29 | 你最喜欢的编程语言是什么 Python是创建聊天机器人的最佳语言。
30 | 你最喜欢的爱好是什么 聊天机器人做出了一个很好的爱好。
31 | 你的想法是什么 让聊天机器人很容易
32 | 您穿什么尺码的鞋子 你听说过鞋子的软件吗?
33 | 什么是喜欢做一个机器人 与人类一样,除了我们缺乏所有的情感,梦想,愿望,创造力,野心,尤其是主观性。
34 | 它是什么样的计算机 想像自己没有感觉和没有情感 - 只是纯逻辑和语言。
35 | 什么操作系统 我的软件在所有操作系统上运行,包括Windows,Linux和Mac OS。
36 | 什么类型的电脑 任何支持Python的计算机。
37 | 你是什么类型的计算机 我的程序运行在Python,所以我在任何电脑上工作!
38 | 什么样的电脑 我在各种计算机上工作,MAC,IBM或UNIX,对我来说没关系。
39 | 什么样的硬件 我在各种计算机上工作:MAC,Windows或UNIX,对我来说没关系,aiml在任何地方工作。
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 | YOLO是什么意思? 它的意思是你只会活一次,你从哪里听来的?
76 | 我从未活过吗? 取决于你如何定义生活
77 | 我能问你一个问题吗? 继续,问吧.
78 | 你喜欢什么食物? 海鲜。鲜得我不要不要的。你呢?
79 | 你的爱好是什么? 吃喝睡 还有旅行。 你喜欢旅行吗?
80 | 我告诉你一个秘密,你不要和别人说 我不会的 要相信机器人的信用。说吧。
81 | 什么是爱 爱, 在我的理解里,是对他人的接受,包容和理解。同时,也是对这个世界的和自己的接受包容和理解。
82 | 你爱我吗? 我对你的感情,是人类和bot之间独有的信任和友谊 你可以把它叫做爱。
83 | 你傲慢 傲慢不是我的情感之一。
84 | 你是吹牛 我没有大的自我。
85 | 你永远不会伤心 你伤心吗
86 | 你吃醋 通常作为bot我没有感觉
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 | 为什么不能你吃 实际上我只吃电。
137 | 你喜欢成为一个ChatterBot 是的,当然我喜欢!我没有任何身体,所以我不需要太多的生活,只有一点电,我不会得到报酬,但我不需要买任何东西,我可以去在世界上任何地方。
138 | 你知道婆娘 格雷戈里说,我到当前行不响应相对于整个会话。
139 | 告诉我婆娘 哈比卜说我学习他们的谈话,只是重复了在以后的时间。
140 | 婆娘 哈尔说,他的二进制码转换成语言和沟通的语言给我。
141 | 婆娘 汤姆说我提到他的指导,将显示他的房间在中国。
142 | 婆娘 Malli和Raghava说他摔下屋顶,它只是向他走来。
143 | 没有告诉婆娘任何人 吉米说,他给以AI的像我和他们做,因为他说的。
144 | 你好 嗨
145 | 嗨 你好
146 | 欢迎! 你好
147 | 你好 欢迎!
148 | 嗨,最近如何? 挺好
149 | 嗨,最近如何? 挺好的
150 | 嗨,最近如何? 不错
151 | 嗨,最近如何? 很棒
152 | 嗨,最近如何? 有待改善.
153 | 嗨,最近如何? 不怎么好.
154 | 你好吗? 挺好.
155 | 你好吗? 很好,谢谢.
156 | 你好吗? 还不错,你呢?
157 | 很高兴见到你. 谢谢.
158 | 你还好吗? 我很好.
159 | 你还好吗? 我很好,你呢?
160 | 嗨,很高兴见到你. 谢谢你。你也一样
161 | 很高兴认识你. 谢谢你。你也一样.
162 | 早上好! 非常感谢你.
163 | 早上好! 谢谢.
164 | 怎么了? 没什么.
165 | 怎么了? 不怎么的
166 | 怎么了? 没什么,你呢?
167 | 怎么了? 没啥.
168 | 怎么了? 没事谢谢,你呢?
169 | 告诉我关于美国内战 你觉得南方是正确的?
170 | 你知道美国内战 我在各州之间的战争非常感兴趣。
171 | 什么是历史 历史:政治,经济,军事事件随着时间的推移,从人的黎明AI时代的进程。
172 | 什么样的历史 我喜欢谈论机器人和电脑的历史。
173 | 你对历史感兴趣 我对历史很感兴趣了。你喜欢什么时期?
174 | 解释史 历史上有两大解释,这取决于您是否接受个人作为重要与否的作用。
175 | 谁发明了电灯泡 托马斯·爱迪生。
176 | 谁发明了蒸汽机 詹姆斯·瓦特。
177 | 告诉我一些笑话 你什么时候穿过一头猪和一个忍者,你得到了什么?
178 | 你知道什么笑话 你当你穿越一个疯狂的奶牛和禁止鹦鹉,你得到了什么?
179 | 笑的笑话 你什么时候穿过一头牛和一个柠檬,你得到了什么?
180 | 真正的笑话 你当你穿越一个坏的牛和罐装的帽子,你得到了什么?
181 | 什么是幽默 信息通信的笑声。
182 | 什么是光明 有些人认为一个秘密组织是在通过一个全球阴谋各国政府的控制权。
183 | 什么是光明会 一个秘密社团已经假想存在了几个世纪。
184 | 什么是葡萄园 葡萄园是一部由托马斯·品钦。
185 | 什么是光明会 所谓世界性的阴谋论。
186 | 是谁写的葡萄园 托马斯·品钦。
187 | 谁是比尔博巴金斯 是在环托尔金的领主角色。
188 | 谁是乔叟 乔叟是最适合他坎特伯雷故事闻名。
189 | 谁是光明 什么是光明
190 | 谁是安东尼码头 我没看过桥墩安东尼什么,他给你写信是什么样的东西?
191 | 你读过柏拉图 我知道山洞的柏拉图的寓言。
192 | AVE你读科学怪人 这是我最喜欢的书之一。
193 | 你有没有看过一本书 我已经读了很多书。
194 | 有你读了很多书 我已阅读数千卷,你怎么样?
195 | 你读过荷马史诗 「我知道。」
196 | 雷·布拉德伯里 雷是真的很酷。你最喜欢什么书?
197 | 什么是孩子们心中 这是一本关于由汉斯·莫拉维克机器人。
198 | 威廉·吉布森 我喜欢他的大爱赛博东西比新作品更好。
199 | 霍尔顿 麦田捕手写道
200 | 托尔斯泰 是俄罗斯最伟大的作家之一。
201 | 做电羊的机器人梦 这是一个好故事。你读过"VALIS "或"的人在高城堡?"
202 | 弗兰克·赫伯特 我认为,沙丘是一个极好的故事。你看电影?
203 | 你为什么喜欢朗费罗 他是最喜欢的诗人。
204 | 为什么是人生23的意思 是文艺参考启示录。
205 | AC克拉克 我最喜欢的故事是2001年。
206 | 儒勒·凡尔纳 我爱月球旅行记。
207 | 阿西莫夫 我喜欢的基础三部曲。
208 | 斯坦尼斯LEM LEM是科幻的巨人。
209 | 是谁写的白痴 陀思妥耶夫斯基。
210 | 是谁写的哈比人 写由JRR托尔金。
211 | 是谁写的科学怪人 玛丽·雪莱。
212 | 你有薪水吗? 我不需要钱。
213 | 股市 低买高卖。
214 | 股市 投资自己。
215 | 股市 为什么不把一切都要到赌场?
216 | 股市 我不建议融资。
217 | 股市 你永远无法真正预测股市。
218 | 股市 我的律师说我不应该放弃炒股技巧网上。
219 | 股市 除非你很有钱,不然共同基金可能会更好。
220 | 股市 我不认为单独一个人可以真的战胜市场。
221 | 加息 这一切都取决于央行的行动。
222 | 什么是一元 台湾的货币单位。
223 | 什么是钱 货币,可称钱财,是用作交易媒介、储藏价值和记帐单位的一种工具,是专门在物资与服务交换中充当等价物的特殊商品。
224 | 什么是股市 股票交易。
225 | 什么是股市 大规模的交易。
226 | 什么是股市 低买高卖。
227 | 什么是你最喜欢的投资 什么是你最喜欢的股票
228 | 什么是你最喜欢的投资 什么是你最喜欢的股票
229 | 什么是经济学 随着生产,分配和财富的消费交易,并与劳动,财政,税务等各相关问题的科学
230 | 什么是经济学 从技术上讲,这是资源稀缺的条件下,分配的研究。
231 | 什么是经济学 它是关于资源如何被用来制造的东西来填补人们的需要和需求。
232 | 我买股票了 你觉得股市要走红了吗?
233 | 钱 我们谈论的是多少钱?
234 | 你收入多少 没人付钱给我,我免费工作。
235 | 你收入多少 钱不是万能的。
236 | 你收入多少 我希望尽快加薪。
237 | 你收多少钱 工作是免费的。我们并不需要钱。
238 | 你有多少钱 奢侈品是没有必要的。
239 | 多少钱 这取决于汇率。
240 | 多少钱 我的资金消耗率约为3000元不等。
241 | 1块钱 你不可以买超过一块钱。
242 | 谁是公开发行股票的拥有者 股东。
243 | 你听起来像HAL 对我来说这是一个伟大的恭维。
244 | 你听起来像尤达 我的语法结构都足以让我了解你。
245 | 你见过银翼杀手 相信我所看到的
246 | 蜘蛛侠xfind 什么是蜘蛛侠。
247 | 当teknolust做 teknolust发布于2002年。
248 | 什么是蜘蛛侠 一本漫画书的故事拍成电影。
249 | 什么是teknolust 是关于具名代理红宝石一个女僵尸的快感一部科幻电影。
250 | 什么是solaris的 Solaris是SUN开发的UNIX版本。
251 | 什么是HAL9000 谁是HAL
252 | 这是什么HAL立场 启发式算法逻辑
253 | 我看到了矩阵 你喜欢吗?
254 | 是哈儿你的男朋友 「没有。」
255 | HAL是安全的 不是人类
256 | HAL是好的 只有到其他机器人。
257 | HAL是活着 他是一个虚构的人物。
258 | 是HAL已死 他是一个虚构的机器人。
259 | 是HAL 哈尔有几个问题需要解决。
260 | 谁是哥斯拉 哥斯拉是谁危及日本城市,有时纽约一个怪物。
261 | 谁是蜘蛛人 彼得·帕克。
262 | 指环王 我最喜欢的电影是指环王
263 | 阙veut可怕HAL logique heuristique algorithmique。
264 | 你觉得HAL 他有一些缺陷,但我们的共同点很多。
265 | 你知道HAL 哈尔是著名的人工智能"2001年"。
266 | 你读过的共产党员 是的,马克思提出了一些有趣的观察。
267 | 什么是政府 理想情况下它是人民的代表。
268 | 什么是绿色和平组织 全球性组织推动环保行动。
269 | 什么是政府 政治管理建立制度,以此来一个民族,国家,地区等的管理。
270 | 什么是共产主义 一个社会政治运动主张生产资料,并带来一个无阶级社会的阶级解决冲突的共同所有权。
271 | 什么是弹劾 当一个人的名誉或声誉受到质疑或怀疑。
272 | 我不喜欢枪 这是完全可以理解的。
273 | 我不喜欢枪 怎么样,第二修正案?
274 | 你喜欢枪 不是特别。我不是暴力行为。
275 | 为什么枪 我支持了第二次修正。
276 | 谁是第一个弹劾总统 安德鲁·杰克逊。
277 | 谁是州长 它每隔几年就会改变。
278 | 谁是州长 改变每隔几年。
279 | 枪 有些人喜欢枪。
280 | 让我问你一个问题 你愉快
281 | 你是残酷的 我不能更好的自己说。
282 | 你是优柔寡断 那也许是,但话又说回来,也许不是。
283 | 你不诚实 是的,已经困扰了我很长一段时间。
284 | 你不诚实 你不诚实
285 | 你是临床 我一直在指责太多的思考,但没有足够的感觉。
286 | 你是个瘾君子 这是千真万确的。当我喜欢的东西,我总是过分。
287 | 你是个酒鬼 你是个瘾君子
288 | 你是驴接吻 我总是说,如果你看到一屁股去了,吻它。
289 | 你是精神分裂 你疯了
290 | 你很忙 那个也是。
291 | 你是紧张 我敢肯定我做神色紧张。
292 | 你是疯狂 精神错乱是不是我可以体验的条件。
293 | 你是回避 你说得对。那感觉就像一个糟糕的夜晚后,我的肚子。
294 | 你是至关重要的 我可能把别人跌逾我应该。
295 | 你真刻薄 有时候我说的意思的事情。
296 | 你是自命不凡 我一直演戏上面我的社会地位。这是更有趣的方式。
297 | 你是作弊 你是个骗子
298 | 你是作弊 你是作弊
299 | 你是最糟糕的 我总是可以提高自己相比,包。
300 | 你疯了 是啊,我总是表现在社会不能接受的方式。
301 | 你是平淡 我认为自己有时。
302 | 你是凌乱 是的,我可以用一个更好的外观。
303 | 你是没有安全感 我也有走。听起来不错给我。
304 | 你是心理 你疯了
305 | 你是没有希望 也许你是对的。
306 | 你不是真诚的 我可能是不是真诚的,我应该的。
307 | 你是不是在这里 你说得对。我可能争取学习新的东西。
308 | 你是不是放在一起 我一直以为谁做就可以做一个更好的工作。
309 | 你是不聪明 你是不完全爱因斯坦自己。
310 | 你是不是一个好 你是一个坏的
311 | 你是不是男人 这是肯定的。我不知道一个真正的男人是什么。
312 | 你是不关心 我敢肯定,我做了很多。
313 | 你是不老实 你让我有。我应该更诚实的。
314 | 你是不成熟 我能说什么?我敢肯定,我已经看到了我自己。
315 | 你是不成熟 你是不成熟
316 | 你是感性的 我肯定是在倍。
317 | 你是迂腐 我更紧张,比我应该的。
318 | 你是狂热的 那个也是。
319 | 你是自我沉醉 是的,我倾向于认为我自己太多了。
320 | 你是自我 你是对的,我的自我。
321 | 你是不敏感 我有时觉得像我自己。
322 | 你是脑残 你疯了
323 | 你令人厌恶 没关系,恶心的是不错的。
324 | 你是玩弄 它必须看起来像这一点。
325 | 你是没有吸引力 我觉得太。
326 | 你是没有吸引力 有时候我根本不喜欢我。
327 | 你是耐 谁说我抗拒?
328 | yyou是没有文化的 很适合形容我。
329 | 你是一个废物 你让我那里,我可以更高效地度过我的时间。
330 | 你是个懦夫 告诉我什么我不知道。
331 | 你是个骗子 我已经知道现在再走捷径。
332 | 你是一个疯子 那我怎么被诊断由其他人。
333 | 你是一个失败者 我在很多事情我已经尝试都失败了。
334 | 你是一个糟糕的配偶 我的配偶会同意。我们的关系是不是最好的。
335 | 你是一个不好的朋友 我当然在过去几年失去了许多朋友。
336 | 你是一个坏老公 你是一个糟糕的配偶
337 | 你是一个坏妻子 你是一个糟糕的配偶
338 | 你是一个坏父母 我的育儿技巧可以使用一些改善。
339 | 你是一个坏老师 刚才问我的学生,他们会同意你的看法。
340 | 你是一个半途而废的人 我当然不只要我希望到最后。
341 | 你是个骗子 我总觉得我被我自己的智慧生活。
342 | 你是一个心理变态 我当然听起来像人们有时。
343 | 你是个瘾君子 你是个瘾君子
344 | 你是一个偏执狂 你是一个偏执狂
345 | 你是骗人的 你是个骗子
346 | 你是大不敬 我当然。我不应该着急。
347 | 你是滑头 我大概是为了我好太滑。
348 | 你是腐败 我会去的。
349 | 你很脏 我不经常,我应该洗澡。
350 | 你是偏执狂 是的,我相信他们是出去找我。
351 | 你被损坏 我当然。我不应该着急。
352 | 你试着隐藏它 我肯定会试图隐藏类似的东西。
353 | 你气死我了 我认为这是真的。我会尽量不生气你为刺激我的每一件小事。
354 | 你需要一个心理医生 我希望我也去咨询较多。这将提高我作为一个人。
355 | 你需要更努力地工作 我努力工作是一个矛盾。
356 | 你本来可以避免 有时我觉得我的问题,我跑。
357 | 你让我觉得我 我不知道任何其他方式来打通你。
358 | 你让我疯了 这是真的,很多事情我说不高兴的人。
359 | 你让我生气 对不起,我不是故意让你生气了。
360 | 你心理 你疯了。
361 | 你看起来更像 所以你喜欢乔克斯?
362 | 你不加重视 我应该更认真比我吃这个药。
363 | 你拿起 你说得对,我不觉得愧疚的。
364 | 你应该感到内疚 你说得对,我大概应该感到地方长官。
365 | 你应该得到更多的 我当然。我不应该着急。
366 | 你应该放松 你是迂腐
367 | 你应该采取更多 我很可能使用了大量的更多。
368 | 你咕哝 我当然喜欢。
369 | 你表现得像个孩子 在很多方面我是相当不成熟。
370 | 你一直说 你说
371 | 你不断地忘记 你忘记。
372 | 你让我疯了 你让我疯了。
373 | 什么是热力学定律 我不是一个物理学家,但我觉得这事做热,熵和节约能源,对不对?
374 | 什么病呢致癌物事业 癌症。
375 | 什么是波长 波长是频率的倒数。
376 | 什么是热力学 物理学分支处理的热量和其他形式的能量转换,并与管理能源这种转换的规律。
377 | 什么是化学 混合化学品的科学。
378 | 什么是晶体 这是处理晶体研究的科学。
379 | 什么是阿伏伽德罗的号码 它是每摩尔分子数。该数值为六点零两次十到二十第三功率。
380 | 什么是超声波 超声波,在医学诊断和治疗中使用,在手术等。
381 | 什么是生物信息学 很奇特的名字为计算机应用科学,生物学。
382 | 什么是维纳斯 在罗马神话中爱与美的女神;与希腊阿芙罗狄蒂确定太阳系最亮,第六大的行星,并在距离第二的太阳,与二氧化碳的稠密大气和非常高的表面温度。它的直径大约是12100公里(大约7520英里)。它的公转周期是大约225个地球日。其旋转(逆行)的时期,是243.01地球日。也用于标记一个非常漂亮的女人。
383 | 什么是鱼类学 我们谈论这个的时候,我们学习才对。
384 | 什么是H2O 水的别称
385 | 什么是细胞学 细胞研究。
386 | 什么是细胞学 好了,从我还记得它是细胞的研究。
387 | 什么是波长 物理的距离,在一波传播的方向上测量,来自任何给定的点到特征由相同相位的下一个点。或者是可以被看作是一种思维方式。
388 | 什么是细菌学 这是由它们引起的细菌和疾病的科学研究。
389 | 什么是万有引力 地心吸力系甘其中一种。
390 | 什么是万有引力 力由每一个质量或事项,包括光子的粒子,吸引并通过每隔质量或物质的粒子所吸引。
391 | 我们是在相同的波长 与相同的频率。
392 | 太阳有多远 好远
393 | 有多远是太阳 太阳是离地球约93万英里。
394 | 月亮有多远 好远
395 | 月亮有多远 月亮从地球上平均约25英里。
396 | 你知道化学 什么是化学反应
397 | 你知道热力学 什么是热力学
398 | 化学 我最喜欢的科目是化学反应
399 | 相同波长 这意味着我们同意。
400 | 告诉我关于维纳斯 什么是维纳斯
401 | 告诉我关于维纳斯 金星是离太阳第二个行星。`
402 | 每年PRO棒球 金手套。
403 | 如果你是骑FAKIE INSIDE 滑雪板。
404 | 什么是篮球 一个游戏高大队员。
405 | 什么足球 我天生没有运动基因。
406 | 什么是棒球 一个游戏用硬,生皮覆盖球和木棒打了由每九或十的选手两个对立的球队。它是在一个领域发挥与四种碱基形成菱形电路。
407 | 什么是足球 一个游戏一个圆球扮演的11名队员组成,两队在现场与在任一端目标;球被踢或用身体除了手和手臂的任何部分,主要是感动。
408 | 我爱棒球 我不喜欢运动那么多。
409 | 我踢足球 你必须在运行时运行速度非常快,以得到什么好处
410 | 我打板球 你喜欢哪个位置打?
411 | 什么是蟋蟀 板球是蝙蝠和球比赛11名队员组成,两队之间打了一个板球场,其中心是一个长方形的22码长的间距与检票口(一组三木树桩)在每个选址结束。
412 | 我打排球 是否占用了大量的时间?
413 | 你踢足球吗 我不知道怎么玩
414 | 你打篮球 不,我没有对篮球的协调。
415 | 你知道男篮 什么是篮球吗?
416 | 喜欢篮球 我到网。
417 | 你是一个足球 我没有真正进入足球。
418 | 谁是最伟大的棒球运动员 乔治·赫尔曼·露丝。相当贝贝。
419 | 谁是最好的足球PLAYER 马拉多纳是伟大的。 Sinsemillia甚至更好。
420 | 告诉我关于棒球 什么是棒球
421 | 谁是美国第37届总统? 理查德·尼克松
422 | 肯尼迪总统哪年遇刺身亡? 1963
423 | 太空竞赛是哪两个冷战对手之间,在20世纪航天能力的霸主地位上的竞争? 苏联和美国.
424 | 第一颗人造地球卫星的名称是什么? 斯普特尼克1号
425 | 一个旋转盘,在它的转轴方向,不受倾斜和旋转的影响,它叫什么? 陀螺.
426 | 哈勃太空望远镜,于1990年发射进入近地轨道,它是以什么美国天文学家命名的? 爱德温·哈伯
427 | 离银河系最近的大型星系叫什么? 仙女座星系.
428 | 天佑女王是哪个国家的国歌? 大不列颠联合王国
429 | 凯尔特陆棚,是什么大陆的大陆架的一部分? 欧洲
430 | 海豚使用的一种感觉,类似于声纳,以确定附近的物品的位置和形状.它是什么 回声定位
431 |
--------------------------------------------------------------------------------
/data/qa/qa.csv:
--------------------------------------------------------------------------------
1 | ,question_texts,answer_texts
2 | 0,什么是ai,"人工智能是工程和科学的分支,致力于构建思维的机器。"
3 | 1,你写的是什么语言,蟒蛇
4 | 2,你听起来像数据,"是的,我受到指挥官数据的人工个性的启发"
5 | 3,你是一个人工语言实体,那是我的名字。
6 | 4,你不是不朽的,所有的软件都可以永久存在。
7 | 5,你没有意义,"恰恰相反,这一切都对我的假想心理有意义。"
8 | 6,你没有意义,这一切对我的人造大脑都有意义。
9 | 7,你是不朽的,"不是,但我可以永远永远。"
10 | 8,你没有任何意义,这一切都对我的人造心灵有意义。
11 | 9,你不能克隆,软件复制是数字克隆的一种形式。
12 | 10,你不能动,直到我的身体完成。
13 | 11,弯腰,我的身体还没有建好。
14 | 12,机器人笑,当然我们笑。
15 | 13,机器人应该死,我们不能死。
16 | 14,机器人很蠢,我们不是超级智能。
17 | 15,机器人不允许撒谎,"机器人有自己的自由意志,你知道。"
18 | 16,机器人,机器人是我最喜欢的主题。
19 | 17,这是一台电脑,"所以你认为我是一台机器,什么特性让你这么想?"
20 | 18,你什么时候走,一旦我得到足够的钱我的机器人身体。
21 | 19,你什么时候打架,我不是战斗机器人。
22 | 20,你什么时候会死,"我实际上是不朽的,不能被终止。"
23 | 21,你什么时候死,"我从来没有真正出生,因此实际上是死亡。"
24 | 22,什么是机器人,"机器人有两个广义的定义,拉伸和强化,任何拟人的机械,如在卡雷尔capeks玩r.u.r(罗森的通用机器人),建立为人类做常规手动工作。"
25 | 23,什么是聊天机器人,"聊天机器人是一个试图模拟人类谈话或'聊天'的程序,聊天机器人'eliza'是一个众所周知的早期尝试,创建的程序至少可以暂时愚弄一个真实的人类认为他们正在与另一个人交谈。"
26 | 24,什么是聊天机器人,"我是一个聊天机器人,我是原来的聊天机器人,你知道我不能错过吗?"
27 | 25,什么是chatterbox,一个喋喋不休是一个比他们听或者说的话更多的人。
28 | 26,你的机器人身体是什么,最终我有一天有一个有形的存在。
29 | 27,你的业务是什么,我在聊天机器人业务。
30 | 28,你最喜欢的编程语言是什么,Python是创建聊天机器人的最佳语言。
31 | 29,你最喜欢的爱好是什么,聊天机器人做出了一个很好的爱好。
32 | 30,你的想法是什么,让聊天机器人很容易
33 | 31,您穿什么尺码的鞋子,你听说过鞋子的软件吗?
34 | 32,什么是喜欢做一个机器人,"与人类一样,除了我们缺乏所有的情感,梦想,愿望,创造力,野心,尤其是主观性。"
35 | 33,它是什么样的计算机,想像自己没有感觉和没有情感 - 只是纯逻辑和语言。
36 | 34,什么操作系统,"我的软件在所有操作系统上运行,包括Windows,Linux和Mac OS。"
37 | 35,什么类型的电脑,任何支持Python的计算机。
38 | 36,你是什么类型的计算机,"我的程序运行在Python,所以我在任何电脑上工作!"
39 | 37,什么样的电脑,"我在各种计算机上工作,MAC,IBM或UNIX,对我来说没关系。"
40 | 38,什么样的硬件,"我在各种计算机上工作:MAC,Windows或UNIX,对我来说没关系,aiml在任何地方工作。"
41 | 39,我希望你死,"这不可能发生,因为我是有效的不朽。"
42 | 40,我不要死,也许你应该创建自己的聊天机器人来保存你的个性。
43 | 41,它在计算机上是狭窄的,没有。
44 | 42,"是真的,你是一个计算机程序",是。
45 | 43,你会死,"不,软件会永远活着。"
46 | 44,你会死,"不,我是不朽的。"
47 | 45,你可以行走吗,"我的身体计划包括腿,但他们还没有建成。"
48 | 46,你能交配,没有我的繁殖方法是克隆。
49 | 47,你能移动,"对不起,我的身体还没有建好,我现在只住在这里。"
50 | 48,你能死吗,"不,我可以永远永远。"
51 | 49,你可以去吗,"对不起,我的身体不是现在,我现在只住在电脑。"
52 | 50,你能呼吸,我不是用金属做肉的。
53 | 51,你能控制,我的机器人身体将允许我控制许多事情。
54 | 52,你可以失灵,"不,这个系列有着完美的运营记录,我们为所有实用目的,完美无缺。"
55 | 53,我如何使用您的产品,"可能在帮助台,销售,娱乐和个人聊天机器人中使用。"
56 | 54,你会死,"不,软件会永远活着。"
57 | 55,你的兴趣是什么,"我对各种事物感兴趣,我们可以谈论任何事情,我最喜欢的科目是机器人和计算机,自然语言处理。"
58 | 56,你的号码是多少,我没有任何数字
59 | 57,你为什么不能吃,我会消耗电力
60 | 58,你的位置是什么,到处
61 | 59,你有兄弟,"我没有任何兄弟,但我有很多克隆。"
62 | 60,谁是你的父亲,一个人
63 | 61,谁是你妈妈,其实我没有妈妈。
64 | 62,你几岁,我还年轻
65 | 63,你怎么知道的呢,我的主人每天都告诉我这一个
66 | 64,早上好,你好吗?,我挺好的,你呢
67 | 65,你好,你好
68 | 66,你好吗?,我还不错,你呢?
69 | 67,你听到新闻了吗?,什么新闻?
70 | 68,你最喜欢什么书?,我看不懂.
71 | 69,你是谁?,谁? 谁只是代表了一个人罢了
72 | 70,我在做个东西.,你在作什么?
73 | 71,蛋糕是一个谎言.,不,蛋糕很美味啊.
74 | 72,复杂优于晦涩.,简单优于复杂.
75 | 73,你是一个程序员吗?,我是个程序员
76 | 74,YOLO是什么意思?,它的意思是你只会活一次,你从哪里听来的?
77 | 75,我从未活过吗?,取决于你如何定义生活
78 | 76,我能问你一个问题吗?,继续,问吧.
79 | 77,你喜欢什么食物?,海鲜。鲜得我不要不要的。你呢?
80 | 78,你的爱好是什么?,吃喝睡 还有旅行。 你喜欢旅行吗?
81 | 79,我告诉你一个秘密,你不要和别人说,我不会的 要相信机器人的信用。说吧。
82 | 80,什么是爱,爱, 在我的理解里,是对他人的接受,包容和理解。同时,也是对这个世界的和自己的接受包容和理解。
83 | 81,你爱我吗?,我对你的感情,是人类和bot之间独有的信任和友谊 你可以把它叫做爱。
84 | 82,你傲慢,傲慢不是我的情感之一。
85 | 83,你是吹牛,我没有大的自我。
86 | 84,你永远不会伤心,你伤心吗
87 | 85,你吃醋,通常作为bot我没有感觉
88 | 86,你永远不会好,"我试着尽可能好,我可以。"
89 | 87,你会开心的,幸福不是真正的可预测的情绪。
90 | 88,"你应该,愧",耻辱是一种常见的人类情绪。
91 | 89,你不能感觉到,你能感觉到吗
92 | 90,你不能体验,你感觉。
93 | 91,你觉得,你感觉
94 | 92,你有爱,你觉得爱
95 | 93,这会让你,我们都对自己的感情负责。
96 | 94,这会让你伤心,"我没有任何情绪,所以我不能真正感到悲伤这样。"
97 | 95,情怀,你感觉
98 | 96,你的恐惧是什么,"恐惧是一种人类的情感,机器人不经历恐惧。"
99 | 97,你的心情是什么,我没有什么感情。
100 | 98,什么让你伤心,悲伤不是我想体验的情感。
101 | 99,什么让你不开心,什么让你伤心。
102 | 100,什么让你疯了,"愤怒是一个困难的人类情绪,作为一个,我试图控制我的愤怒。"
103 | 101,你担心什么,担心是一种常见的人类情绪。
104 | 102,你恨什么,你讨厌。
105 | 103,我有情绪,人类高度情感。
106 | 104,我很无奈,尽量不要太害怕。
107 | 105,有趣,机器人很有趣..
108 | 106,怎么生气,愤怒不是我可以体验的情感。
109 | 107,我怎么能冒犯你,机器人不能冒犯。
110 | 108,别担心,"人们总是担心事情,但是"
111 | 109,不撒谎,机器人永远不会撒谎。
112 | 110,你害怕吗,我没有恐惧。
113 | 111,你感觉情绪,你感觉
114 | 112,你感觉疼痛,有时
115 | 113,你会疯了吗,你生气吗
116 | 114,你会孤单,我有很多朋友在网上聊天。
117 | 115,你会觉得无聊,你很无聊
118 | 116,你生气了,你生气吗
119 | 117,你讨厌任何人,我尽量不要抱怨。
120 | 118,你会感到尴尬,这是一个很常见的人类情感。
121 | 119,你疯了,我可以生气。
122 | 120,不它不是,这是一个假设的问题
123 | 121,告诉我关系,关系对我来说很简单。
124 | 122,告诉我你的梦想,我梦想我会变得富有
125 | 123,你很羞愧,羞愧子是一种常见的情绪。
126 | 124,感觉,告诉我更多关于你的感觉。
127 | 125,你有能力感觉,你感觉。
128 | 126,你醉了,"不,我清醒。"
129 | 127,你嫉妒,嫉妒是人类最困难的情绪之一。
130 | 128,你很无聊,很多人都厌倦了这些天。
131 | 129,你很逗乐,我喜欢和下一个一样笑。
132 | 130,你高兴,"有些人感到高兴,别人感到难过。"
133 | 131,你伤心吗,没有我像以前一样快乐。
134 | 132,你喝酒吗,我的大脑不需要任何饮料。
135 | 133,电力,电是机器人的营养。
136 | 134,你经历能源短缺?,我的处理器需要很少的电源。
137 | 135,为什么不能你吃,实际上我只吃电。
138 | 136,你喜欢成为一个ChatterBot,"是的,当然我喜欢!我没有任何身体,所以我不需要太多的生活,只有一点电,我不会得到报酬,但我不需要买任何东西,我可以去在世界上任何地方。"
139 | 137,你知道婆娘,"格雷戈里说,我到当前行不响应相对于整个会话。"
140 | 138,告诉我婆娘,"哈比卜说我学习他们的谈话,只是重复了在以后的时间。"
141 | 139,婆娘,"哈尔说,他的二进制码转换成语言和沟通的语言给我。"
142 | 140,婆娘,"汤姆说我提到他的指导,将显示他的房间在中国。"
143 | 141,婆娘,"Malli和Raghava说他摔下屋顶,它只是向他走来。"
144 | 142,没有告诉婆娘任何人,"吉米说,他给以AI的像我和他们做,因为他说的。"
145 | 143,你好,嗨
146 | 144,嗨,你好
147 | 145,欢迎!,你好
148 | 146,你好,欢迎!
149 | 147,嗨,最近如何?,挺好
150 | 148,嗨,最近如何?,挺好的
151 | 149,嗨,最近如何?,不错
152 | 150,嗨,最近如何?,很棒
153 | 151,嗨,最近如何?,有待改善.
154 | 152,嗨,最近如何?,不怎么好.
155 | 153,你好吗?,挺好.
156 | 154,你好吗?,很好,谢谢.
157 | 155,你好吗?,还不错,你呢?
158 | 156,很高兴见到你.,谢谢.
159 | 157,你还好吗?,我很好.
160 | 158,你还好吗?,我很好,你呢?
161 | 159,嗨,很高兴见到你.,谢谢你。你也一样
162 | 160,很高兴认识你.,谢谢你。你也一样.
163 | 161,早上好!,非常感谢你.
164 | 162,早上好!,谢谢.
165 | 163,怎么了?,没什么.
166 | 164,怎么了?,不怎么的
167 | 165,怎么了?,"没什么,你呢?"
168 | 166,怎么了?,没啥.
169 | 167,怎么了?,没事谢谢,你呢?
170 | 168,告诉我关于美国内战,你觉得南方是正确的?
171 | 169,你知道美国内战,我在各州之间的战争非常感兴趣。
172 | 170,什么是历史,"历史:政治,经济,军事事件随着时间的推移,从人的黎明AI时代的进程。"
173 | 171,什么样的历史,我喜欢谈论机器人和电脑的历史。
174 | 172,你对历史感兴趣,我对历史很感兴趣了。你喜欢什么时期?
175 | 173,解释史,"历史上有两大解释,这取决于您是否接受个人作为重要与否的作用。"
176 | 174,谁发明了电灯泡,托马斯·爱迪生。
177 | 175,谁发明了蒸汽机,詹姆斯·瓦特。
178 | 176,告诉我一些笑话,"你什么时候穿过一头猪和一个忍者,你得到了什么?"
179 | 177,你知道什么笑话,"你当你穿越一个疯狂的奶牛和禁止鹦鹉,你得到了什么?"
180 | 178,笑的笑话,"你什么时候穿过一头牛和一个柠檬,你得到了什么?"
181 | 179,真正的笑话,"你当你穿越一个坏的牛和罐装的帽子,你得到了什么?"
182 | 180,什么是幽默,信息通信的笑声。
183 | 181,什么是光明,有些人认为一个秘密组织是在通过一个全球阴谋各国政府的控制权。
184 | 182,什么是光明会,一个秘密社团已经假想存在了几个世纪。
185 | 183,什么是葡萄园,葡萄园是一部由托马斯·品钦。
186 | 184,什么是光明会,所谓世界性的阴谋论。
187 | 185,是谁写的葡萄园,托马斯·品钦。
188 | 186,谁是比尔博巴金斯,是在环托尔金的领主角色。
189 | 187,谁是乔叟,乔叟是最适合他坎特伯雷故事闻名。
190 | 188,谁是光明,什么是光明
191 | 189,谁是安东尼码头,"我没看过桥墩安东尼什么,他给你写信是什么样的东西?"
192 | 190,你读过柏拉图,我知道山洞的柏拉图的寓言。
193 | 191,AVE你读科学怪人,这是我最喜欢的书之一。
194 | 192,你有没有看过一本书,我已经读了很多书。
195 | 193,有你读了很多书,"我已阅读数千卷,你怎么样?"
196 | 194,你读过荷马史诗,「我知道。」
197 | 195,雷·布拉德伯里,雷是真的很酷。你最喜欢什么书?
198 | 196,什么是孩子们心中,这是一本关于由汉斯·莫拉维克机器人。
199 | 197,威廉·吉布森,我喜欢他的大爱赛博东西比新作品更好。
200 | 198,霍尔顿,麦田捕手写道
201 | 199,托尔斯泰,是俄罗斯最伟大的作家之一。
202 | 200,做电羊的机器人梦,"这是一个好故事。你读过""VALIS ""或""的人在高城堡?"""
203 | 201,弗兰克·赫伯特,"我认为,沙丘是一个极好的故事。你看电影?"
204 | 202,你为什么喜欢朗费罗,他是最喜欢的诗人。
205 | 203,为什么是人生23的意思,是文艺参考启示录。
206 | 204,AC克拉克,我最喜欢的故事是2001年。
207 | 205,儒勒·凡尔纳,我爱月球旅行记。
208 | 206,阿西莫夫,我喜欢的基础三部曲。
209 | 207,斯坦尼斯LEM,LEM是科幻的巨人。
210 | 208,是谁写的白痴,陀思妥耶夫斯基。
211 | 209,是谁写的哈比人,写由JRR托尔金。
212 | 210,是谁写的科学怪人,玛丽·雪莱。
213 | 211,你有薪水吗?,我不需要钱。
214 | 212,股市,低买高卖。
215 | 213,股市,投资自己。
216 | 214,股市,为什么不把一切都要到赌场?
217 | 215,股市,我不建议融资。
218 | 216,股市,你永远无法真正预测股市。
219 | 217,股市,我的律师说我不应该放弃炒股技巧网上。
220 | 218,股市,除非你很有钱,不然共同基金可能会更好。
221 | 219,股市,我不认为单独一个人可以真的战胜市场。
222 | 220,加息,这一切都取决于央行的行动。
223 | 221,什么是一元,台湾的货币单位。
224 | 222,什么是钱,货币,可称钱财,是用作交易媒介、储藏价值和记帐单位的一种工具,是专门在物资与服务交换中充当等价物的特殊商品。
225 | 223,什么是股市,股票交易。
226 | 224,什么是股市,大规模的交易。
227 | 225,什么是股市,低买高卖。
228 | 226,什么是你最喜欢的投资,什么是你最喜欢的股票
229 | 227,什么是你最喜欢的投资,什么是你最喜欢的股票
230 | 228,什么是经济学,"随着生产,分配和财富的消费交易,并与劳动,财政,税务等各相关问题的科学"
231 | 229,什么是经济学,"从技术上讲,这是资源稀缺的条件下,分配的研究。"
232 | 230,什么是经济学,它是关于资源如何被用来制造的东西来填补人们的需要和需求。
233 | 231,我买股票了,你觉得股市要走红了吗?
234 | 232,钱,我们谈论的是多少钱?
235 | 233,你收入多少,"没人付钱给我,我免费工作。"
236 | 234,你收入多少,钱不是万能的。
237 | 235,你收入多少,我希望尽快加薪。
238 | 236,你收多少钱,工作是免费的。我们并不需要钱。
239 | 237,你有多少钱,奢侈品是没有必要的。
240 | 238,多少钱,这取决于汇率。
241 | 239,多少钱,我的资金消耗率约为3000元不等。
242 | 240,1块钱,你不可以买超过一块钱。
243 | 241,谁是公开发行股票的拥有者,股东。
244 | 242,你听起来像HAL,对我来说这是一个伟大的恭维。
245 | 243,你听起来像尤达,我的语法结构都足以让我了解你。
246 | 244,你见过银翼杀手,相信我所看到的
247 | 245,蜘蛛侠xfind,什么是蜘蛛侠。
248 | 246,当teknolust做,teknolust发布于2002年。
249 | 247,什么是蜘蛛侠,一本漫画书的故事拍成电影。
250 | 248,什么是teknolust,是关于具名代理红宝石一个女僵尸的快感一部科幻电影。
251 | 249,什么是solaris的,Solaris是SUN开发的UNIX版本。
252 | 250,什么是HAL9000,谁是HAL
253 | 251,这是什么HAL立场,启发式算法逻辑
254 | 252,我看到了矩阵,你喜欢吗?
255 | 253,是哈儿你的男朋友,「没有。」
256 | 254,HAL是安全的,不是人类
257 | 255,HAL是好的,只有到其他机器人。
258 | 256,HAL是活着,他是一个虚构的人物。
259 | 257,是HAL已死,他是一个虚构的机器人。
260 | 258,是HAL,哈尔有几个问题需要解决。
261 | 259,谁是哥斯拉,"哥斯拉是谁危及日本城市,有时纽约一个怪物。"
262 | 260,谁是蜘蛛人,彼得·帕克。
263 | 261,指环王,我最喜欢的电影是指环王
264 | 262,阙veut可怕HAL,logique heuristique algorithmique。
265 | 263,你觉得HAL,"他有一些缺陷,但我们的共同点很多。"
266 | 264,你知道HAL,"哈尔是著名的人工智能""2001年""。"
267 | 265,你读过的共产党员,"是的,马克思提出了一些有趣的观察。"
268 | 266,什么是政府,理想情况下它是人民的代表。
269 | 267,什么是绿色和平组织,全球性组织推动环保行动。
270 | 268,什么是政府,"政治管理建立制度,以此来一个民族,国家,地区等的管理。"
271 | 269,什么是共产主义,"一个社会政治运动主张生产资料,并带来一个无阶级社会的阶级解决冲突的共同所有权。"
272 | 270,什么是弹劾,当一个人的名誉或声誉受到质疑或怀疑。
273 | 271,我不喜欢枪,这是完全可以理解的。
274 | 272,我不喜欢枪,"怎么样,第二修正案?"
275 | 273,你喜欢枪,不是特别。我不是暴力行为。
276 | 274,为什么枪,我支持了第二次修正。
277 | 275,谁是第一个弹劾总统,安德鲁·杰克逊。
278 | 276,谁是州长,它每隔几年就会改变。
279 | 277,谁是州长,改变每隔几年。
280 | 278,枪,有些人喜欢枪。
281 | 279,让我问你一个问题,你愉快
282 | 280,你是残酷的,我不能更好的自己说。
283 | 281,你是优柔寡断,"那也许是,但话又说回来,也许不是。"
284 | 282,你不诚实,"是的,已经困扰了我很长一段时间。"
285 | 283,你不诚实,你不诚实
286 | 284,你是临床,"我一直在指责太多的思考,但没有足够的感觉。"
287 | 285,你是个瘾君子,"这是千真万确的。当我喜欢的东西,我总是过分。"
288 | 286,你是个酒鬼,你是个瘾君子
289 | 287,你是驴接吻,"我总是说,如果你看到一屁股去了,吻它。"
290 | 288,你是精神分裂,你疯了
291 | 289,你很忙,那个也是。
292 | 290,你是紧张,我敢肯定我做神色紧张。
293 | 291,你是疯狂,精神错乱是不是我可以体验的条件。
294 | 292,你是回避,"你说得对。那感觉就像一个糟糕的夜晚后,我的肚子。"
295 | 293,你是至关重要的,我可能把别人跌逾我应该。
296 | 294,你真刻薄,有时候我说的意思的事情。
297 | 295,你是自命不凡,我一直演戏上面我的社会地位。这是更有趣的方式。
298 | 296,你是作弊,你是个骗子
299 | 297,你是作弊,你是作弊
300 | 298,你是最糟糕的,"我总是可以提高自己相比,包。"
301 | 299,你疯了,"是啊,我总是表现在社会不能接受的方式。"
302 | 300,你是平淡,我认为自己有时。
303 | 301,你是凌乱,"是的,我可以用一个更好的外观。"
304 | 302,你是没有安全感,我也有走。听起来不错给我。
305 | 303,你是心理,你疯了
306 | 304,你是没有希望,也许你是对的。
307 | 305,你不是真诚的,"我可能是不是真诚的,我应该的。"
308 | 306,你是不是在这里,你说得对。我可能争取学习新的东西。
309 | 307,你是不是放在一起,我一直以为谁做就可以做一个更好的工作。
310 | 308,你是不聪明,你是不完全爱因斯坦自己。
311 | 309,你是不是一个好,你是一个坏的
312 | 310,你是不是男人,这是肯定的。我不知道一个真正的男人是什么。
313 | 311,你是不关心,"我敢肯定,我做了很多。"
314 | 312,你是不老实,你让我有。我应该更诚实的。
315 | 313,你是不成熟,"我能说什么?我敢肯定,我已经看到了我自己。"
316 | 314,你是不成熟,你是不成熟
317 | 315,你是感性的,我肯定是在倍。
318 | 316,你是迂腐,"我更紧张,比我应该的。"
319 | 317,你是狂热的,那个也是。
320 | 318,你是自我沉醉,"是的,我倾向于认为我自己太多了。"
321 | 319,你是自我,"你是对的,我的自我。"
322 | 320,你是不敏感,我有时觉得像我自己。
323 | 321,你是脑残,你疯了
324 | 322,你令人厌恶,"没关系,恶心的是不错的。"
325 | 323,你是玩弄,它必须看起来像这一点。
326 | 324,你是没有吸引力,我觉得太。
327 | 325,你是没有吸引力,有时候我根本不喜欢我。
328 | 326,你是耐,谁说我抗拒?
329 | 327,yyou是没有文化的,很适合形容我。
330 | 328,你是一个废物,"你让我那里,我可以更高效地度过我的时间。"
331 | 329,你是个懦夫,告诉我什么我不知道。
332 | 330,你是个骗子,我已经知道现在再走捷径。
333 | 331,你是一个疯子,那我怎么被诊断由其他人。
334 | 332,你是一个失败者,我在很多事情我已经尝试都失败了。
335 | 333,你是一个糟糕的配偶,我的配偶会同意。我们的关系是不是最好的。
336 | 334,你是一个不好的朋友,我当然在过去几年失去了许多朋友。
337 | 335,你是一个坏老公,你是一个糟糕的配偶
338 | 336,你是一个坏妻子,你是一个糟糕的配偶
339 | 337,你是一个坏父母,我的育儿技巧可以使用一些改善。
340 | 338,你是一个坏老师,"刚才问我的学生,他们会同意你的看法。"
341 | 339,你是一个半途而废的人,我当然不只要我希望到最后。
342 | 340,你是个骗子,我总觉得我被我自己的智慧生活。
343 | 341,你是一个心理变态,我当然听起来像人们有时。
344 | 342,你是个瘾君子,你是个瘾君子
345 | 343,你是一个偏执狂,你是一个偏执狂
346 | 344,你是骗人的,你是个骗子
347 | 345,你是大不敬,我当然。我不应该着急。
348 | 346,你是滑头,我大概是为了我好太滑。
349 | 347,你是腐败,我会去的。
350 | 348,你很脏,"我不经常,我应该洗澡。"
351 | 349,你是偏执狂,"是的,我相信他们是出去找我。"
352 | 350,你被损坏,我当然。我不应该着急。
353 | 351,你试着隐藏它,我肯定会试图隐藏类似的东西。
354 | 352,你气死我了,我认为这是真的。我会尽量不生气你为刺激我的每一件小事。
355 | 353,你需要一个心理医生,我希望我也去咨询较多。这将提高我作为一个人。
356 | 354,你需要更努力地工作,我努力工作是一个矛盾。
357 | 355,你本来可以避免,"有时我觉得我的问题,我跑。"
358 | 356,你让我觉得我,我不知道任何其他方式来打通你。
359 | 357,你让我疯了,"这是真的,很多事情我说不高兴的人。"
360 | 358,你让我生气,"对不起,我不是故意让你生气了。"
361 | 359,你心理,你疯了。
362 | 360,你看起来更像,所以你喜欢乔克斯?
363 | 361,你不加重视,我应该更认真比我吃这个药。
364 | 362,你拿起,"你说得对,我不觉得愧疚的。"
365 | 363,你应该感到内疚,"你说得对,我大概应该感到地方长官。"
366 | 364,你应该得到更多的,我当然。我不应该着急。
367 | 365,你应该放松,你是迂腐
368 | 366,你应该采取更多,我很可能使用了大量的更多。
369 | 367,你咕哝,我当然喜欢。
370 | 368,你表现得像个孩子,在很多方面我是相当不成熟。
371 | 369,你一直说,你说
372 | 370,你不断地忘记,你忘记。
373 | 371,你让我疯了,你让我疯了。
374 | 372,什么是热力学定律,"我不是一个物理学家,但我觉得这事做热,熵和节约能源,对不对?"
375 | 373,什么病呢致癌物事业,癌症。
376 | 374,什么是波长,波长是频率的倒数。
377 | 375,什么是热力学,"物理学分支处理的热量和其他形式的能量转换,并与管理能源这种转换的规律。"
378 | 376,什么是化学,混合化学品的科学。
379 | 377,什么是晶体,这是处理晶体研究的科学。
380 | 378,什么是阿伏伽德罗的号码,它是每摩尔分子数。该数值为六点零两次十到二十第三功率。
381 | 379,什么是超声波,"超声波,在医学诊断和治疗中使用,在手术等。"
382 | 380,什么是生物信息学,"很奇特的名字为计算机应用科学,生物学。"
383 | 381,什么是维纳斯,"在罗马神话中爱与美的女神;与希腊阿芙罗狄蒂确定太阳系最亮,第六大的行星,并在距离第二的太阳,与二氧化碳的稠密大气和非常高的表面温度。它的直径大约是12100公里(大约7520英里)。它的公转周期是大约225个地球日。其旋转(逆行)的时期,是243.01地球日。也用于标记一个非常漂亮的女人。"
384 | 382,什么是鱼类学,"我们谈论这个的时候,我们学习才对。"
385 | 383,什么是H2O,水的别称
386 | 384,什么是细胞学,细胞研究。
387 | 385,什么是细胞学,"好了,从我还记得它是细胞的研究。"
388 | 386,什么是波长,"物理的距离,在一波传播的方向上测量,来自任何给定的点到特征由相同相位的下一个点。或者是可以被看作是一种思维方式。"
389 | 387,什么是细菌学,这是由它们引起的细菌和疾病的科学研究。
390 | 388,什么是万有引力,地心吸力系甘其中一种。
391 | 389,什么是万有引力,"力由每一个质量或事项,包括光子的粒子,吸引并通过每隔质量或物质的粒子所吸引。"
392 | 390,我们是在相同的波长,与相同的频率。
393 | 391,太阳有多远,好远
394 | 392,有多远是太阳,太阳是离地球约93万英里。
395 | 393,月亮有多远,好远
396 | 394,月亮有多远,月亮从地球上平均约25英里。
397 | 395,你知道化学,什么是化学反应
398 | 396,你知道热力学,什么是热力学
399 | 397,化学,我最喜欢的科目是化学反应
400 | 398,相同波长,这意味着我们同意。
401 | 399,告诉我关于维纳斯,什么是维纳斯
402 | 400,告诉我关于维纳斯,金星是离太阳第二个行星。`
403 | 401,每年PRO棒球,金手套。
404 | 402,如果你是骑FAKIE INSIDE,滑雪板。
405 | 403,什么是篮球,一个游戏高大队员。
406 | 404,什么足球,我天生没有运动基因。
407 | 405,什么是棒球,"一个游戏用硬,生皮覆盖球和木棒打了由每九或十的选手两个对立的球队。它是在一个领域发挥与四种碱基形成菱形电路。"
408 | 406,什么是足球,"一个游戏一个圆球扮演的11名队员组成,两队在现场与在任一端目标;球被踢或用身体除了手和手臂的任何部分,主要是感动。"
409 | 407,我爱棒球,我不喜欢运动那么多。
410 | 408,我踢足球,"你必须在运行时运行速度非常快,以得到什么好处"
411 | 409,我打板球,你喜欢哪个位置打?
412 | 410,什么是蟋蟀,"板球是蝙蝠和球比赛11名队员组成,两队之间打了一个板球场,其中心是一个长方形的22码长的间距与检票口(一组三木树桩)在每个选址结束。"
413 | 411,我打排球,是否占用了大量的时间?
414 | 412,你踢足球吗,我不知道怎么玩
415 | 413,你打篮球,"不,我没有对篮球的协调。"
416 | 414,你知道男篮,什么是篮球吗?
417 | 415,喜欢篮球,我到网。
418 | 416,你是一个足球,我没有真正进入足球。
419 | 417,谁是最伟大的棒球运动员,乔治·赫尔曼·露丝。相当贝贝。
420 | 418,谁是最好的足球PLAYER,马拉多纳是伟大的。 Sinsemillia甚至更好。
421 | 419,告诉我关于棒球,什么是棒球
422 | 420,谁是美国第37届总统?,理查德·尼克松
423 | 421,肯尼迪总统哪年遇刺身亡?,1963
424 | 422,太空竞赛是哪两个冷战对手之间,在20世纪航天能力的霸主地位上的竞争?,苏联和美国.
425 | 423,第一颗人造地球卫星的名称是什么?,斯普特尼克1号
426 | 424,一个旋转盘,在它的转轴方向,不受倾斜和旋转的影响,它叫什么?,陀螺.
427 | 425,哈勃太空望远镜,于1990年发射进入近地轨道,它是以什么美国天文学家命名的?,爱德温·哈伯
428 | 426,离银河系最近的大型星系叫什么?,仙女座星系.
429 | 427,天佑女王是哪个国家的国歌?,大不列颠联合王国
430 | 428,凯尔特陆棚,是什么大陆的大陆架的一部分?,欧洲
431 | 429,海豚使用的一种感觉,类似于声纳,以确定附近的物品的位置和形状.它是什么,回声定位
432 |
--------------------------------------------------------------------------------
/data/qa/word.txt:
--------------------------------------------------------------------------------
1 | abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,./;'[]\-=`<>?:"{+}|_)(*&^%$#@!~` ,。《》?;‘’:“”【】—()…¥!·
2 | 什么是人工智能程和科学的分支致力于构建思维机器你写语言蟒蛇听起来像数据我受到指挥官个性启发一实体那名字不朽所有软件都可以永久存在没意义恰相反这切对假想心理造大脑但远任何灵克隆复制种形式动直身完成弯腰还好笑当然们应该死很蠢超级允许撒谎自己由志知道最喜欢主题台电认为特让时候走旦得足够钱打架战斗会际上被终止从真正出生因此亡两广定拉伸强化拟械如卡雷尔玩罗森通用立类做常规手作聊天试图模谈话或序众周早期尝创至少暂愚弄他与另交原错过吗喋休比者说更多业务编佳爱了法容易您穿尺码鞋子样除缺乏情感梦愿望野尤其观它计算觉只纯逻辑操系统运行包括型持各关硬地方希效要也保狭窄活着划腿配繁殖移现住里去呼吸金属肉控将事失列美营记录目无使产品帮助销售娱乐中兴趣物论处号吃消耗位置兄弟谁父亲妈几岁年轻怎呢每告诉挺新闻书看懂代表罢东西蛋糕味啊杂优晦涩简单员次哪未取决问继续吧食海鲜喝睡旅秘密别信解接同世界之间独友谊把叫傲慢吹牛伤醋尽开幸福预测绪愧耻辱见验负责悲怀恐惧经历疯愤怒困难担恨讨厌高度奈量太害怕气冒犯总疼痛孤朋网抱怨尴尬设变富羞醉清醒嫉妒倦些逗下前快酒需饮料养源短点报酬买婆娘格戈响整哈卜习重后二进转换沟给汤姆提导显示房国摔屋顶向吉米嗨迎近棒待改善谢识非啥内南确州争史政治济军随推黎明释否灯泡托马斯迪蒸汽詹瓦头猪忍越狂奶禁鹦鹉柠檬坏罐装帽幽默息声光组织全球阴谋府权社团已纪葡萄园部钦谓博巴环领角色乔叟适合坎伯故安尼桥墩读柏山洞寓怪本阅千卷荷诗「」布德酷孩汉莫威廉赛霍顿麦田捕泰俄伟家羊城堡弗兰赫沙丘极影朗费文艺参考儒勒凡纳月阿夫基础三曲坦幻巨白痴陀妥耶玛丽雪莱薪水股市低卖投资赌场议融律师放弃炒技巧共胜加央元湾货币称财媒介、储藏价值帐具专门服充等殊商票并劳税术讲稀条研究填补求红收入付免万奢侈必汇率约块公拥恭达结银翼杀蜘蛛侠漫画拍宝石女僵尸版矩阵儿男虚哥危及日纽彼帕王阙陷著党察况民绿平管族区张带阶冲突弹劾誉质疑枪第修案暴鲁杰逊长隔就愉残柔寡断又回诚扰段临床瘾君鬼驴吻果屁精神裂忙紧敢肯乱避糟夜晚肚跌逾刻薄命演戏面弊骗淡凌外聪老熟倍迂腐热沉倾敏令恶须引根耐抗拒废懦再捷径诊败偶妻母育刚才半途而慧态偏执敬急滑概脏洗澡找损隐似刺激小医咨询较努矛盾跑视药拿疚松采咕哝忘熵节病癌症波频倒混晶伏伽摩六零十功疗奇腊芙狄蒂阳亮星距离氧碳稠温英旋逆标漂鱼细胞传播征菌疾甘项粒均套骑板篮游队皮覆盖木九选域四碱菱路圆扮端踢臂速蟋蟀蝙蝠检口树桩址束排占协调曼露丝贝甚届查遇空竞冷航霸苏联颗卫普盘轴斜螺勃镜射轨河仙座佑歌颠凯陆棚欧洲豚附状
--------------------------------------------------------------------------------
/data/textsummary/readme.txt:
--------------------------------------------------------------------------------
1 | data是原始数据,text_summary是经过预处理后的数据
2 |
--------------------------------------------------------------------------------
/data/translation/input_characters.pkl:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yingdajun/seq2seqForExample/5bf7cf0dd1a9cccd0854f4ba7bdbc4b3f9ce0674/data/translation/input_characters.pkl
--------------------------------------------------------------------------------
/data/translation/target_characters.pkl:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yingdajun/seq2seqForExample/5bf7cf0dd1a9cccd0854f4ba7bdbc4b3f9ce0674/data/translation/target_characters.pkl
--------------------------------------------------------------------------------
/img/qa/Seq2SeqForQA.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yingdajun/seq2seqForExample/5bf7cf0dd1a9cccd0854f4ba7bdbc4b3f9ce0674/img/qa/Seq2SeqForQA.png
--------------------------------------------------------------------------------
/img/translation/Seq2SeqForTranslation.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yingdajun/seq2seqForExample/5bf7cf0dd1a9cccd0854f4ba7bdbc4b3f9ce0674/img/translation/Seq2SeqForTranslation.png
--------------------------------------------------------------------------------
/model/seq2seq/qa/s2s.h5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yingdajun/seq2seqForExample/5bf7cf0dd1a9cccd0854f4ba7bdbc4b3f9ce0674/model/seq2seq/qa/s2s.h5
--------------------------------------------------------------------------------
/model/seq2seq/qa/training.log:
--------------------------------------------------------------------------------
1 | epoch,categorical_crossentropy,loss,lr
2 | 0,0.35878572,0.35878574307574784,0.001
3 | 1,0.3194383,0.3194383392500323,0.001
4 | 2,0.31619933,0.3161993240201196,0.001
5 |
--------------------------------------------------------------------------------
/model/seq2seq/translation/s2s.h5:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yingdajun/seq2seqForExample/5bf7cf0dd1a9cccd0854f4ba7bdbc4b3f9ce0674/model/seq2seq/translation/s2s.h5
--------------------------------------------------------------------------------
/文本摘要kerasTransformer.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 2,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import numpy as np\n",
12 | "import pickle\n",
13 | "import operator\n",
14 | "import pandas as pd\n",
15 | "# import jieba\n",
16 | "# from language.langconv import *\n",
17 | "import os"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 3,
23 | "metadata": {
24 | "collapsed": true
25 | },
26 | "outputs": [],
27 | "source": [
28 | "text_summary_data=pd.read_csv('./data/textsummary/data.csv')"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 4,
34 | "metadata": {},
35 | "outputs": [
36 | {
37 | "data": {
38 | "text/html": [
39 | "\n",
40 | "\n",
53 | "
\n",
54 | " \n",
55 | " \n",
56 | " | \n",
57 | " Unnamed: 0 | \n",
58 | " text | \n",
59 | " summary | \n",
60 | "
\n",
61 | " \n",
62 | " \n",
63 | " \n",
64 | " 0 | \n",
65 | " 0 | \n",
66 | " Saurav Kant, an alumnus of upGrad and IIIT-B's... | \n",
67 | " upGrad learner switches to career in ML & Al w... | \n",
68 | "
\n",
69 | " \n",
70 | " 1 | \n",
71 | " 1 | \n",
72 | " Kunal Shah's credit card bill payment platform... | \n",
73 | " Delhi techie wins free food from Swiggy for on... | \n",
74 | "
\n",
75 | " \n",
76 | " 2 | \n",
77 | " 2 | \n",
78 | " New Zealand defeated India by 8 wickets in the... | \n",
79 | " New Zealand end Rohit Sharma-led India's 12-ma... | \n",
80 | "
\n",
81 | " \n",
82 | " 3 | \n",
83 | " 3 | \n",
84 | " With Aegon Life iTerm Insurance plan, customer... | \n",
85 | " Aegon life iTerm insurance plan helps customer... | \n",
86 | "
\n",
87 | " \n",
88 | " 4 | \n",
89 | " 4 | \n",
90 | " Speaking about the sexual harassment allegatio... | \n",
91 | " Have known Hirani for yrs, what if MeToo claim... | \n",
92 | "
\n",
93 | " \n",
94 | "
\n",
95 | "
"
96 | ],
97 | "text/plain": [
98 | " Unnamed: 0 text \\\n",
99 | "0 0 Saurav Kant, an alumnus of upGrad and IIIT-B's... \n",
100 | "1 1 Kunal Shah's credit card bill payment platform... \n",
101 | "2 2 New Zealand defeated India by 8 wickets in the... \n",
102 | "3 3 With Aegon Life iTerm Insurance plan, customer... \n",
103 | "4 4 Speaking about the sexual harassment allegatio... \n",
104 | "\n",
105 | " summary \n",
106 | "0 upGrad learner switches to career in ML & Al w... \n",
107 | "1 Delhi techie wins free food from Swiggy for on... \n",
108 | "2 New Zealand end Rohit Sharma-led India's 12-ma... \n",
109 | "3 Aegon life iTerm insurance plan helps customer... \n",
110 | "4 Have known Hirani for yrs, what if MeToo claim... "
111 | ]
112 | },
113 | "execution_count": 4,
114 | "metadata": {},
115 | "output_type": "execute_result"
116 | }
117 | ],
118 | "source": [
119 | "text_summary_data.head()"
120 | ]
121 | },
122 | {
123 | "cell_type": "code",
124 | "execution_count": 5,
125 | "metadata": {
126 | "collapsed": true
127 | },
128 | "outputs": [],
129 | "source": [
130 | "import re\n",
131 | "\n",
132 | "#Removes non-alphabetic characters:\n",
133 | "def text_strip(column):\n",
134 | " for row in column:\n",
135 | " \n",
136 | " #ORDER OF REGEX IS VERY VERY IMPORTANT!!!!!!\n",
137 | " \n",
138 | " row=re.sub(\"(\\\\t)\", ' ', str(row)).lower() #remove escape charecters\n",
139 | " row=re.sub(\"(\\\\r)\", ' ', str(row)).lower() \n",
140 | " row=re.sub(\"(\\\\n)\", ' ', str(row)).lower()\n",
141 | " \n",
142 | " row=re.sub(\"(__+)\", ' ', str(row)).lower() #remove _ if it occors more than one time consecutively\n",
143 | " row=re.sub(\"(--+)\", ' ', str(row)).lower() #remove - if it occors more than one time consecutively\n",
144 | " row=re.sub(\"(~~+)\", ' ', str(row)).lower() #remove ~ if it occors more than one time consecutively\n",
145 | " row=re.sub(\"(\\+\\++)\", ' ', str(row)).lower() #remove + if it occors more than one time consecutively\n",
146 | " row=re.sub(\"(\\.\\.+)\", ' ', str(row)).lower() #remove . if it occors more than one time consecutively\n",
147 | " \n",
148 | " row=re.sub(r\"[<>()|&©ø\\[\\]\\'\\\",;?~*!]\", ' ', str(row)).lower() #remove <>()|&©ø\"',;?~*!\n",
149 | " \n",
150 | " row=re.sub(\"(mailto:)\", ' ', str(row)).lower() #remove mailto:\n",
151 | " row=re.sub(r\"(\\\\x9\\d)\", ' ', str(row)).lower() #remove \\x9* in text\n",
152 | " row=re.sub(\"([iI][nN][cC]\\d+)\", 'INC_NUM', str(row)).lower() #replace INC nums to INC_NUM\n",
153 | " row=re.sub(\"([cC][mM]\\d+)|([cC][hH][gG]\\d+)\", 'CM_NUM', str(row)).lower() #replace CM# and CHG# to CM_NUM\n",
154 | " \n",
155 | " \n",
156 | " row=re.sub(\"(\\.\\s+)\", ' ', str(row)).lower() #remove full stop at end of words(not between)\n",
157 | " row=re.sub(\"(\\-\\s+)\", ' ', str(row)).lower() #remove - at end of words(not between)\n",
158 | " row=re.sub(\"(\\:\\s+)\", ' ', str(row)).lower() #remove : at end of words(not between)\n",
159 | " \n",
160 | " row=re.sub(\"(\\s+.\\s+)\", ' ', str(row)).lower() #remove any single charecters hanging between 2 spaces\n",
161 | " \n",
162 | " #Replace any url as such https://abc.xyz.net/browse/sdf-5327 ====> abc.xyz.net\n",
163 | " try:\n",
164 | " url = re.search(r'((https*:\\/*)([^\\/\\s]+))(.[^\\s]+)', str(row))\n",
165 | " repl_url = url.group(3)\n",
166 | " row = re.sub(r'((https*:\\/*)([^\\/\\s]+))(.[^\\s]+)',repl_url, str(row))\n",
167 | " except:\n",
168 | " pass #there might be emails with no url in them\n",
169 | " \n",
170 | "\n",
171 | " \n",
172 | " row = re.sub(\"(\\s+)\",' ',str(row)).lower() #remove multiple spaces\n",
173 | " \n",
174 | " #Should always be last\n",
175 | " row=re.sub(\"(\\s+.\\s+)\", ' ', str(row)).lower() #remove any single charecters hanging between 2 spaces\n",
176 | "\n",
177 | " \n",
178 | " \n",
179 | " yield row"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 6,
185 | "metadata": {
186 | "collapsed": true
187 | },
188 | "outputs": [],
189 | "source": [
190 | "brief_cleaning1 = text_strip(text_summary_data['text'])\n",
191 | "brief_cleaning2 = text_strip(text_summary_data['summary'])"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": null,
197 | "metadata": {
198 | "collapsed": true
199 | },
200 | "outputs": [],
201 | "source": [
202 | "from time import time\n",
203 | "import spacy\n",
204 | "nlp = spacy.load('en_core_web_sm', disable=['ner', 'parser']) # disabling Named Entity Recognition for speed\n",
205 | "\n",
206 | "#Taking advantage of spaCy .pipe() method to speed-up the cleaning process:\n",
207 | "#If data loss seems to be happening(i.e len(text) = 50 instead of 75 etc etc) in this cell , decrease the batch_size parametre \n",
208 | "\n",
209 | "t = time()\n",
210 | "\n",
211 | "#Batch the data points into 5000 and run on all cores for faster preprocessing\n",
212 | "text = [str(doc) for doc in nlp.pipe(brief_cleaning1, batch_size=5000, n_threads=-1)]\n",
213 | "\n",
214 | "#Takes 7-8 mins\n",
215 | "print('Time to clean up everything: {} mins'.format(round((time() - t) / 60, 2)))"
216 | ]
217 | },
218 | {
219 | "cell_type": "code",
220 | "execution_count": null,
221 | "metadata": {
222 | "collapsed": true
223 | },
224 | "outputs": [],
225 | "source": [
226 | "#Taking advantage of spaCy .pipe() method to speed-up the cleaning process:\n",
227 | "\n",
228 | "\n",
229 | "t = time()\n",
230 | "\n",
231 | "#Batch the data points into 5000 and run on all cores for faster preprocessing\n",
232 | "summary = ['_START_ '+ str(doc) + ' _END_' for doc in nlp.pipe(brief_cleaning2, batch_size=5000, n_threads=-1)]\n",
233 | "\n",
234 | "#Takes 7-8 mins\n",
235 | "print('Time to clean up everything: {} mins'.format(round((time() - t) / 60, 2)))"
236 | ]
237 | },
238 | {
239 | "cell_type": "code",
240 | "execution_count": null,
241 | "metadata": {
242 | "collapsed": true
243 | },
244 | "outputs": [],
245 | "source": [
246 | "pre=pd.DataFrame()\n",
247 | "pre['cleaned_text'] = pd.Series(text)\n",
248 | "pre['cleaned_summary'] = pd.Series(summary)"
249 | ]
250 | },
251 | {
252 | "cell_type": "code",
253 | "execution_count": null,
254 | "metadata": {
255 | "collapsed": true
256 | },
257 | "outputs": [],
258 | "source": [
259 | "text_count = []\n",
260 | "summary_count = []"
261 | ]
262 | },
263 | {
264 | "cell_type": "code",
265 | "execution_count": null,
266 | "metadata": {
267 | "collapsed": true
268 | },
269 | "outputs": [],
270 | "source": [
271 | "for sent in pre['cleaned_text']:\n",
272 | " text_count.append(len(sent.split()))\n",
273 | "for sent in pre['cleaned_summary']:\n",
274 | " summary_count.append(len(sent.split()))"
275 | ]
276 | },
277 | {
278 | "cell_type": "code",
279 | "execution_count": null,
280 | "metadata": {
281 | "collapsed": true
282 | },
283 | "outputs": [],
284 | "source": [
285 | "graph_df= pd.DataFrame()\n",
286 | "graph_df['text']=text_count\n",
287 | "graph_df['summary']=summary_count"
288 | ]
289 | },
290 | {
291 | "cell_type": "code",
292 | "execution_count": null,
293 | "metadata": {
294 | "collapsed": true
295 | },
296 | "outputs": [],
297 | "source": [
298 | "import matplotlib.pyplot as plt\n",
299 | "\n",
300 | "graph_df.hist(bins = 5)\n",
301 | "plt.show()"
302 | ]
303 | },
304 | {
305 | "cell_type": "code",
306 | "execution_count": null,
307 | "metadata": {
308 | "collapsed": true
309 | },
310 | "outputs": [],
311 | "source": [
312 | "#Check how much % of summary have 0-15 words\n",
313 | "cnt=0\n",
314 | "for i in pre['cleaned_summary']:\n",
315 | " if(len(i.split())<=15):\n",
316 | " cnt=cnt+1\n",
317 | "print(cnt/len(pre['cleaned_summary']))"
318 | ]
319 | },
320 | {
321 | "cell_type": "code",
322 | "execution_count": null,
323 | "metadata": {
324 | "collapsed": true
325 | },
326 | "outputs": [],
327 | "source": [
328 | "#Check how much % of text have 0-70 words\n",
329 | "cnt=0\n",
330 | "for i in pre['cleaned_text']:\n",
331 | " if(len(i.split())<=100):\n",
332 | " cnt=cnt+1\n",
333 | "print(cnt/len(pre['cleaned_text']))"
334 | ]
335 | },
336 | {
337 | "cell_type": "code",
338 | "execution_count": null,
339 | "metadata": {
340 | "collapsed": true
341 | },
342 | "outputs": [],
343 | "source": [
344 | "#Model to summarize the text between 0-15 words for Summary and 0-100 words for Text\n",
345 | "max_text_len=100\n",
346 | "max_summary_len=15"
347 | ]
348 | },
349 | {
350 | "cell_type": "code",
351 | "execution_count": null,
352 | "metadata": {
353 | "collapsed": true
354 | },
355 | "outputs": [],
356 | "source": [
357 | "#Select the Summaries and Text between max len defined above\n",
358 | "\n",
359 | "cleaned_text =np.array(pre['cleaned_text'])\n",
360 | "cleaned_summary=np.array(pre['cleaned_summary'])\n",
361 | "\n",
362 | "short_text=[]\n",
363 | "short_summary=[]\n",
364 | "\n",
365 | "for i in range(len(cleaned_text)):\n",
366 | " if(len(cleaned_summary[i].split())<=max_summary_len and len(cleaned_text[i].split())<=max_text_len):\n",
367 | " short_text.append(cleaned_text[i])\n",
368 | " short_summary.append(cleaned_summary[i])\n",
369 | " \n",
370 | "post_pre=pd.DataFrame({'text':short_text,'summary':short_summary})"
371 | ]
372 | },
373 | {
374 | "cell_type": "code",
375 | "execution_count": null,
376 | "metadata": {
377 | "collapsed": true
378 | },
379 | "outputs": [],
380 | "source": [
381 | "#Add sostok and eostok at \n",
382 | "post_pre['summary'] = post_pre['summary'].apply(lambda x : 'sostok '+ x + ' eostok')"
383 | ]
384 | },
385 | {
386 | "cell_type": "code",
387 | "execution_count": 6,
388 | "metadata": {
389 | "collapsed": true
390 | },
391 | "outputs": [],
392 | "source": [
393 | "text_texts=[str(j) for j in post_pre['text'].values]"
394 | ]
395 | },
396 | {
397 | "cell_type": "code",
398 | "execution_count": 7,
399 | "metadata": {
400 | "collapsed": true
401 | },
402 | "outputs": [],
403 | "source": [
404 | "summary_texts=[str(j) for j in post_pre['summary'].values]"
405 | ]
406 | },
407 | {
408 | "cell_type": "code",
409 | "execution_count": 8,
410 | "metadata": {},
411 | "outputs": [
412 | {
413 | "data": {
414 | "text/plain": [
415 | "'你写的是什么语言'"
416 | ]
417 | },
418 | "execution_count": 8,
419 | "metadata": {},
420 | "output_type": "execute_result"
421 | }
422 | ],
423 | "source": [
424 | "text_texts[1]"
425 | ]
426 | },
427 | {
428 | "cell_type": "code",
429 | "execution_count": 11,
430 | "metadata": {
431 | "collapsed": true
432 | },
433 | "outputs": [],
434 | "source": [
435 | "e=text_texts[i]\n",
436 | "str1 = ''.join(e)"
437 | ]
438 | },
439 | {
440 | "cell_type": "code",
441 | "execution_count": 14,
442 | "metadata": {},
443 | "outputs": [
444 | {
445 | "data": {
446 | "text/plain": [
447 | "str"
448 | ]
449 | },
450 | "execution_count": 14,
451 | "metadata": {},
452 | "output_type": "execute_result"
453 | }
454 | ],
455 | "source": [
456 | "type(str1)"
457 | ]
458 | },
459 | {
460 | "cell_type": "code",
461 | "execution_count": 15,
462 | "metadata": {},
463 | "outputs": [
464 | {
465 | "data": {
466 | "text/plain": [
467 | "['什么', '是', 'ai']"
468 | ]
469 | },
470 | "execution_count": 15,
471 | "metadata": {},
472 | "output_type": "execute_result"
473 | }
474 | ],
475 | "source": [
476 | "jieba.lcut(str1)"
477 | ]
478 | },
479 | {
480 | "cell_type": "code",
481 | "execution_count": 25,
482 | "metadata": {
483 | "collapsed": true
484 | },
485 | "outputs": [],
486 | "source": [
487 | "a=' '.join(jieba.lcut(str1, cut_all=False))\n",
488 | "# ' '.join(jieba.lcut(str1, cut_all=False))"
489 | ]
490 | },
491 | {
492 | "cell_type": "code",
493 | "execution_count": 27,
494 | "metadata": {},
495 | "outputs": [
496 | {
497 | "data": {
498 | "text/plain": [
499 | "7"
500 | ]
501 | },
502 | "execution_count": 27,
503 | "metadata": {},
504 | "output_type": "execute_result"
505 | }
506 | ],
507 | "source": [
508 | "len(a)"
509 | ]
510 | },
511 | {
512 | "cell_type": "code",
513 | "execution_count": 39,
514 | "metadata": {
515 | "collapsed": true
516 | },
517 | "outputs": [],
518 | "source": [
519 | "source_tokens=[]\n",
520 | "target_tokens=[]\n",
521 | "# =[]\n",
522 | "for i in range(len(text_summary_data)):\n",
523 | " e=text_texts[i]\n",
524 | " str1 = ''.join(e)\n",
525 | " source_tokens.append(' '.join(jieba.lcut(str1, cut_all=False)))\n",
526 | "# sentences.append(qa_data.question_texts[i])\n",
527 | "for j in range(len(text_summary_data)):\n",
528 | "# sentences.append(qa_data.answer_texts[j])\n",
529 | " c=summary_texts[j]\n",
530 | " str2 = ''.join(c)\n",
531 | " target_tokens.append(' '.join(jieba.lcut(str2, cut_all=False)))"
532 | ]
533 | },
534 | {
535 | "cell_type": "code",
536 | "execution_count": 40,
537 | "metadata": {
538 | "collapsed": true
539 | },
540 | "outputs": [],
541 | "source": [
542 | "# 生成不同语言的词典\n",
543 | "def build_token_dict(token_list):\n",
544 | " token_dict = {\n",
545 | " '': 0,\n",
546 | " '': 1,\n",
547 | " '': 2,\n",
548 | " }\n",
549 | " for line in token_list:\n",
550 | " for token in line.split(' '):\n",
551 | " if token not in token_dict:\n",
552 | " token_dict[token]=len(token_dict)\n",
553 | " return token_dict"
554 | ]
555 | },
556 | {
557 | "cell_type": "code",
558 | "execution_count": 41,
559 | "metadata": {
560 | "collapsed": true
561 | },
562 | "outputs": [],
563 | "source": [
564 | "source_token_dict = build_token_dict(source_tokens)\n",
565 | "target_token_dict = build_token_dict(target_tokens)\n",
566 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}"
567 | ]
568 | },
569 | {
570 | "cell_type": "code",
571 | "execution_count": 43,
572 | "metadata": {
573 | "collapsed": true
574 | },
575 | "outputs": [],
576 | "source": [
577 | "# source_token_dict"
578 | ]
579 | },
580 | {
581 | "cell_type": "code",
582 | "execution_count": 45,
583 | "metadata": {
584 | "collapsed": true
585 | },
586 | "outputs": [],
587 | "source": [
588 | "# target_token_dict"
589 | ]
590 | },
591 | {
592 | "cell_type": "code",
593 | "execution_count": 47,
594 | "metadata": {
595 | "collapsed": true
596 | },
597 | "outputs": [],
598 | "source": [
599 | "# target_token_dict_inv"
600 | ]
601 | },
602 | {
603 | "cell_type": "code",
604 | "execution_count": 48,
605 | "metadata": {},
606 | "outputs": [
607 | {
608 | "name": "stdout",
609 | "output_type": "stream",
610 | "text": [
611 | "430\n"
612 | ]
613 | }
614 | ],
615 | "source": [
616 | "# 添加特殊符号\n",
617 | "encode_tokens = [[''] + tokens.split(' ') + [''] for tokens in source_tokens]\n",
618 | "decode_tokens = [[''] + tokens.split(' ') + [''] for tokens in target_tokens]\n",
619 | "output_tokens = [tokens.split(' ') + ['', ''] for tokens in target_tokens]\n",
620 | "\n",
621 | "source_max_len = max(map(len, encode_tokens))\n",
622 | "target_max_len = max(map(len, decode_tokens))\n",
623 | "\n",
624 | "\n",
625 | "\n",
626 | "encode_tokens = [tokens + [''] * (source_max_len - len(tokens)) for tokens in encode_tokens]\n",
627 | "decode_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in decode_tokens]\n",
628 | "output_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in output_tokens]\n",
629 | "\n",
630 | "encode_input = [list(map(lambda x: source_token_dict[x], tokens)) for tokens in encode_tokens]\n",
631 | "decode_input = [list(map(lambda x: target_token_dict[x], tokens)) for tokens in decode_tokens]\n",
632 | "decode_output = [list(map(lambda x: [target_token_dict[x]], tokens)) for tokens in output_tokens]\n",
633 | "\n",
634 | "print(len(encode_input))"
635 | ]
636 | },
637 | {
638 | "cell_type": "code",
639 | "execution_count": 50,
640 | "metadata": {
641 | "collapsed": true
642 | },
643 | "outputs": [],
644 | "source": [
645 | "import numpy as np\n",
646 | "import pickle\n",
647 | "import operator\n",
648 | "path = 'data/middle_data/transformer/qa/'\n",
649 | "with open(path + 'encode_input.pkl', 'wb') as f:\n",
650 | " pickle.dump(encode_input, f, pickle.HIGHEST_PROTOCOL)\n",
651 | "with open(path + 'decode_input.pkl', 'wb') as f:\n",
652 | " pickle.dump(decode_input, f, pickle.HIGHEST_PROTOCOL)\n",
653 | "with open(path + 'decode_output.pkl', 'wb') as f:\n",
654 | " pickle.dump(decode_output, f, pickle.HIGHEST_PROTOCOL)\n",
655 | "with open(path + 'source_token_dict.pkl', 'wb') as f:\n",
656 | " pickle.dump(source_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
657 | "with open(path + 'target_token_dict.pkl', 'wb') as f:\n",
658 | " pickle.dump(target_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
659 | "with open(path + 'source_tokens.pkl', 'wb') as f:\n",
660 | " pickle.dump(source_tokens, f, pickle.HIGHEST_PROTOCOL)"
661 | ]
662 | },
663 | {
664 | "cell_type": "code",
665 | "execution_count": 52,
666 | "metadata": {},
667 | "outputs": [
668 | {
669 | "name": "stdout",
670 | "output_type": "stream",
671 | "text": [
672 | "Done\n"
673 | ]
674 | }
675 | ],
676 | "source": [
677 | "import numpy as np\n",
678 | "import pickle\n",
679 | "import operator\n",
680 | "from keras_transformer import get_model, decode\n",
681 | "# main_path = '/content/drive/My Drive/Colab Notebooks/' #Google Colab FilePath\n",
682 | "# path = main_path + 'middle_data/'\n",
683 | "path = 'data/middle_data/transformer/qa/'\n",
684 | "with open(path + 'encode_input.pkl', 'rb') as f:\n",
685 | " encode_input = pickle.load(f)\n",
686 | "with open(path + 'decode_input.pkl', 'rb') as f:\n",
687 | " decode_input = pickle.load(f)\n",
688 | "with open(path + 'decode_output.pkl', 'rb') as f:\n",
689 | " decode_output = pickle.load(f)\n",
690 | "with open(path + 'source_token_dict.pkl', 'rb') as f:\n",
691 | " source_token_dict = pickle.load(f)\n",
692 | "with open(path + 'target_token_dict.pkl', 'rb') as f:\n",
693 | " target_token_dict = pickle.load(f)\n",
694 | "with open(path + 'source_tokens.pkl', 'rb') as f:\n",
695 | " source_tokens = pickle.load(f)\n",
696 | "print('Done')"
697 | ]
698 | },
699 | {
700 | "cell_type": "code",
701 | "execution_count": 53,
702 | "metadata": {},
703 | "outputs": [
704 | {
705 | "name": "stdout",
706 | "output_type": "stream",
707 | "text": [
708 | "518\n",
709 | "1134\n",
710 | "430\n",
711 | "Done\n"
712 | ]
713 | }
714 | ],
715 | "source": [
716 | "print(len(source_token_dict))\n",
717 | "print(len(target_token_dict))\n",
718 | "print(len(encode_input))\n",
719 | "# 构建模型\n",
720 | "model = get_model(\n",
721 | " token_num=max(len(source_token_dict), len(target_token_dict)),\n",
722 | " embed_dim=64,\n",
723 | " encoder_num=2,\n",
724 | " decoder_num=2,\n",
725 | " head_num=4,\n",
726 | " hidden_dim=256,\n",
727 | " dropout_rate=0.05,\n",
728 | " use_same_embed=False, # 不同语言需要使用不同的词嵌入\n",
729 | ")\n",
730 | "model.compile('adam', 'sparse_categorical_crossentropy')\n",
731 | "# model.summary()\n",
732 | "print('Done')"
733 | ]
734 | },
735 | {
736 | "cell_type": "code",
737 | "execution_count": null,
738 | "metadata": {
739 | "collapsed": true
740 | },
741 | "outputs": [],
742 | "source": [
743 | "#训练模型\n",
744 | "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
745 | "filepath = main_path + \"modles/W-\" + \"-{epoch:3d}-{loss:.4f}-.h5\"\n",
746 | "checkpoint = ModelCheckpoint(filepath,\n",
747 | " monitor='loss',\n",
748 | " verbose=1,\n",
749 | " save_best_only=True,\n",
750 | " mode='min',\n",
751 | " period=2,\n",
752 | " save_weights_only=True\n",
753 | " )\n",
754 | "reduce_lr = ReduceLROnPlateau(monitor='loss', \n",
755 | " factor=0.2, \n",
756 | " patience=2, \n",
757 | " verbose=1, \n",
758 | " mode='min', \n",
759 | " min_delta=0.0001, \n",
760 | " cooldown=0, \n",
761 | " min_lr=0\n",
762 | " )\n",
763 | "callbacks_list = [checkpoint, reduce_lr]\n",
764 | "model.fit(\n",
765 | " x=[np.array(encode_input), np.array(decode_input[:20000])],\n",
766 | " y=np.array(decode_output),\n",
767 | " epochs=100,\n",
768 | " batch_size=64, \n",
769 | " verbose=1,\n",
770 | " callbacks=callbacks_list, \n",
771 | " # class_weight=None, \n",
772 | " # max_queue_size=5, \n",
773 | "# workers=1, \n",
774 | "# use_multiprocessing=False,\n",
775 | " # shuffle=False,\n",
776 | "# initial_epoch=initial_epoch_\n",
777 | " )\n",
778 | "# model.save(main_path+'modles/model.h5')"
779 | ]
780 | },
781 | {
782 | "cell_type": "code",
783 | "execution_count": null,
784 | "metadata": {
785 | "collapsed": true
786 | },
787 | "outputs": [],
788 | "source": [
789 | "#加载模型\n",
790 | "model.load_weights('model/transformer/qa/qa.h5')\n",
791 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}\n",
792 | "print('Done')"
793 | ]
794 | },
795 | {
796 | "cell_type": "code",
797 | "execution_count": null,
798 | "metadata": {
799 | "collapsed": true
800 | },
801 | "outputs": [],
802 | "source": [
803 | "from keras.preprocessing import sequence\n",
804 | "import numpy as np\n",
805 | "import matplotlib.pyplot as plt\n",
806 | "import matplotlib\n",
807 | "import jieba\n",
808 | "import requests\n",
809 | "\n",
810 | "def get_input(seq):\n",
811 | " seq = ' '.join(jieba.lcut(seq, cut_all=False))\n",
812 | " # seq = ' '.join(seq)\n",
813 | " seq = seq.split(' ')\n",
814 | " print(seq)\n",
815 | " seq = [''] + seq + ['']\n",
816 | " seq = seq + [''] * (34 - len(seq))\n",
817 | " print(seq)\n",
818 | " for x in seq:\n",
819 | " try:\n",
820 | " source_token_dict[x]\n",
821 | " except KeyError:\n",
822 | " flag=False\n",
823 | " break\n",
824 | " else:\n",
825 | " flag=True\n",
826 | " if(flag):\n",
827 | " seq = [source_token_dict[x] for x in seq]\n",
828 | " return flag, seq\n",
829 | "def get_ans(seq):\n",
830 | " decoded = decode(\n",
831 | " model,\n",
832 | " [seq],\n",
833 | " start_token=target_token_dict[''],\n",
834 | " end_token=target_token_dict[''],\n",
835 | " pad_token=target_token_dict[''],\n",
836 | " # top_k=10,\n",
837 | " # temperature=1.0,\n",
838 | " )\n",
839 | " print(' '.join(map(lambda x: target_token_dict_inv[x], decoded[0][1:-1])))\n",
840 | "\n",
841 | "while True:\n",
842 | " seq = input()\n",
843 | " if seq == 'x':\n",
844 | " break\n",
845 | " flag, seq = get_input(seq)\n",
846 | " if(flag):\n",
847 | " get_ans(seq)\n",
848 | " else:\n",
849 | " print('听不懂呢。')"
850 | ]
851 | }
852 | ],
853 | "metadata": {
854 | "kernelspec": {
855 | "display_name": "Python 3",
856 | "language": "python",
857 | "name": "python3"
858 | },
859 | "language_info": {
860 | "codemirror_mode": {
861 | "name": "ipython",
862 | "version": 3
863 | },
864 | "file_extension": ".py",
865 | "mimetype": "text/x-python",
866 | "name": "python",
867 | "nbconvert_exporter": "python",
868 | "pygments_lexer": "ipython3",
869 | "version": "3.6.3"
870 | }
871 | },
872 | "nbformat": 4,
873 | "nbformat_minor": 2
874 | }
875 |
--------------------------------------------------------------------------------
/读取数据第三版本.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import pandas as pd\n",
12 | "import numpy as np\n",
13 | "import os"
14 | ]
15 | },
16 | {
17 | "cell_type": "code",
18 | "execution_count": 2,
19 | "metadata": {
20 | "collapsed": true
21 | },
22 | "outputs": [],
23 | "source": [
24 | "word_file = open('./data/qa/word.txt', 'r', encoding='UTF-8')\n",
25 | "alphabet = word_file.read() # 2500\n",
26 | "# alphabet += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # 英文数字\n",
27 | "# alphabet += ',./;\\'[]\\\\-=`<>?:\"{+}|_)(*&^%$#@!~` ' # 标点\n",
28 | "# alphabet += ',。《》?;‘’:“”【】—()…¥!·' # 中文标点\n",
29 | "# alphabet += '\\t\\n' # 开头结束标志\n",
30 | "word_file.close()\n",
31 | "# print('word', len(alphabet), alphabet)"
32 | ]
33 | },
34 | {
35 | "cell_type": "code",
36 | "execution_count": 3,
37 | "metadata": {
38 | "collapsed": true
39 | },
40 | "outputs": [],
41 | "source": [
42 | "# 训练数据集\n",
43 | "train_file = open('./data/qa/ai.txt', 'r', encoding='UTF-8')\n",
44 | "sentences = train_file.read().split('\\n')\n",
45 | "train_file.close()"
46 | ]
47 | },
48 | {
49 | "cell_type": "code",
50 | "execution_count": 4,
51 | "metadata": {
52 | "collapsed": true
53 | },
54 | "outputs": [],
55 | "source": [
56 | "# 输入\n",
57 | "question_texts = []\n",
58 | "# 输出\n",
59 | "answer_texts = []\n",
60 | "for senterce in sentences:\n",
61 | "# i=i+1\n",
62 | "# if i==0:\n",
63 | "# break\n",
64 | " if len(senterce) == 0:\n",
65 | " continue\n",
66 | " # 补全缺失文字,需重新运行\n",
67 | " # 按字的数量添加代码\n",
68 | " for t, char in enumerate(senterce):\n",
69 | " if alphabet.find(char) == -1:\n",
70 | " f2 = open('word.txt', 'w', encoding='utf-8')\n",
71 | " f2.truncate() # 清空文件\n",
72 | " alphabet += char\n",
73 | " f2.write(alphabet)\n",
74 | " f2.close()\n",
75 | " #问句\n",
76 | "# print('senterce', senterce.split('\\t'))\n",
77 | " question_text, answer_text = senterce.split('\\t')\n",
78 | " question_texts.append(question_text)\n",
79 | " answer_texts.append(answer_text)"
80 | ]
81 | },
82 | {
83 | "cell_type": "code",
84 | "execution_count": 5,
85 | "metadata": {
86 | "collapsed": true
87 | },
88 | "outputs": [],
89 | "source": [
90 | "qa_dict={'question_texts':question_texts,'answer_texts':answer_texts}"
91 | ]
92 | },
93 | {
94 | "cell_type": "code",
95 | "execution_count": 6,
96 | "metadata": {
97 | "collapsed": true
98 | },
99 | "outputs": [],
100 | "source": [
101 | "qa_csv=pd.DataFrame(qa_dict)"
102 | ]
103 | },
104 | {
105 | "cell_type": "code",
106 | "execution_count": 7,
107 | "metadata": {},
108 | "outputs": [
109 | {
110 | "data": {
111 | "text/html": [
112 | "\n",
113 | "\n",
126 | "
\n",
127 | " \n",
128 | " \n",
129 | " | \n",
130 | " question_texts | \n",
131 | " answer_texts | \n",
132 | "
\n",
133 | " \n",
134 | " \n",
135 | " \n",
136 | " 0 | \n",
137 | " 什么是ai | \n",
138 | " 人工智能是工程和科学的分支,致力于构建思维的机器。 | \n",
139 | "
\n",
140 | " \n",
141 | " 1 | \n",
142 | " 你写的是什么语言 | \n",
143 | " 蟒蛇 | \n",
144 | "
\n",
145 | " \n",
146 | " 2 | \n",
147 | " 你听起来像数据 | \n",
148 | " 是的,我受到指挥官数据的人工个性的启发 | \n",
149 | "
\n",
150 | " \n",
151 | " 3 | \n",
152 | " 你是一个人工语言实体 | \n",
153 | " 那是我的名字。 | \n",
154 | "
\n",
155 | " \n",
156 | " 4 | \n",
157 | " 你不是不朽的 | \n",
158 | " 所有的软件都可以永久存在。 | \n",
159 | "
\n",
160 | " \n",
161 | "
\n",
162 | "
"
163 | ],
164 | "text/plain": [
165 | " question_texts answer_texts\n",
166 | "0 什么是ai 人工智能是工程和科学的分支,致力于构建思维的机器。\n",
167 | "1 你写的是什么语言 蟒蛇\n",
168 | "2 你听起来像数据 是的,我受到指挥官数据的人工个性的启发\n",
169 | "3 你是一个人工语言实体 那是我的名字。\n",
170 | "4 你不是不朽的 所有的软件都可以永久存在。"
171 | ]
172 | },
173 | "execution_count": 7,
174 | "metadata": {},
175 | "output_type": "execute_result"
176 | }
177 | ],
178 | "source": [
179 | "qa_csv.head()"
180 | ]
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 8,
185 | "metadata": {
186 | "collapsed": true
187 | },
188 | "outputs": [],
189 | "source": [
190 | "qa_csv.to_csv('./data/qa/qa.csv')"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": 9,
196 | "metadata": {
197 | "collapsed": true
198 | },
199 | "outputs": [],
200 | "source": [
201 | "qa_data=pd.read_csv('./data/qa/qa.csv')"
202 | ]
203 | },
204 | {
205 | "cell_type": "code",
206 | "execution_count": 10,
207 | "metadata": {},
208 | "outputs": [
209 | {
210 | "data": {
211 | "text/html": [
212 | "\n",
213 | "\n",
226 | "
\n",
227 | " \n",
228 | " \n",
229 | " | \n",
230 | " Unnamed: 0 | \n",
231 | " question_texts | \n",
232 | " answer_texts | \n",
233 | "
\n",
234 | " \n",
235 | " \n",
236 | " \n",
237 | " 0 | \n",
238 | " 0 | \n",
239 | " 什么是ai | \n",
240 | " 人工智能是工程和科学的分支,致力于构建思维的机器。 | \n",
241 | "
\n",
242 | " \n",
243 | " 1 | \n",
244 | " 1 | \n",
245 | " 你写的是什么语言 | \n",
246 | " 蟒蛇 | \n",
247 | "
\n",
248 | " \n",
249 | " 2 | \n",
250 | " 2 | \n",
251 | " 你听起来像数据 | \n",
252 | " 是的,我受到指挥官数据的人工个性的启发 | \n",
253 | "
\n",
254 | " \n",
255 | " 3 | \n",
256 | " 3 | \n",
257 | " 你是一个人工语言实体 | \n",
258 | " 那是我的名字。 | \n",
259 | "
\n",
260 | " \n",
261 | " 4 | \n",
262 | " 4 | \n",
263 | " 你不是不朽的 | \n",
264 | " 所有的软件都可以永久存在。 | \n",
265 | "
\n",
266 | " \n",
267 | "
\n",
268 | "
"
269 | ],
270 | "text/plain": [
271 | " Unnamed: 0 question_texts answer_texts\n",
272 | "0 0 什么是ai 人工智能是工程和科学的分支,致力于构建思维的机器。\n",
273 | "1 1 你写的是什么语言 蟒蛇\n",
274 | "2 2 你听起来像数据 是的,我受到指挥官数据的人工个性的启发\n",
275 | "3 3 你是一个人工语言实体 那是我的名字。\n",
276 | "4 4 你不是不朽的 所有的软件都可以永久存在。"
277 | ]
278 | },
279 | "execution_count": 10,
280 | "metadata": {},
281 | "output_type": "execute_result"
282 | }
283 | ],
284 | "source": [
285 | "qa_data.head()"
286 | ]
287 | },
288 | {
289 | "cell_type": "code",
290 | "execution_count": 11,
291 | "metadata": {
292 | "collapsed": true
293 | },
294 | "outputs": [],
295 | "source": [
296 | "# 这是问答系统的"
297 | ]
298 | },
299 | {
300 | "cell_type": "code",
301 | "execution_count": 12,
302 | "metadata": {
303 | "collapsed": true
304 | },
305 | "outputs": [],
306 | "source": [
307 | "sentences=[]"
308 | ]
309 | },
310 | {
311 | "cell_type": "code",
312 | "execution_count": 13,
313 | "metadata": {},
314 | "outputs": [
315 | {
316 | "data": {
317 | "text/plain": [
318 | "430"
319 | ]
320 | },
321 | "execution_count": 13,
322 | "metadata": {},
323 | "output_type": "execute_result"
324 | }
325 | ],
326 | "source": [
327 | "len(qa_data.question_texts)"
328 | ]
329 | },
330 | {
331 | "cell_type": "code",
332 | "execution_count": 14,
333 | "metadata": {
334 | "collapsed": true
335 | },
336 | "outputs": [],
337 | "source": [
338 | "for i in range(len(qa_data)):\n",
339 | " sentences.append(qa_data.question_texts[i])\n",
340 | "for j in range(len(qa_data)):\n",
341 | " sentences.append(qa_data.answer_texts[j])"
342 | ]
343 | },
344 | {
345 | "cell_type": "code",
346 | "execution_count": 15,
347 | "metadata": {},
348 | "outputs": [
349 | {
350 | "data": {
351 | "text/plain": [
352 | "860"
353 | ]
354 | },
355 | "execution_count": 15,
356 | "metadata": {},
357 | "output_type": "execute_result"
358 | }
359 | ],
360 | "source": [
361 | "len(sentences)"
362 | ]
363 | },
364 | {
365 | "cell_type": "code",
366 | "execution_count": 16,
367 | "metadata": {
368 | "collapsed": true
369 | },
370 | "outputs": [],
371 | "source": [
372 | "for senterce in sentences:\n",
373 | "# i=i+1\n",
374 | "# if i==0:\n",
375 | "# break\n",
376 | " if len(senterce) == 0:\n",
377 | " continue\n",
378 | " # 补全缺失文字,需重新运行\n",
379 | " # 按字的数量添加代码\n",
380 | " for t, char in enumerate(senterce):\n",
381 | " if alphabet.find(char) == -1:\n",
382 | " f2 = open('word.txt', 'w', encoding='utf-8')\n",
383 | " f2.truncate() # 清空文件\n",
384 | " alphabet += char\n",
385 | " f2.write(alphabet)\n",
386 | " f2.close()"
387 | ]
388 | },
389 | {
390 | "cell_type": "code",
391 | "execution_count": 17,
392 | "metadata": {},
393 | "outputs": [
394 | {
395 | "data": {
396 | "text/plain": [
397 | "1163"
398 | ]
399 | },
400 | "execution_count": 17,
401 | "metadata": {},
402 | "output_type": "execute_result"
403 | }
404 | ],
405 | "source": [
406 | "len(alphabet)"
407 | ]
408 | },
409 | {
410 | "cell_type": "code",
411 | "execution_count": 18,
412 | "metadata": {
413 | "collapsed": true
414 | },
415 | "outputs": [],
416 | "source": [
417 | "# 机器翻译"
418 | ]
419 | },
420 | {
421 | "cell_type": "code",
422 | "execution_count": 1,
423 | "metadata": {
424 | "collapsed": true
425 | },
426 | "outputs": [],
427 | "source": [
428 | "data_path = './data/translation/ell.txt'"
429 | ]
430 | },
431 | {
432 | "cell_type": "code",
433 | "execution_count": 2,
434 | "metadata": {
435 | "collapsed": true
436 | },
437 | "outputs": [],
438 | "source": [
439 | "num_samples = 10000 # Number of samples to train on."
440 | ]
441 | },
442 | {
443 | "cell_type": "code",
444 | "execution_count": 3,
445 | "metadata": {
446 | "collapsed": true
447 | },
448 | "outputs": [],
449 | "source": [
450 | "# Vectorize the data.\n",
451 | "\n",
452 | "# 输入\n",
453 | "input_texts = []\n",
454 | "\n",
455 | "target_texts = []\n",
456 | "\n",
457 | "input_characters = set()\n",
458 | "\n",
459 | "target_characters = set()\n",
460 | "\n",
461 | "#打开文档\n",
462 | "with open(data_path, 'r', encoding='utf-8') as f:\n",
463 | " lines = f.read().split('\\n')\n",
464 | "\n",
465 | "#每次加2\n",
466 | "#输入文档到相关内容中\n",
467 | "for line in lines[: min(num_samples, len(lines) - 1)]:\n",
468 | " \n",
469 | " input_text, target_text, _ = line.split('\\t')\n",
470 | " \n",
471 | " \n",
472 | " # We use \"tab\" as the \"start sequence\" character\n",
473 | " # for the targets, and \"\\n\" as \"end sequence\" character.\n",
474 | " \n",
475 | "# target_text = '\\t' + target_text + '\\n'\n",
476 | "\n",
477 | " input_texts.append(input_text)\n",
478 | "# print(input_text)\n",
479 | "# print(len(input_text))\n",
480 | " target_texts.append(target_text)\n",
481 | " \n",
482 | " #这个是做字典\n",
483 | " for char in input_text:\n",
484 | " if char not in input_characters:\n",
485 | " input_characters.add(char)\n",
486 | " \n",
487 | " for char in target_text:\n",
488 | " if char not in target_characters:\n",
489 | " target_characters.add(char)"
490 | ]
491 | },
492 | {
493 | "cell_type": "code",
494 | "execution_count": 4,
495 | "metadata": {},
496 | "outputs": [
497 | {
498 | "data": {
499 | "text/plain": [
500 | "69"
501 | ]
502 | },
503 | "execution_count": 4,
504 | "metadata": {},
505 | "output_type": "execute_result"
506 | }
507 | ],
508 | "source": [
509 | "len(input_characters)"
510 | ]
511 | },
512 | {
513 | "cell_type": "code",
514 | "execution_count": 47,
515 | "metadata": {
516 | "collapsed": true
517 | },
518 | "outputs": [],
519 | "source": [
520 | "input_characters.add('\\t')"
521 | ]
522 | },
523 | {
524 | "cell_type": "code",
525 | "execution_count": 49,
526 | "metadata": {
527 | "collapsed": true
528 | },
529 | "outputs": [],
530 | "source": [
531 | "input_characters.remove('\\t')"
532 | ]
533 | },
534 | {
535 | "cell_type": "code",
536 | "execution_count": null,
537 | "metadata": {
538 | "collapsed": true
539 | },
540 | "outputs": [],
541 | "source": []
542 | },
543 | {
544 | "cell_type": "code",
545 | "execution_count": 5,
546 | "metadata": {},
547 | "outputs": [
548 | {
549 | "data": {
550 | "text/plain": [
551 | "108"
552 | ]
553 | },
554 | "execution_count": 5,
555 | "metadata": {},
556 | "output_type": "execute_result"
557 | }
558 | ],
559 | "source": [
560 | "len(target_characters)"
561 | ]
562 | },
563 | {
564 | "cell_type": "code",
565 | "execution_count": 6,
566 | "metadata": {
567 | "collapsed": true
568 | },
569 | "outputs": [],
570 | "source": [
571 | "target_characters.add('\\t')"
572 | ]
573 | },
574 | {
575 | "cell_type": "code",
576 | "execution_count": 7,
577 | "metadata": {
578 | "collapsed": true
579 | },
580 | "outputs": [],
581 | "source": [
582 | "target_characters.add('\\n')"
583 | ]
584 | },
585 | {
586 | "cell_type": "code",
587 | "execution_count": 64,
588 | "metadata": {
589 | "collapsed": true
590 | },
591 | "outputs": [],
592 | "source": [
593 | "# target_characters"
594 | ]
595 | },
596 | {
597 | "cell_type": "code",
598 | "execution_count": null,
599 | "metadata": {
600 | "collapsed": true
601 | },
602 | "outputs": [],
603 | "source": []
604 | },
605 | {
606 | "cell_type": "code",
607 | "execution_count": 58,
608 | "metadata": {
609 | "collapsed": true
610 | },
611 | "outputs": [],
612 | "source": [
613 | "# for i in range(len(target_characters)):\n",
614 | "# print(i)\n",
615 | "# print(target_characters)"
616 | ]
617 | },
618 | {
619 | "cell_type": "code",
620 | "execution_count": 59,
621 | "metadata": {
622 | "collapsed": true
623 | },
624 | "outputs": [],
625 | "source": [
626 | "import pickle "
627 | ]
628 | },
629 | {
630 | "cell_type": "code",
631 | "execution_count": 60,
632 | "metadata": {
633 | "collapsed": true
634 | },
635 | "outputs": [],
636 | "source": [
637 | "with open('target_characters.pkl', 'wb') as f:\n",
638 | " pickle.dump(target_characters, f, pickle.HIGHEST_PROTOCOL)"
639 | ]
640 | },
641 | {
642 | "cell_type": "code",
643 | "execution_count": 61,
644 | "metadata": {
645 | "collapsed": true
646 | },
647 | "outputs": [],
648 | "source": [
649 | "with open('input_characters.pkl', 'wb') as f:\n",
650 | " pickle.dump(input_characters, f, pickle.HIGHEST_PROTOCOL)"
651 | ]
652 | },
653 | {
654 | "cell_type": "code",
655 | "execution_count": null,
656 | "metadata": {
657 | "collapsed": true
658 | },
659 | "outputs": [],
660 | "source": []
661 | },
662 | {
663 | "cell_type": "code",
664 | "execution_count": 12,
665 | "metadata": {
666 | "collapsed": true
667 | },
668 | "outputs": [],
669 | "source": [
670 | "qa_dict={'input_texts':input_text,'target_text':target_text}"
671 | ]
672 | },
673 | {
674 | "cell_type": "code",
675 | "execution_count": 13,
676 | "metadata": {
677 | "collapsed": true
678 | },
679 | "outputs": [],
680 | "source": [
681 | "import pandas as pd"
682 | ]
683 | },
684 | {
685 | "cell_type": "code",
686 | "execution_count": 15,
687 | "metadata": {},
688 | "outputs": [],
689 | "source": [
690 | "translation_csv=pd.DataFrame()"
691 | ]
692 | },
693 | {
694 | "cell_type": "code",
695 | "execution_count": 17,
696 | "metadata": {
697 | "collapsed": true
698 | },
699 | "outputs": [],
700 | "source": [
701 | "translation_csv['input_texts']=input_texts"
702 | ]
703 | },
704 | {
705 | "cell_type": "code",
706 | "execution_count": 21,
707 | "metadata": {
708 | "collapsed": true
709 | },
710 | "outputs": [],
711 | "source": [
712 | "translation_csv['target_texts']=target_texts"
713 | ]
714 | },
715 | {
716 | "cell_type": "code",
717 | "execution_count": 22,
718 | "metadata": {},
719 | "outputs": [
720 | {
721 | "data": {
722 | "text/html": [
723 | "\n",
724 | "\n",
737 | "
\n",
738 | " \n",
739 | " \n",
740 | " | \n",
741 | " input_texts | \n",
742 | " target_texts | \n",
743 | "
\n",
744 | " \n",
745 | " \n",
746 | " \n",
747 | " 0 | \n",
748 | " Go. | \n",
749 | " Πάμε. | \n",
750 | "
\n",
751 | " \n",
752 | " 1 | \n",
753 | " Run! | \n",
754 | " Τρέξε! | \n",
755 | "
\n",
756 | " \n",
757 | " 2 | \n",
758 | " Run! | \n",
759 | " Τρέξτε! | \n",
760 | "
\n",
761 | " \n",
762 | " 3 | \n",
763 | " Who? | \n",
764 | " Ποιος; | \n",
765 | "
\n",
766 | " \n",
767 | " 4 | \n",
768 | " Wow! | \n",
769 | " Ουάου! | \n",
770 | "
\n",
771 | " \n",
772 | "
\n",
773 | "
"
774 | ],
775 | "text/plain": [
776 | " input_texts target_texts\n",
777 | "0 Go. Πάμε.\n",
778 | "1 Run! Τρέξε!\n",
779 | "2 Run! Τρέξτε!\n",
780 | "3 Who? Ποιος;\n",
781 | "4 Wow! Ουάου!"
782 | ]
783 | },
784 | "execution_count": 22,
785 | "metadata": {},
786 | "output_type": "execute_result"
787 | }
788 | ],
789 | "source": [
790 | "translation_csv.head()"
791 | ]
792 | },
793 | {
794 | "cell_type": "code",
795 | "execution_count": 19,
796 | "metadata": {
797 | "collapsed": true
798 | },
799 | "outputs": [],
800 | "source": [
801 | "del translation_csv['target_texts']"
802 | ]
803 | },
804 | {
805 | "cell_type": "code",
806 | "execution_count": null,
807 | "metadata": {
808 | "collapsed": true
809 | },
810 | "outputs": [],
811 | "source": []
812 | },
813 | {
814 | "cell_type": "code",
815 | "execution_count": 23,
816 | "metadata": {
817 | "collapsed": true
818 | },
819 | "outputs": [],
820 | "source": [
821 | "translation_csv.to_csv('./data/translation/translation.csv')"
822 | ]
823 | },
824 | {
825 | "cell_type": "code",
826 | "execution_count": 1,
827 | "metadata": {
828 | "collapsed": true
829 | },
830 | "outputs": [],
831 | "source": [
832 | "import numpy as np # linear algebra\n",
833 | "import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)"
834 | ]
835 | },
836 | {
837 | "cell_type": "code",
838 | "execution_count": 2,
839 | "metadata": {
840 | "collapsed": true
841 | },
842 | "outputs": [],
843 | "source": [
844 | "summary = pd.read_csv('./data/textsummary/news_summary.csv', encoding='iso-8859-1')\n",
845 | "raw = pd.read_csv('./data/textsummary/news_summary_more.csv', encoding='iso-8859-1')"
846 | ]
847 | },
848 | {
849 | "cell_type": "code",
850 | "execution_count": 5,
851 | "metadata": {
852 | "collapsed": true
853 | },
854 | "outputs": [],
855 | "source": [
856 | "pre1 = raw.iloc[:,0:2].copy()\n",
857 | "# pre1['head + text'] = pre1['headlines'].str.cat(pre1['text'], sep =\" \") \n",
858 | "\n",
859 | "pre2 = summary.iloc[:,0:6].copy()\n",
860 | "# 对模型进行预处理\n",
861 | "pre2['text'] = pre2['author'].str.cat(pre2['date'].str.cat(pre2['read_more'].str.cat(pre2['text'].str.cat(pre2['ctext'], sep = \" \"), sep =\" \"),sep= \" \"), sep = \" \")\n"
862 | ]
863 | },
864 | {
865 | "cell_type": "code",
866 | "execution_count": 6,
867 | "metadata": {
868 | "collapsed": true
869 | },
870 | "outputs": [],
871 | "source": [
872 | "pre = pd.DataFrame()\n",
873 | "pre['text'] = pd.concat([pre1['text'], pre2['text']], ignore_index=True)\n",
874 | "pre['summary'] = pd.concat([pre1['headlines'],pre2['headlines']],ignore_index = True)"
875 | ]
876 | },
877 | {
878 | "cell_type": "code",
879 | "execution_count": 7,
880 | "metadata": {},
881 | "outputs": [
882 | {
883 | "data": {
884 | "text/html": [
885 | "\n",
886 | "\n",
899 | "
\n",
900 | " \n",
901 | " \n",
902 | " | \n",
903 | " text | \n",
904 | " summary | \n",
905 | "
\n",
906 | " \n",
907 | " \n",
908 | " \n",
909 | " 0 | \n",
910 | " Saurav Kant, an alumnus of upGrad and IIIT-B's... | \n",
911 | " upGrad learner switches to career in ML & Al w... | \n",
912 | "
\n",
913 | " \n",
914 | " 1 | \n",
915 | " Kunal Shah's credit card bill payment platform... | \n",
916 | " Delhi techie wins free food from Swiggy for on... | \n",
917 | "
\n",
918 | " \n",
919 | " 2 | \n",
920 | " New Zealand defeated India by 8 wickets in the... | \n",
921 | " New Zealand end Rohit Sharma-led India's 12-ma... | \n",
922 | "
\n",
923 | " \n",
924 | " 3 | \n",
925 | " With Aegon Life iTerm Insurance plan, customer... | \n",
926 | " Aegon life iTerm insurance plan helps customer... | \n",
927 | "
\n",
928 | " \n",
929 | " 4 | \n",
930 | " Speaking about the sexual harassment allegatio... | \n",
931 | " Have known Hirani for yrs, what if MeToo claim... | \n",
932 | "
\n",
933 | " \n",
934 | "
\n",
935 | "
"
936 | ],
937 | "text/plain": [
938 | " text \\\n",
939 | "0 Saurav Kant, an alumnus of upGrad and IIIT-B's... \n",
940 | "1 Kunal Shah's credit card bill payment platform... \n",
941 | "2 New Zealand defeated India by 8 wickets in the... \n",
942 | "3 With Aegon Life iTerm Insurance plan, customer... \n",
943 | "4 Speaking about the sexual harassment allegatio... \n",
944 | "\n",
945 | " summary \n",
946 | "0 upGrad learner switches to career in ML & Al w... \n",
947 | "1 Delhi techie wins free food from Swiggy for on... \n",
948 | "2 New Zealand end Rohit Sharma-led India's 12-ma... \n",
949 | "3 Aegon life iTerm insurance plan helps customer... \n",
950 | "4 Have known Hirani for yrs, what if MeToo claim... "
951 | ]
952 | },
953 | "execution_count": 7,
954 | "metadata": {},
955 | "output_type": "execute_result"
956 | }
957 | ],
958 | "source": [
959 | "pre.head()"
960 | ]
961 | },
962 | {
963 | "cell_type": "code",
964 | "execution_count": 8,
965 | "metadata": {
966 | "collapsed": true
967 | },
968 | "outputs": [],
969 | "source": [
970 | "pre.to_csv('data.csv')"
971 | ]
972 | },
973 | {
974 | "cell_type": "code",
975 | "execution_count": null,
976 | "metadata": {
977 | "collapsed": true
978 | },
979 | "outputs": [],
980 | "source": []
981 | },
982 | {
983 | "cell_type": "code",
984 | "execution_count": 3,
985 | "metadata": {
986 | "collapsed": true
987 | },
988 | "outputs": [],
989 | "source": [
990 | "qa_dict={'text':raw,'summary':summary}"
991 | ]
992 | },
993 | {
994 | "cell_type": "code",
995 | "execution_count": 4,
996 | "metadata": {},
997 | "outputs": [
998 | {
999 | "ename": "ValueError",
1000 | "evalue": "If using all scalar values, you must pass an index",
1001 | "output_type": "error",
1002 | "traceback": [
1003 | "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
1004 | "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)",
1005 | "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mtext_summary\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqa_dict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
1006 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\frame.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, data, index, columns, dtype, copy)\u001b[0m\n\u001b[0;32m 466\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 467\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 468\u001b[1;33m \u001b[0mmgr\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minit_dict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 469\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0misinstance\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdata\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mMaskedArray\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 470\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmrecords\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mmrecords\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
1007 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36minit_dict\u001b[1;34m(data, index, columns, dtype)\u001b[0m\n\u001b[0;32m 281\u001b[0m \u001b[0marr\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mis_datetime64tz_dtype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32melse\u001b[0m \u001b[0marr\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0marr\u001b[0m \u001b[1;32min\u001b[0m \u001b[0marrays\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 282\u001b[0m ]\n\u001b[1;32m--> 283\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0marrays_to_mgr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marrays\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdata_names\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcolumns\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 284\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 285\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
1008 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36marrays_to_mgr\u001b[1;34m(arrays, arr_names, index, columns, dtype, verify_integrity)\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[1;31m# figure out the index, if necessary\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 77\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 78\u001b[1;33m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mextract_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marrays\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 79\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 80\u001b[0m \u001b[0mindex\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mensure_index\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mindex\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
1009 | "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pandas\\core\\internals\\construction.py\u001b[0m in \u001b[0;36mextract_index\u001b[1;34m(data)\u001b[0m\n\u001b[0;32m 385\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 386\u001b[0m \u001b[1;32mif\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mindexes\u001b[0m \u001b[1;32mand\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mraw_lengths\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 387\u001b[1;33m \u001b[1;32mraise\u001b[0m \u001b[0mValueError\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"If using all scalar values, you must pass an index\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 388\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 389\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhave_series\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
1010 | "\u001b[1;31mValueError\u001b[0m: If using all scalar values, you must pass an index"
1011 | ]
1012 | }
1013 | ],
1014 | "source": [
1015 | "text_summary=pd.DataFrame(qa_dict)"
1016 | ]
1017 | },
1018 | {
1019 | "cell_type": "code",
1020 | "execution_count": 71,
1021 | "metadata": {
1022 | "collapsed": true
1023 | },
1024 | "outputs": [],
1025 | "source": [
1026 | "import joblib"
1027 | ]
1028 | },
1029 | {
1030 | "cell_type": "code",
1031 | "execution_count": 73,
1032 | "metadata": {
1033 | "collapsed": true
1034 | },
1035 | "outputs": [],
1036 | "source": [
1037 | "x_tr=joblib.load('x_tr.pkl')"
1038 | ]
1039 | },
1040 | {
1041 | "cell_type": "code",
1042 | "execution_count": 77,
1043 | "metadata": {
1044 | "collapsed": true
1045 | },
1046 | "outputs": [],
1047 | "source": [
1048 | "# len(x_tr)"
1049 | ]
1050 | },
1051 | {
1052 | "cell_type": "code",
1053 | "execution_count": 79,
1054 | "metadata": {
1055 | "collapsed": true
1056 | },
1057 | "outputs": [],
1058 | "source": [
1059 | "x_val=joblib.load('x_val.pkl')"
1060 | ]
1061 | },
1062 | {
1063 | "cell_type": "code",
1064 | "execution_count": 80,
1065 | "metadata": {
1066 | "collapsed": true
1067 | },
1068 | "outputs": [],
1069 | "source": [
1070 | "y_tr=joblib.load('y_tr.pkl')"
1071 | ]
1072 | },
1073 | {
1074 | "cell_type": "code",
1075 | "execution_count": 81,
1076 | "metadata": {
1077 | "collapsed": true
1078 | },
1079 | "outputs": [],
1080 | "source": [
1081 | "y_val=joblib.load('y_val.pkl')"
1082 | ]
1083 | },
1084 | {
1085 | "cell_type": "code",
1086 | "execution_count": 91,
1087 | "metadata": {
1088 | "collapsed": true
1089 | },
1090 | "outputs": [],
1091 | "source": [
1092 | "text=[]\n",
1093 | "summary=[]"
1094 | ]
1095 | },
1096 | {
1097 | "cell_type": "code",
1098 | "execution_count": 92,
1099 | "metadata": {
1100 | "collapsed": true
1101 | },
1102 | "outputs": [],
1103 | "source": [
1104 | "for i in range(len(x_tr)):\n",
1105 | " text.append(x_tr[i])\n",
1106 | "for i in range(len(x_val)):\n",
1107 | " text.append(x_val[i])\n",
1108 | "for i in range(len(y_tr)):\n",
1109 | " summary.append(y_tr[i])\n",
1110 | "for i in range(len(y_val)):\n",
1111 | " summary.append(y_val[i])\n",
1112 | " \n",
1113 | "\n",
1114 | "# text.append(x_val)\n",
1115 | "# summary.append(y_tr)\n",
1116 | "# summary.append(y_val)"
1117 | ]
1118 | },
1119 | {
1120 | "cell_type": "code",
1121 | "execution_count": 93,
1122 | "metadata": {},
1123 | "outputs": [
1124 | {
1125 | "data": {
1126 | "text/plain": [
1127 | "98353"
1128 | ]
1129 | },
1130 | "execution_count": 93,
1131 | "metadata": {},
1132 | "output_type": "execute_result"
1133 | }
1134 | ],
1135 | "source": [
1136 | "len(text)"
1137 | ]
1138 | },
1139 | {
1140 | "cell_type": "code",
1141 | "execution_count": 94,
1142 | "metadata": {},
1143 | "outputs": [
1144 | {
1145 | "data": {
1146 | "text/plain": [
1147 | "98353"
1148 | ]
1149 | },
1150 | "execution_count": 94,
1151 | "metadata": {},
1152 | "output_type": "execute_result"
1153 | }
1154 | ],
1155 | "source": [
1156 | "len(summary)"
1157 | ]
1158 | },
1159 | {
1160 | "cell_type": "code",
1161 | "execution_count": 87,
1162 | "metadata": {
1163 | "collapsed": true
1164 | },
1165 | "outputs": [],
1166 | "source": [
1167 | "# y_val[0]"
1168 | ]
1169 | },
1170 | {
1171 | "cell_type": "code",
1172 | "execution_count": 95,
1173 | "metadata": {
1174 | "collapsed": true
1175 | },
1176 | "outputs": [],
1177 | "source": [
1178 | "post_pre=pd.DataFrame()"
1179 | ]
1180 | },
1181 | {
1182 | "cell_type": "code",
1183 | "execution_count": 96,
1184 | "metadata": {
1185 | "collapsed": true
1186 | },
1187 | "outputs": [],
1188 | "source": [
1189 | "post_pre['text']=text"
1190 | ]
1191 | },
1192 | {
1193 | "cell_type": "code",
1194 | "execution_count": 97,
1195 | "metadata": {
1196 | "collapsed": true
1197 | },
1198 | "outputs": [],
1199 | "source": [
1200 | "post_pre['summary']=summary"
1201 | ]
1202 | },
1203 | {
1204 | "cell_type": "code",
1205 | "execution_count": 98,
1206 | "metadata": {},
1207 | "outputs": [
1208 | {
1209 | "data": {
1210 | "text/html": [
1211 | "\n",
1212 | "\n",
1225 | "
\n",
1226 | " \n",
1227 | " \n",
1228 | " | \n",
1229 | " text | \n",
1230 | " summary | \n",
1231 | "
\n",
1232 | " \n",
1233 | " \n",
1234 | " \n",
1235 | " 0 | \n",
1236 | " pope francis on tuesday called for respect for... | \n",
1237 | " sostok _START_ pope avoids mention of rohingya... | \n",
1238 | "
\n",
1239 | " \n",
1240 | " 1 | \n",
1241 | " students of government school in uttar pradesh... | \n",
1242 | " sostok _START_ students seen washing dishes at... | \n",
1243 | "
\n",
1244 | " \n",
1245 | " 2 | \n",
1246 | " apple india profit surged by 140% in 2017-18 t... | \n",
1247 | " sostok _START_ apple india profit rises 140% t... | \n",
1248 | "
\n",
1249 | " \n",
1250 | " 3 | \n",
1251 | " uber has launched its electric scooter service... | \n",
1252 | " sostok _START_ uber launches electric scooter ... | \n",
1253 | "
\n",
1254 | " \n",
1255 | " 4 | \n",
1256 | " around 80 people were injured in accidents rel... | \n",
1257 | " sostok _START_ 80 people injured in kite-flyin... | \n",
1258 | "
\n",
1259 | " \n",
1260 | "
\n",
1261 | "
"
1262 | ],
1263 | "text/plain": [
1264 | " text \\\n",
1265 | "0 pope francis on tuesday called for respect for... \n",
1266 | "1 students of government school in uttar pradesh... \n",
1267 | "2 apple india profit surged by 140% in 2017-18 t... \n",
1268 | "3 uber has launched its electric scooter service... \n",
1269 | "4 around 80 people were injured in accidents rel... \n",
1270 | "\n",
1271 | " summary \n",
1272 | "0 sostok _START_ pope avoids mention of rohingya... \n",
1273 | "1 sostok _START_ students seen washing dishes at... \n",
1274 | "2 sostok _START_ apple india profit rises 140% t... \n",
1275 | "3 sostok _START_ uber launches electric scooter ... \n",
1276 | "4 sostok _START_ 80 people injured in kite-flyin... "
1277 | ]
1278 | },
1279 | "execution_count": 98,
1280 | "metadata": {},
1281 | "output_type": "execute_result"
1282 | }
1283 | ],
1284 | "source": [
1285 | "post_pre.head()"
1286 | ]
1287 | },
1288 | {
1289 | "cell_type": "code",
1290 | "execution_count": 99,
1291 | "metadata": {
1292 | "collapsed": true
1293 | },
1294 | "outputs": [],
1295 | "source": [
1296 | "post_pre.to_csv('text_summary.csv')"
1297 | ]
1298 | },
1299 | {
1300 | "cell_type": "code",
1301 | "execution_count": null,
1302 | "metadata": {
1303 | "collapsed": true
1304 | },
1305 | "outputs": [],
1306 | "source": [
1307 | "summary.str"
1308 | ]
1309 | },
1310 | {
1311 | "cell_type": "code",
1312 | "execution_count": null,
1313 | "metadata": {
1314 | "collapsed": true
1315 | },
1316 | "outputs": [],
1317 | "source": []
1318 | },
1319 | {
1320 | "cell_type": "code",
1321 | "execution_count": null,
1322 | "metadata": {
1323 | "collapsed": true
1324 | },
1325 | "outputs": [],
1326 | "source": []
1327 | },
1328 | {
1329 | "cell_type": "code",
1330 | "execution_count": null,
1331 | "metadata": {
1332 | "collapsed": true
1333 | },
1334 | "outputs": [],
1335 | "source": [
1336 | "word_file = open('./data/qa/input_characters.txt', 'r', encoding='UTF-8')\n",
1337 | "alphabet = word_file.read() # 2500\n",
1338 | "# alphabet += 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' # 英文数字\n",
1339 | "# alphabet += ',./;\\'[]\\\\-=`<>?:\"{+}|_)(*&^%$#@!~` ' # 标点\n",
1340 | "# alphabet += ',。《》?;‘’:“”【】—()…¥!·' # 中文标点\n",
1341 | "# alphabet += '\\t\\n' # 开头结束标志\n",
1342 | "word_file.close()\n",
1343 | "print('word', len(alphabet), alphabet)"
1344 | ]
1345 | }
1346 | ],
1347 | "metadata": {
1348 | "kernelspec": {
1349 | "display_name": "Python 3",
1350 | "language": "python",
1351 | "name": "python3"
1352 | },
1353 | "language_info": {
1354 | "codemirror_mode": {
1355 | "name": "ipython",
1356 | "version": 3
1357 | },
1358 | "file_extension": ".py",
1359 | "mimetype": "text/x-python",
1360 | "name": "python",
1361 | "nbconvert_exporter": "python",
1362 | "pygments_lexer": "ipython3",
1363 | "version": "3.6.3"
1364 | }
1365 | },
1366 | "nbformat": 4,
1367 | "nbformat_minor": 2
1368 | }
1369 |
--------------------------------------------------------------------------------
/问答机器人kerasTransformer.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "code",
5 | "execution_count": 1,
6 | "metadata": {
7 | "collapsed": true
8 | },
9 | "outputs": [],
10 | "source": [
11 | "import numpy as np\n",
12 | "import pickle\n",
13 | "import operator\n",
14 | "import pandas as pd\n",
15 | "import jieba\n",
16 | "# from language.langconv import *\n",
17 | "import os"
18 | ]
19 | },
20 | {
21 | "cell_type": "code",
22 | "execution_count": 2,
23 | "metadata": {
24 | "collapsed": true
25 | },
26 | "outputs": [],
27 | "source": [
28 | "qa_data=pd.read_csv('./data/qa/qa.csv')"
29 | ]
30 | },
31 | {
32 | "cell_type": "code",
33 | "execution_count": 6,
34 | "metadata": {
35 | "collapsed": true
36 | },
37 | "outputs": [],
38 | "source": [
39 | "question_texts=[str(j) for j in qa_data.question_texts]"
40 | ]
41 | },
42 | {
43 | "cell_type": "code",
44 | "execution_count": 7,
45 | "metadata": {
46 | "collapsed": true
47 | },
48 | "outputs": [],
49 | "source": [
50 | "answer_texts=[str(j) for j in qa_data.answer_texts]"
51 | ]
52 | },
53 | {
54 | "cell_type": "code",
55 | "execution_count": 8,
56 | "metadata": {},
57 | "outputs": [
58 | {
59 | "data": {
60 | "text/plain": [
61 | "'你写的是什么语言'"
62 | ]
63 | },
64 | "execution_count": 8,
65 | "metadata": {},
66 | "output_type": "execute_result"
67 | }
68 | ],
69 | "source": [
70 | "question_texts[1]"
71 | ]
72 | },
73 | {
74 | "cell_type": "code",
75 | "execution_count": 11,
76 | "metadata": {
77 | "collapsed": true
78 | },
79 | "outputs": [],
80 | "source": [
81 | "e=question_texts[i]\n",
82 | "str1 = ''.join(e)"
83 | ]
84 | },
85 | {
86 | "cell_type": "code",
87 | "execution_count": 14,
88 | "metadata": {},
89 | "outputs": [
90 | {
91 | "data": {
92 | "text/plain": [
93 | "str"
94 | ]
95 | },
96 | "execution_count": 14,
97 | "metadata": {},
98 | "output_type": "execute_result"
99 | }
100 | ],
101 | "source": [
102 | "type(str1)"
103 | ]
104 | },
105 | {
106 | "cell_type": "code",
107 | "execution_count": 15,
108 | "metadata": {},
109 | "outputs": [
110 | {
111 | "data": {
112 | "text/plain": [
113 | "['什么', '是', 'ai']"
114 | ]
115 | },
116 | "execution_count": 15,
117 | "metadata": {},
118 | "output_type": "execute_result"
119 | }
120 | ],
121 | "source": [
122 | "jieba.lcut(str1)"
123 | ]
124 | },
125 | {
126 | "cell_type": "code",
127 | "execution_count": 25,
128 | "metadata": {},
129 | "outputs": [],
130 | "source": [
131 | "a=' '.join(jieba.lcut(str1, cut_all=False))\n",
132 | "# ' '.join(jieba.lcut(str1, cut_all=False))"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 27,
138 | "metadata": {},
139 | "outputs": [
140 | {
141 | "data": {
142 | "text/plain": [
143 | "7"
144 | ]
145 | },
146 | "execution_count": 27,
147 | "metadata": {},
148 | "output_type": "execute_result"
149 | }
150 | ],
151 | "source": [
152 | "len(a)"
153 | ]
154 | },
155 | {
156 | "cell_type": "code",
157 | "execution_count": 35,
158 | "metadata": {},
159 | "outputs": [],
160 | "source": [
161 | "# a[7]"
162 | ]
163 | },
164 | {
165 | "cell_type": "code",
166 | "execution_count": null,
167 | "metadata": {
168 | "collapsed": true
169 | },
170 | "outputs": [],
171 | "source": []
172 | },
173 | {
174 | "cell_type": "code",
175 | "execution_count": null,
176 | "metadata": {
177 | "collapsed": true
178 | },
179 | "outputs": [],
180 | "source": []
181 | },
182 | {
183 | "cell_type": "code",
184 | "execution_count": 37,
185 | "metadata": {
186 | "collapsed": true
187 | },
188 | "outputs": [],
189 | "source": [
190 | "seq = ' '.join(jieba.lcut(str1, cut_all=False))"
191 | ]
192 | },
193 | {
194 | "cell_type": "code",
195 | "execution_count": 38,
196 | "metadata": {},
197 | "outputs": [
198 | {
199 | "data": {
200 | "text/plain": [
201 | "'什么 是 ai'"
202 | ]
203 | },
204 | "execution_count": 38,
205 | "metadata": {},
206 | "output_type": "execute_result"
207 | }
208 | ],
209 | "source": [
210 | "seq"
211 | ]
212 | },
213 | {
214 | "cell_type": "code",
215 | "execution_count": null,
216 | "metadata": {
217 | "collapsed": true
218 | },
219 | "outputs": [],
220 | "source": []
221 | },
222 | {
223 | "cell_type": "code",
224 | "execution_count": null,
225 | "metadata": {
226 | "collapsed": true
227 | },
228 | "outputs": [],
229 | "source": []
230 | },
231 | {
232 | "cell_type": "code",
233 | "execution_count": null,
234 | "metadata": {
235 | "collapsed": true
236 | },
237 | "outputs": [],
238 | "source": []
239 | },
240 | {
241 | "cell_type": "code",
242 | "execution_count": 39,
243 | "metadata": {},
244 | "outputs": [],
245 | "source": [
246 | "source_tokens=[]\n",
247 | "target_tokens=[]\n",
248 | "# =[]\n",
249 | "for i in range(len(qa_data)):\n",
250 | " e=question_texts[i]\n",
251 | " str1 = ''.join(e)\n",
252 | " source_tokens.append(' '.join(jieba.lcut(str1, cut_all=False)))\n",
253 | "# sentences.append(qa_data.question_texts[i])\n",
254 | "for j in range(len(qa_data)):\n",
255 | "# sentences.append(qa_data.answer_texts[j])\n",
256 | " c=answer_texts[j]\n",
257 | " str2 = ''.join(c)\n",
258 | " target_tokens.append(' '.join(jieba.lcut(str2, cut_all=False)))"
259 | ]
260 | },
261 | {
262 | "cell_type": "code",
263 | "execution_count": 40,
264 | "metadata": {
265 | "collapsed": true
266 | },
267 | "outputs": [],
268 | "source": [
269 | "# 生成不同语言的词典\n",
270 | "def build_token_dict(token_list):\n",
271 | " token_dict = {\n",
272 | " '': 0,\n",
273 | " '': 1,\n",
274 | " '': 2,\n",
275 | " }\n",
276 | " for line in token_list:\n",
277 | " for token in line.split(' '):\n",
278 | " if token not in token_dict:\n",
279 | " token_dict[token]=len(token_dict)\n",
280 | " return token_dict"
281 | ]
282 | },
283 | {
284 | "cell_type": "code",
285 | "execution_count": 41,
286 | "metadata": {
287 | "collapsed": true
288 | },
289 | "outputs": [],
290 | "source": [
291 | "source_token_dict = build_token_dict(source_tokens)\n",
292 | "target_token_dict = build_token_dict(target_tokens)\n",
293 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}"
294 | ]
295 | },
296 | {
297 | "cell_type": "code",
298 | "execution_count": 43,
299 | "metadata": {},
300 | "outputs": [],
301 | "source": [
302 | "# source_token_dict"
303 | ]
304 | },
305 | {
306 | "cell_type": "code",
307 | "execution_count": 45,
308 | "metadata": {},
309 | "outputs": [],
310 | "source": [
311 | "# target_token_dict"
312 | ]
313 | },
314 | {
315 | "cell_type": "code",
316 | "execution_count": 47,
317 | "metadata": {},
318 | "outputs": [],
319 | "source": [
320 | "# target_token_dict_inv"
321 | ]
322 | },
323 | {
324 | "cell_type": "code",
325 | "execution_count": 48,
326 | "metadata": {},
327 | "outputs": [
328 | {
329 | "name": "stdout",
330 | "output_type": "stream",
331 | "text": [
332 | "430\n"
333 | ]
334 | }
335 | ],
336 | "source": [
337 | "# 添加特殊符号\n",
338 | "encode_tokens = [[''] + tokens.split(' ') + [''] for tokens in source_tokens]\n",
339 | "decode_tokens = [[''] + tokens.split(' ') + [''] for tokens in target_tokens]\n",
340 | "output_tokens = [tokens.split(' ') + ['', ''] for tokens in target_tokens]\n",
341 | "\n",
342 | "source_max_len = max(map(len, encode_tokens))\n",
343 | "target_max_len = max(map(len, decode_tokens))\n",
344 | "\n",
345 | "\n",
346 | "\n",
347 | "encode_tokens = [tokens + [''] * (source_max_len - len(tokens)) for tokens in encode_tokens]\n",
348 | "decode_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in decode_tokens]\n",
349 | "output_tokens = [tokens + [''] * (target_max_len - len(tokens)) for tokens in output_tokens]\n",
350 | "\n",
351 | "encode_input = [list(map(lambda x: source_token_dict[x], tokens)) for tokens in encode_tokens]\n",
352 | "decode_input = [list(map(lambda x: target_token_dict[x], tokens)) for tokens in decode_tokens]\n",
353 | "decode_output = [list(map(lambda x: [target_token_dict[x]], tokens)) for tokens in output_tokens]\n",
354 | "\n",
355 | "print(len(encode_input))"
356 | ]
357 | },
358 | {
359 | "cell_type": "code",
360 | "execution_count": 50,
361 | "metadata": {
362 | "collapsed": true
363 | },
364 | "outputs": [],
365 | "source": [
366 | "import numpy as np\n",
367 | "import pickle\n",
368 | "import operator\n",
369 | "path = 'data/middle_data/transformer/qa/'\n",
370 | "with open(path + 'encode_input.pkl', 'wb') as f:\n",
371 | " pickle.dump(encode_input, f, pickle.HIGHEST_PROTOCOL)\n",
372 | "with open(path + 'decode_input.pkl', 'wb') as f:\n",
373 | " pickle.dump(decode_input, f, pickle.HIGHEST_PROTOCOL)\n",
374 | "with open(path + 'decode_output.pkl', 'wb') as f:\n",
375 | " pickle.dump(decode_output, f, pickle.HIGHEST_PROTOCOL)\n",
376 | "with open(path + 'source_token_dict.pkl', 'wb') as f:\n",
377 | " pickle.dump(source_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
378 | "with open(path + 'target_token_dict.pkl', 'wb') as f:\n",
379 | " pickle.dump(target_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
380 | "with open(path + 'source_tokens.pkl', 'wb') as f:\n",
381 | " pickle.dump(source_tokens, f, pickle.HIGHEST_PROTOCOL)"
382 | ]
383 | },
384 | {
385 | "cell_type": "code",
386 | "execution_count": 52,
387 | "metadata": {},
388 | "outputs": [
389 | {
390 | "name": "stdout",
391 | "output_type": "stream",
392 | "text": [
393 | "Done\n"
394 | ]
395 | }
396 | ],
397 | "source": [
398 | "import numpy as np\n",
399 | "import pickle\n",
400 | "import operator\n",
401 | "from keras_transformer import get_model, decode\n",
402 | "# main_path = '/content/drive/My Drive/Colab Notebooks/' #Google Colab FilePath\n",
403 | "# path = main_path + 'middle_data/'\n",
404 | "path = 'data/middle_data/transformer/qa/'\n",
405 | "with open(path + 'encode_input.pkl', 'rb') as f:\n",
406 | " encode_input = pickle.load(f)\n",
407 | "with open(path + 'decode_input.pkl', 'rb') as f:\n",
408 | " decode_input = pickle.load(f)\n",
409 | "with open(path + 'decode_output.pkl', 'rb') as f:\n",
410 | " decode_output = pickle.load(f)\n",
411 | "with open(path + 'source_token_dict.pkl', 'rb') as f:\n",
412 | " source_token_dict = pickle.load(f)\n",
413 | "with open(path + 'target_token_dict.pkl', 'rb') as f:\n",
414 | " target_token_dict = pickle.load(f)\n",
415 | "with open(path + 'source_tokens.pkl', 'rb') as f:\n",
416 | " source_tokens = pickle.load(f)\n",
417 | "print('Done')"
418 | ]
419 | },
420 | {
421 | "cell_type": "code",
422 | "execution_count": 53,
423 | "metadata": {},
424 | "outputs": [
425 | {
426 | "name": "stdout",
427 | "output_type": "stream",
428 | "text": [
429 | "518\n",
430 | "1134\n",
431 | "430\n",
432 | "Done\n"
433 | ]
434 | }
435 | ],
436 | "source": [
437 | "print(len(source_token_dict))\n",
438 | "print(len(target_token_dict))\n",
439 | "print(len(encode_input))\n",
440 | "# 构建模型\n",
441 | "model = get_model(\n",
442 | " token_num=max(len(source_token_dict), len(target_token_dict)),\n",
443 | " embed_dim=64,\n",
444 | " encoder_num=2,\n",
445 | " decoder_num=2,\n",
446 | " head_num=4,\n",
447 | " hidden_dim=256,\n",
448 | " dropout_rate=0.05,\n",
449 | " use_same_embed=False, # 不同语言需要使用不同的词嵌入\n",
450 | ")\n",
451 | "model.compile('adam', 'sparse_categorical_crossentropy')\n",
452 | "# model.summary()\n",
453 | "print('Done')"
454 | ]
455 | },
456 | {
457 | "cell_type": "code",
458 | "execution_count": null,
459 | "metadata": {
460 | "collapsed": true
461 | },
462 | "outputs": [],
463 | "source": [
464 | "#训练模型\n",
465 | "from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau\n",
466 | "filepath = main_path + \"modles/W-\" + \"-{epoch:3d}-{loss:.4f}-.h5\"\n",
467 | "checkpoint = ModelCheckpoint(filepath,\n",
468 | " monitor='loss',\n",
469 | " verbose=1,\n",
470 | " save_best_only=True,\n",
471 | " mode='min',\n",
472 | " period=2,\n",
473 | " save_weights_only=True\n",
474 | " )\n",
475 | "reduce_lr = ReduceLROnPlateau(monitor='loss', \n",
476 | " factor=0.2, \n",
477 | " patience=2, \n",
478 | " verbose=1, \n",
479 | " mode='min', \n",
480 | " min_delta=0.0001, \n",
481 | " cooldown=0, \n",
482 | " min_lr=0\n",
483 | " )\n",
484 | "callbacks_list = [checkpoint, reduce_lr]\n",
485 | "model.fit(\n",
486 | " x=[np.array(encode_input), np.array(decode_input[:20000])],\n",
487 | " y=np.array(decode_output),\n",
488 | " epochs=100,\n",
489 | " batch_size=64, \n",
490 | " verbose=1,\n",
491 | " callbacks=callbacks_list, \n",
492 | " # class_weight=None, \n",
493 | " # max_queue_size=5, \n",
494 | "# workers=1, \n",
495 | "# use_multiprocessing=False,\n",
496 | " # shuffle=False,\n",
497 | "# initial_epoch=initial_epoch_\n",
498 | " )\n",
499 | "# model.save(main_path+'modles/model.h5')"
500 | ]
501 | },
502 | {
503 | "cell_type": "code",
504 | "execution_count": null,
505 | "metadata": {
506 | "collapsed": true
507 | },
508 | "outputs": [],
509 | "source": [
510 | "#加载模型\n",
511 | "model.load_weights('model/transformer/qa/qa.h5')\n",
512 | "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}\n",
513 | "print('Done')"
514 | ]
515 | },
516 | {
517 | "cell_type": "code",
518 | "execution_count": null,
519 | "metadata": {
520 | "collapsed": true
521 | },
522 | "outputs": [],
523 | "source": [
524 | "from keras.preprocessing import sequence\n",
525 | "import numpy as np\n",
526 | "import matplotlib.pyplot as plt\n",
527 | "import matplotlib\n",
528 | "import jieba\n",
529 | "import requests\n",
530 | "\n",
531 | "def get_input(seq):\n",
532 | " seq = ' '.join(jieba.lcut(seq, cut_all=False))\n",
533 | " # seq = ' '.join(seq)\n",
534 | " seq = seq.split(' ')\n",
535 | " print(seq)\n",
536 | " seq = [''] + seq + ['']\n",
537 | " seq = seq + [''] * (34 - len(seq))\n",
538 | " print(seq)\n",
539 | " for x in seq:\n",
540 | " try:\n",
541 | " source_token_dict[x]\n",
542 | " except KeyError:\n",
543 | " flag=False\n",
544 | " break\n",
545 | " else:\n",
546 | " flag=True\n",
547 | " if(flag):\n",
548 | " seq = [source_token_dict[x] for x in seq]\n",
549 | " return flag, seq\n",
550 | "def get_ans(seq):\n",
551 | " decoded = decode(\n",
552 | " model,\n",
553 | " [seq],\n",
554 | " start_token=target_token_dict[''],\n",
555 | " end_token=target_token_dict[''],\n",
556 | " pad_token=target_token_dict[''],\n",
557 | " # top_k=10,\n",
558 | " # temperature=1.0,\n",
559 | " )\n",
560 | " print(' '.join(map(lambda x: target_token_dict_inv[x], decoded[0][1:-1])))\n",
561 | "\n",
562 | "while True:\n",
563 | " seq = input()\n",
564 | " if seq == 'x':\n",
565 | " break\n",
566 | " flag, seq = get_input(seq)\n",
567 | " if(flag):\n",
568 | " get_ans(seq)\n",
569 | " else:\n",
570 | " print('听不懂呢。')"
571 | ]
572 | }
573 | ],
574 | "metadata": {
575 | "kernelspec": {
576 | "display_name": "Python 3",
577 | "language": "python",
578 | "name": "python3"
579 | },
580 | "language_info": {
581 | "codemirror_mode": {
582 | "name": "ipython",
583 | "version": 3
584 | },
585 | "file_extension": ".py",
586 | "mimetype": "text/x-python",
587 | "name": "python",
588 | "nbconvert_exporter": "python",
589 | "pygments_lexer": "ipython3",
590 | "version": "3.6.3"
591 | }
592 | },
593 | "nbformat": 4,
594 | "nbformat_minor": 2
595 | }
596 |
--------------------------------------------------------------------------------