成语接龙指定版本

昨天找到了新华字典的json就很开心,想到了一个顶俩那个项目,自己也想做一个接到口谐辞给。(因为没有给字开头的成语)

思路

算法过程其实很简单,就是广搜就可以,使用队列这个数据结构,每次pop一个,然后push进来一组当前读音的扩展。在此基础上进行广搜遍历,然后当满足输入要求的时候就停止。

扩展包

成语词典数据来源

https://github.com/mozillazg/python-pinyin
嫌GitHub慢的也可去
http://ggalaxy.top/file/idiom.json自取

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import json
import pypinyin
import unicodedata

#广搜,用队列
flag = 0
str(unicodedata.normalize('NFKD', pypinyin.lazy_pinyin("bǎi")[0]).encode('ascii','ignore'))[2:-1]

def judge(a,b):
if(unicodedata.normalize('NFKD', pypinyin.lazy_pinyin(a)[0]).encode('ascii','ignore') == unicodedata.normalize('NFKD', pypinyin.lazy_pinyin(b)[0]).encode('ascii','ignore')):
return True
else:
return False

def display(c):#传进来一个字符串
list_play = c.split("+")
#print(list_play)
for i in range(len(list_play)-1):
print("第"+str(i)+"个成语:",end = "")
print(file_json[int(list_play[i])]["word"])
def degrade(a):#数据降维
b = str(a)
b = b.replace('[','')
b = b.replace(']','')
b = b.replace("'",'')
#print(b)
a = b.split(",")
return a

def search_idiom(p):
#进来一个拼音,返回一个list,是以传进来的拼音结尾的
global flag
ans1=[]
ans2=[]
for iiii in range(l):#json
ll = len(file_json[iiii]["word"])
#print(file_json[iiii]["pinyin"].split(" "))
if(judge(file_json[iiii]["pinyin"].split(" ")[-1] ,p)):
#print(file_json[iiii]["pinyin"].split(" ")[0])
ans1.append(file_json[iiii]["pinyin"].split(" ")[0].replace("'",""))#返回首字的拼音和索引
ans2.append(str(iiii)+"+")
#print("-------------------------------------\n")
#print(file_json[iiii]["pinyin"].split(" ")[0],pinyin2)
#print(file_json[iiii]["pinyin"].split(" ")[0] == pinyin2)
#print("-------------------------------------\n")
if(judge(file_json[iiii]["pinyin"].split(" ")[0],pinyin2)):
flag = 1
#print(ans1,ans2)
return ans1,ans2
#print(ans1)
return ans1,ans2


def traverse(list_idiom,list_index):
idiom_tmp = degrade(list_idiom)
index_tmp = degrade(list_index)
#print(idiom_tmp)
global dp
global index
global flag
#l = len(np.array(list_idiom).shape)
l_tmp = len(idiom_tmp)

for i in range(l_tmp):
tmp1,tmp2 = search_idiom(idiom_tmp[i])
for j in range(len(tmp1)):
#print(tmp2[j])
tmp2[j]=tmp2[j]+index_tmp[i]
#print(tmp2[j])
if(flag !=0):
#print(tmp2[i])
print("成功了")
flag =1;
display(tmp2[-1])
break

if(len(tmp1)>0):
dp.append(tmp1)
#print(dp)
if(len(tmp2)>0):
index.append(tmp2)


file = open("H:/2020/idiom/idiom.json","rb")
file_json = json.load(file)
l = len(file_json)
while(1):
s = input("想要接龙到的成语(输入exit退出程序):")
if(s == ""):
continue
if(s == "exit"):
break
pinyin1 = pypinyin.pinyin(s)[0][0]#
#print(pinyin1)

while(1):
e = input("开始的成语(exit退出到上一级):")
if(e == ""):
continue
if(e == "exit"):
break
pinyin2 = pypinyin.pinyin(e)[-1][0]
#print(pinyin2)

#转为拼音
dp=[]#核心要维护的队列
index = []#对应dp的队列
dp.append(pinyin1)
index.append("")
times = 0
flag = 0
while(times < 10 and flag == 0):
for ii in range(len(dp)):
traverse(dp.pop(0),index.pop(0))
if(len(dp) == 0):
#print("dp=0")
flag =1


times+=1
print("第"+str(times)+"轮广搜查询")
# if(len(index)>0):
# for ij in range(len(index)):
# for ji in range(len(index[ij])):

# display(index[ij][ji])
# print("-------------------------------------\n")

强调

  • 除了要维护一个子扩展的拼音队列外,还需要维护一个索引队列,size和拼音队列相等,在地下的代码里面分别是dp[]和index[],索引队列用与在找到答案之后的返回,我使用字符串拼接然后split的方法。效果不错。
  • 一定要删除掉空的查询结果,不然你的数组会无穷大。。。。

效果

在这里插入图片描述

能算出来是真的
不过速度真的很慢,一般两轮以内能找到还行,轮次高了就很慢。
我寻思着深搜会不会更快?不过讲道理,一般人想到成语接龙要接到指定的成语肯定是先想着广搜吧,不可能一条道走到黑。
或者还有更好的搜索方法

在这里插入图片描述

python实现成语接龙??

群里面玩的成语接龙很嗨,想到了之前的一个顶俩的项目,自己搞一个陷害QQ小冰的程序吧。

扩展包

pypinyin
扩展包地址传送门

成语词典数据来源

https://github.com/mozillazg/python-pinyin
嫌GitHub慢的也可去
http://ggalaxy.top/file/idiom.json自取

具体代码

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
import json
import pypinyin

file = open("idiom.json","rb")
file_json = json.load(file)
l = len(file_json)
while(1):
s = input("目前要开始的字符:(输入exit退出):")
if(s == ""):
continue
if(s == "exit"):
break
pinyin1 = pypinyin.pinyin(s)[0][0]
print(pinyin1)
#转为拼音
e = input("想要结尾的字符,不要求的输入nothing:")
if e == "" or e =="nothing":
for i in range(l):
ll = len(file_json[i]["word"])
if(file_json[i]["pinyin"].split(" ")[0] == pinyin1):
print("-----------------------------------------------------------------------------------------------------")
print(file_json[i]["word"],end = "")
print(" ||拼音:",end = "")
print(file_json[i]["pinyin"],end = "")
print(" ||释意:",end = "")
print(file_json[i]["derivation"])
else:
for i in range(l):
ll = len(file_json[i]["word"])
if(file_json[i]["pinyin"].split(" ")[0] == pinyin1 and file_json[i]["word"][ll-1] == e):
print("--------------------------------------------------------------------------------------------------------")
print(file_json[i]["word"],end = "")
print(" ||拼音:",end = "")
print(file_json[i]["pinyin"],end = "")
print(" ||释意:",end = "")
print(file_json[i]["derivation"])
print(file_json[9356]["derivation"])



调戏

在这里插入图片描述

dp

给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。

示例 1:

输入: "babad"
输出: "bab"
注意: "aba" 也是一个有效答案。

示例 2:

输入: "cbbd"
输出: "bb"
### 解题思路 思路就是当一个回文串左边和右边字符相等的时候可以扩展,但是栽在了好多特殊情况下,然后一一判断(菜鸡做法),还有一个超时了,a把print删了居然过了。

代码

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
class Solution {
public String longestPalindrome(String s) {

int left = 0;
int right = 0;
int l = s.length();
if(l<2)return s;
int[][] dp = new int[l][l];//dp[i][j]是i到j的子串是否是回文
int i,j;
for(i=0;i<l-1;i++){
dp[i][i] = 1;//1代表是回文,长度为1当然是回文
if(left == right){
left = i;
right = i+1;
}
for(j=i+1;j>0;--j){

if(s.charAt(j-1) == s.charAt(i+1) && dp[j][i] == 1||s.charAt(j-1) == s.charAt(i+1)&&j>i){
dp[j-1][i+1]=1;
if((i-j+3)>=(right-left)){
left = j-1;
right = i+2;
}

}
}
}
return s.substring(left,right);
}
}

spyder

jsoup api

爬虫学习

6个包提供用于开发jsoup应用程序的类和接口。

org.jsoup
org.jsoup.examples
org.jsoup.helper
org.jsoup.nodes
org.jsoup.parser
org.jsoup.safety
org.jsoup.salect

主要类:

Jsoup 类提供了连接,清理和解析HTML文档的方法
Document 获取HTML文档
Element 获取、操作HTML节点

三种加载HTML的方法:

@Test
public void test1() throws IOException {
    //从URL加载HTML
    Document document = Jsoup.connect("http://www.baidu.com").get();
    String title = document.title();
    //获取html中的标题
    System.out.println("title :"+title);

    //从字符串加载HTML
    String html = "<html><head><title>First parse</title></head>"
            + "<body><p>Parsed HTML into a doc.</p></body></html>";
    Document doc = Jsoup.parse(html);
    title = doc.title();
    System.out.println("title :"+title);

    //从文件加载HTML
    doc = Jsoup.parse(new File("F:\\jsoup\\html\\index.html"),"utf-8");
    title = doc.title();
    System.out.println("title :"+title);
}

获取html中的head,body,url等信息:

@Test
public void test2() throws IOException {
    Document document = Jsoup.connect("http://www.baidu.com").get();
    String title = document.title();

    System.out.println("title :"+title);
    //获取html中的head
    System.out.println(document.head());
    //获取html中的body
    //System.out.println(document.body());

    //获取HTML页面中的所有链接
    Elements links = document.select("a[href]");
    for (Element link : links){
        System.out.println("link : "+ link.attr("href"));
        System.out.println("text :"+ link.text());
    }
}

获取URL元信息

@Test
public void test3() throws IOException {
    Document document = Jsoup.connect("https://passport.lagou.com").get();

    System.out.println(document.head());
    //获取URL的元信息
    String description = document.select("meta[name=description]").get(0).attr("content");
    System.out.println("Meta description : " + description);

    String keywords = document.select("meta[name=keywords]").first().attr("content");
    System.out.println("Meta keyword : " + keywords);
}

search-detail

根据class名称获取表单

@Test
public void test4() throws IOException {
    Document document = Jsoup.connect("https://passport.lagou.com/login/login.html?signature=8ECBCDF2B86061432B425A0B94FC863B&service=https%253A%252F%252Fwww.lagou.com%252F&action=login&serviceId=lagou&ts=1547711303033").get();
    //获取拉勾网登入页面的body
    //System.out.println(document.body());
    //根据class名称获取表单
    Elements formElement = document.getElementsByClass("form_body");
    System.out.println(formElement.html());
    //获取URL的元信息
    for (Element inputElement : formElement) {
        String placeholder = inputElement.getElementsByTag("input").attr("placeholder");
        System.out.println(placeholder);
    }
}

设计构思

新建独立的package,实现完整的通用接口,在spring中通过定时触发来调用爬虫清洗数据。

需求

  1. 静态学习资源,(优先级低)

  2. 每日更新学习资源,热评,或热点bug,或热点知识点,可以分类别整理(优先级高)

接口设计

  1. interface message_spyder()//爬取数据以及清洗数据接口(与外部交互)

//单关键词和多关键词检索????

[CSDN,Github,JianShu,博客园(有验证),菜鸟笔记…] * [Q&A,知识点,Bug]

class=“search-list J_search”

a herf 具体问题的连接

  • String spyder_github(“String key”)//github爬虫方法
  • String spyder_jianShu(String key)简书爬虫方法
  • String link_still_live();//有可能链接失效,需要定义一个检查连接是否失效的功能,返回已经失效的连接的String
  • String set_value_topic();//手动设置精华话题或文章,永久保存起来
  • String search_url(String url,String key);//通过自定义url接口进行模糊搜索。(难度大)
  1. interface message_manager()//存储数据以及更新数据接口(与数据库交互)
  • String get_abstract();//子链接主题,子链接内容摘要,子链接,时效

  • String get_contents();//子链接内容,子链接完整标题

数据库设计

以TEXT和url为主,图片以及其他的暂时不考虑。
可以清洗成功的,以String呈现,(无法清洗的,以URL呈现)。

  • Copyrights © 2015-2023 galaxy
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信