-
Notifications
You must be signed in to change notification settings - Fork 1
/
baidu_pinyin.py
85 lines (73 loc) · 2.63 KB
/
baidu_pinyin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/8/10 16:01
# @Author : Ting
import requests
import pickle
import json
import os
mono_url = 'https://hanyu.baidu.com/hanyu/ajax/search_list?wd=%s组词&from=poem&' \
'userid=962828825&pn=%d&_=1533890304009'
pinyin_url = 'https://hanyu.baidu.com/hanyu/ajax/search_list?py=%s&ptype=zici_tag&' \
'wd=%s组词&userid=962828825&pn=%d&_=1533886788036'
file = open('fayin.txt', 'a')
word2pinyin = dict()
def get_polyphone_pinyin(char, pinyin):
page = 1
while True:
url = pinyin_url % (pinyin, char, page)
data = requests.get(url).json()
if 'ret_array' not in data or not data['ret_array']:
print(char + 'not collected')
break
for word in data['ret_array']:
file.write(str(word)+'\n')
update_data(word2pinyin, data['ret_array'])
if page >= data['extra']['total-page']:
break
page += 1
def get_monophone_pinyin(char):
page = 1
while True:
url = mono_url % (char, page)
data = requests.get(url).json()
if 'ret_array' not in data or not data['ret_array']:
print(char + 'not collected')
break
for word in data['ret_array']:
file.write(str(word)+'\n')
update_data(word2pinyin, data['ret_array'])
if page >= data['extra']['total-page']:
break
page += 1
def update_data(diction, data):
for d in data:
word = d['name'][0]
if word not in diction:
diction[word] = set()
diction[word].update(set(d['pinyin']))
char2pinyin = pickle.load(open('all.pkl', 'rb'))
items = list(char2pinyin.items())[:50]
for i, (char, pinyins) in enumerate(items):
word2pinyin[char] = dict()
if len(pinyins) > 1:
for pinyin in pinyins:
get_polyphone_pinyin(char, pinyin)
else:
get_monophone_pinyin(char)
if i % 100 == 0:
print(i, items[i: i+100])
# pickle.dump(word2pinyin, open('word2pinyin.pkl', 'wb'))
file.close()
# word2pinyin = dict()
# char2pinyin = pickle.load(open('all.pkl', 'rb'))
# items = list(char2pinyin.items())
# for i, (char, pinyins) in enumerate(items):
# if len(pinyins) > 1:
# for pinyin in pinyins:
# data = pickle.load(open('fayin/%s_%s.pkl' % (char, pinyin), 'rb'))
# update_data(word2pinyin, data)
# else:
# data = pickle.load(open('fayin/%s.pkl' % char, 'rb'))
# update_data(word2pinyin, data)
# pickle.dump(word2pinyin, open('word2pinyin.pkl', 'wb'))