• 快速检索并引用你在CSDN上所有的博文笔记


    简 介: 利用CSDN提供的博文的列表,编写了一个快速检索自己博文,并自动插入索引连接的程序。这样就可以大大提高博文写作的效率,将之前记录学习、工作的内容更好的进行连接。最后,感谢CSDN技术人员的大力支持和帮助。

    关键词 博文索引分词引用

    背景介绍
    目 录
    Contents
    博文清单
    读取博文内容
    文章检索
    博文检索
    检索方案
    检测测试
    信息插入CSDN
    总 结
    csdm程序

     

    §00 景介绍

      人生就像一场持续的演出,你在其中既做演员、又当导演。就像李雪琴所说: 做不了情绪的导演,可以做情绪的场记 。认真记录一下自己的思考和体验,可以“在擦干眼泪之后,记住每一次教训”。

    ▲ 图1 李雪琴:做不了情绪的导演,可以做情绪的场记

    ▲ 图1 李雪琴:做不了情绪的导演,可以做情绪的场记

      提到场记,在CSDN上记录了很多学习、工作的记录。非常方便,有效。为了能够进一步提高这些场记对于后面的帮助。特地向CSDN索要了一份自己博文的清单列表。这样便可以作为基础便于今后的查询与索引。

    我想询问一下,如何能够方便获得我的所有在CSDN上文章的连接。 我在写作博文的时候,非常频繁的需要引用之前博文工作的内容,由于现在我的博文比较多(2460篇左右),通过现在CSDN上的查询比较麻烦,我想是否能够将我的所有博文的名称 以及对应的CSDN上的链接下载下来,我自己编写一个方便使用的Python名称检测,便可以加速我的博文写作过程。

    如果现在我通过网络爬虫下来这些信息,还是比较麻烦,因此我想询问是否你们可以帮我将现在所有我之前博文的链接能够生成发送给我,或者有什么方便的接口,我可以调用?

     

    §01 文清单


      天(2022-01-18)收到CSDN给我发送过来博文列表,包含了文章名称和访问连接。

    D:\Temp\卓晴老师全部文章列表.csv

    1.1 读取博文内容

      我得到的博文清单的列表是 CSV 格式。在Windows下,使用EXCEL打开该文件。

    1.1.1 Excel文件打开

      但是直接使用Excel打开,看到:

    • 表格包含三列:url,title,status
    • title的中文字符都出现乱码;
      ▲ 图1.1.1 EXCEL打开出现的乱码
      ▲ 图1.1.1 EXCEL打开出现的乱码

      这应该是文件存储时的编码出现问题。

    1.1.2 利用Python读取

    (1)读取程序

    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    
    filename = r'D:\Temp\卓晴老师全部文章列表.csv'
    
    with open(filename, 'r') as f:
        for l in f.readlines():
            print(l)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (2)执行结果

      直接读取出现解码错误。

    Traceback (most recent call last):
    File "<pyshell#6>", line 2, in <module>
    UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 98: illegal multibyte sequence
    
    • 1
    • 2
    • 3

      根据 关于python内open函数encoding编码问题 提到的经验,在打开文件中增加encoding='utf-8'可以解决这个情况。

    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    
    filename = r'D:\Temp\卓晴老师全部文章列表.csv'
    
    with open(filename, 'r', encoding='utf-8') as f:
        for l in f.readlines():
            print(l)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

      这样便可以获得正确的输出信息了:

      下面是部分输出的信息。

    "https://blog.csdn.net/zhuoqingjoking97298/article/details/122526980","为什么LED内部不集成限流电阻呢?","1"
    
    "https://blog.csdn.net/zhuoqingjoking97298/article/details/122530436","什么是微分?什么是导数?如何利用微分-导数方程求导数?","1"
    
    "https://blog.csdn.net/zhuoqingjoking97298/article/details/122531852","numpy中计算矩阵数值的核心函数","1"
    
    "https://blog.csdn.net/zhuoqingjoking97298/article/details/122535149","第十六届全国大学生智能车竞赛相关照片","1"
    
    "https://blog.csdn.net/zhuoqingjoking97298/article/details/122537709","清华大学本科生微积分2021年期末考试试卷","1"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

      可以得到2616条博文记录信息。

    1.1.3 利用numpy读取CSV

      根据python-numpy数组的csv文件写入与读取 介绍的方法,利用NumPy读取CSV文件。

      但是直接读取会存在问题:

    Traceback (most recent call last):
    File "<pyshell#44>", line 1, in <module>
    File "C:\Users\zhuoqing\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1112, in loadtxt
    UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 98: illegal multibyte sequence
    
    • 1
    • 2
    • 3
    • 4

    1.1.4 利用csv读写文件

    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    import csv
    
    filename = r'D:\Python\Cmd\database\csdnlist-2022-1-18.csv'
    
    with open(filename, 'r', encoding='utf-8') as f:
        csv_reader = csv.reader(f, delimiter=',')
    
        for count,l in enumerate(csv_reader):
            print("l: {}\n".format(l))
    
            if count >= 10: break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    l: ['url', 'title', 'status']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064392', '紧急制动', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064573', '大脑里的电极', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064593', '数码管识别', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064723', '劈-I-D', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064893', 'CSDN上究竟可以上载多大的GIF文件?', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104072612', '使用AD8302进行检波', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104083055', '三极管饱和状态下增益', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104085992', '减速电机JGA25-370的控制电路', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104089780', '机械变阻器、电位器', '1']
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104092963', '手持红外温度计AR802B', '1']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

      可以看到使用CSV读取csv文件非常方便,它可以自动将读入的数据进行有效的分割。

    1.2 文章检索

    1.2.1 基础数据文件

      将所得到的文件另外存储到:

    filename = r'D:\Python\Cmd\database\csdnlist-2022-1-18.csv'
    
    • 1

      读取这个数据文件所需要的时间大约为0.3570秒;

    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    
    filename = r'D:\Python\Cmd\database\csdnlist-2022-1-18.csv'
    
    count = 0
    
    starttime = time.time()
    
    with open(filename, 'r', encoding='utf-8') as f:
        for l in f.readlines():
            count +=1
    
    costtime = time.time() - starttime
    print("count: {}\n".format(count), "costtime: {}\n".format(costtime))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    count: 2616
    costtime: 0.35702037811279297
    
    • 1
    • 2

    1.2.2 中文分词

      为了提高检索的效率,在传统的字符串匹配上,将所有的文章的title进行中文分词,以提高分词匹配的加权,可以提高匹配的中文理解能力。

      在知乎 中文分词方法和软件工具汇总笔记 中汇总了中文分词的的方法和软件工具。之前我只知道 jieba分词 ,想不到现在可以使用的工具这么多呢。不过还是首先测试一下jieba分词对于所有引文标题的分词效果。

    (1)JieBa分词

      下面就按照jieba分词中介绍的方法进行实验。

     Ⅰ.安装JieBa
    pip3 install jieba
    
    • 1
     Ⅱ.测试分词效果
    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    import csv
    import jieba
    
    filename = r'D:\Python\Cmd\database\csdnlist-2022-1-18.csv'
    
    with open(filename, 'r', encoding='utf-8') as f:
        csv_reader = csv.reader(f, delimiter=',')
    
        for count,l in enumerate(csv_reader):
            seg = '-'.join(jieba.cut(l[1]))
            print("seg: {}\n".format(seg))
    
            if count >= 20: break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    seg: title
    seg: 紧急制动
    seg: 大脑---电极
    seg: 数码管-识别
    seg:---I---D
    seg: CSDN--究竟-可以-上载-多大--GIF-文件-?
    seg: 使用-AD8302-进行-检波
    seg: 三极管-饱和状态--增益
    seg: 减速-电机-JGA25---370--控制电路
    seg: 机械-变阻器--电位器
    seg: 手持-红外-温度计-AR802B
    seg: 什么-数字-万用表-可以-测量-噪声-?
    seg: 测试-可编程-波形发生器- -AD9833
    seg: AD9833-数字信号-发生器-模块
    seg: 使用-AD9833-谐波-发送-调频-广播
    seg: RDA5807- -FM-收音机-模块
    seg: Hands---On- -Learning- -Through- -Racing
    seg: 脑残--猪尾-汇
    seg: 挑战杯-科展---智能--作品
    seg:-简至-美
    seg: 一分钟-制版-
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

      虽然看起来上面分词的效果不咋地,但利用这个结果还是可以在一定程度上提高对于博文检索的精确性的。

      关于JieBa的详细使用方法,在简书的博文 jieba分词详解 中给出了详细的介绍。

    1.2.3 处理CSV文件

      由于动态调用JieBa分词需要一定时间,因此将原始的CSV文件进行预处理,将所有的标题分词之后进行存储,这样后期处理便可以直接利用分词后的标题进行检索的。

    (1)转换文件格式

    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    import csv
    import jieba
    
    filename = r'D:\Python\Cmd\database\csdnlist-2022-1-18.csv'
    outfile = r'D:\Python\Cmd\database\csdnlist-2022-1-18-jieba.csv'
    
    with open(filename, 'r', encoding='utf-8') as f:
        csv_reader = csv.reader(f, delimiter=',')
    
        with open(outfile, 'w') as outf:
            out_write = csv.writer(outf, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
    
            for count,l in enumerate(csv_reader):
                seg = '`'.join(jieba.cut(l[1]))
                try:
                    out_write.writerow([l[0], seg])
                except:
                    pass
    
            print('Process {} lines'.format(count))
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

      最终获得2615行处理后的数据。其中有一条在out_write.writerow的过程中出现中文编码错误。最终的结果存储在csdnlilst-2022-1-18-jieba.csv中。

        csdnlist-2022-1-18-jieba.csv
        csdnlist-2022-1-18.csv
    
    • 1
    • 2

    (2)检查处理结果

      注意,由于上面在生成新的CSV文件的时候,没有使用 encoding='utf-8'编码的方式,所以在下面再打开CSV文件的时候,不需要在使用encoding='utf-8'编码方式了。

    import sys,os,math,time
    import matplotlib.pyplot as plt
    from numpy import *
    import csv
    import jieba
    
    filename = r'D:\Python\Cmd\database\csdnlist-2022-1-18-jieba.csv'
    
    with open(filename, 'r') as f:
        csv_reader = csv.reader(f, delimiter=',')
    
        for count,l in enumerate(csv_reader):
            print("l: {}\n".format(l))
            if count >= 10: break
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    l: ['url', 'title']
    l: []
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064392', '紧急制动']
    l: []
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064573', '大脑`里`的`电极']
    l: []
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064593', '数码管`识别']
    l: []
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064723', '劈`-`I`-`D']
    l: []
    l: ['https://blog.csdn.net/zhuoqingjoking97298/article/details/104064893', 'CSDN`上`究竟`可以`上载`多大`的`GIF`文件`?']
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

      从上面的测试结果输出的情况来看,在生成的文件中,出现了一半的空行!

      使用EXCEL打开CSV文件,可以查看到在文件中的确每隔一行出现了一个空行,但写入的纵列没有变。

    ▲ 图1.2.1 使用EXCEL打开CSV文件

    ▲ 图1.2.1 使用EXCEL打开CSV文件

      这个问题后面经过初步查找并没有得到具体的解释。不过这不影响之后的应用,对此也就不再进行追查了。

    空行问题解决:

    小小明-代码实体:csv模块导出的文件产生多余换行的原因是默认输出的换行符是\r\n,只需要修改换行符为\n即可解决更问题,参考:csv.writer(f, lineterminator=’\n’)

     

    §02 文检索


    2.1 检索方案

      编写一个检测Python程序,参数是检索的关键词。可以是多个关键词。通过匹配博文的标题进行打分。对于多个关键词,它们的打分值是累加的。

      对于单个关键词打分的标准:

    • 如果能够完全匹配一个分词:+10分;多个分词则继续累加;
    • 如果能够在一个分词内进行匹配:+5分;多个进行累加;
    • 如果能够在整句话中进行整词的匹配:+3分;多个进行累加;
    • 对于每个字进行匹配:+1分
    • 对于得分相同,在原来序列日期近的靠近,所以按照原来的顺序id, 增加一个小数: id/10000.0

      为了突出关键词重要性,如果一个关键词后面携带句号(’.’,’。’)这个关键得分乘以10。

    2.1.1 检索算法

    from headm import *                 # =
    import csv
    
    csdnlist = r'D:\Python\Cmd\database\csdnlist-2022-1-18-jieba.csv'
    
    if len(sys.argv) == 1:
        printf("cdtm key[s]\a")
        exit()
    
    def matchScore(line):
        score = 0
        lsect = line.split('`')
        lstr = line.replace('`', '')
    
        for k in sys.argv[1:]:
            keyscore = 0
            ktime = 1
            if k.count('.') > 0:
                ktime = 10 * k.count('.')
                k = k.replace('.', '')
    
            if k.count('。') > 0:
                ktime = 10 * k.count('。')
                k = k.replace('。', '')
    
            for s in lsect:
                if s == k:              keyscore += 10
                elif s.find(k) >= 0:    keyscore += 5
    
            if keyscore == 0:
                if lstr.find(k) >= 0:  keyscore += 3
                else:
                    for c in k:
                        if lstr.find(c) >= 0: keyscore += 1
    
            score += keyscore * ktime
    
        return score
    
    urldim = []
    titledim = []
    urlscore = []
    
    with open(csdnlist, 'r') as f:
        csvr = csv.reader(f, delimiter=',')
        for count,l in enumerate(csvr):
            if count == 0: continue     # First row of csv is title row
            if len(l) == 0: continue    # Empty row
    
            urldim.append(l[0])
            titledim.append(l[1].replace('`', ''))
            urlscore.append(matchScore(l[1])+count/10000.0)
    
    alldim = [(s,u,t) for s,u,t in zip(urlscore, urldim, titledim)]
    
    alldim = sorted(alldim, key=lambda x:x[0], reverse=True)
    
    for i in range(20):
        printt(alldim[i][0], alldim[i][2])
    
    • 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

    2.2 检测测试

      下面给出一些基本检测测试结果,看是否与检索意图能够相互匹配。

    • 规则

      10.5068 第十七届全国大学生智能车竞赛智能视觉组规则补充说明(1)
      10.4046 第十六届全国大学生智能汽车竞赛 讯飞智慧餐厅 全国总决赛竞赛规则
      10.4002 关于第十六届全国大学生智能汽车竞赛总决赛的规则建议
      10.3882 第十六 届全国大学生智能汽车竞赛 讯飞创意组 全国 选拔赛 竞赛规则
      10.388 讯飞智慧餐厅关于规则与赛程的通知
      10.2872 全国大学生智能汽车竞赛-讯飞赛道规则解读
      10.1828 关于2020年第十五届全国大学生智能汽车竞赛山东赛区技术答辩的相关规则
      10.1682 聚沙成塔 : 第十六届智能车竞赛规则你一言,我一语
      10.1074 电子硬件黑客规则
      10.094 STC单片机的命名规则
      10.0852 第十四届智能车竞赛规则浅聊
      10.0608 第十五届全国大学生智能汽车竞赛竞速赛规则(讨论稿)
      10.042 第十四届智能车竞赛规则浅聊
      10.0414 竞赛规则补充说明 | 无线节能组车模
      5.4928 第十七届全国大学生智能汽车竞赛创意组-百度智慧交通 “丝绸之路”比赛规则
      5.4682 第十七届智能车竞赛竞速比赛规则修订文档-一览表
      5.464 第十七届全国大学智能汽车竞赛竞速比赛规则
      5.2346 第十六届全国大学智能汽车竞赛竞速比赛规则
      5.0412 轻磅消息 | 室外光电竞速创意比赛规则
      1.5142 一个中等规模的七段数码数据库以及利用它训练的识别网络

    • STC

      10.4764 有STC制作一个手持微型示波器
      10.475999999999999 第十七届全国大学智能车竞赛STC芯片申请方法
      10.3348 直播预告 | STC单车拉力组专题培训
      10.2364 普大喜奔:智能车竞赛STC 16位、8位单片机免费样品申请开始啦!
      10.1864 STC单片机功率控制下载板
      10.1086 STC单片机高速下载电路改进
      10.1038 制作新版STC单片机WiFi下载器
      10.1014 通过WiFi对STC单片机程序下载和调试
      10.0992 STC自动高速下载线
      10.0954 STC单片机自动下载调试器设计
      10.094 STC单片机的命名规则
      10.0938 测试几款STC下载电路
      10.0936 全自动STC下载电路设计
      10.0932 STC单片机下载实验
      10.087 关于第十五届全国大学生智能车竞赛 STC 单片机
      5.1924 基于STC8H1K28的人机键盘界面
      5.175 双轴机械臂位置闭环控制:STC8H1K28,42HS48EIS,BH32
      5.1604 双轴机械臂调试:步进电机42HS348E,BH32角度传感器,MCU:STC8H1K28
      5.1596 基于STC8H1K28的双轴机械臂驱动模块:步进电机42HS348E,BH32角度传感器
      5.1526 设计基于MAX1240,MAX5353的ADDC模块STC8G1KSOP8

    2.3 信息插入CSDN

      下面,将上面的检索程序补充完全,也就是将检索的信息直接添加CSDN的Markdown的编辑器。如果在查询之前CSDN有选择文字,则使用改文字作为插入的TITLE。

      在下面针对以下名词进行检索:十七届竞赛通知电灯单片机人工智能微积分信号与系统

     

      结 ※


      用CSDN提供的博文的列表,编写了一个快速检索自己博文,并自动插入索引连接的程序。这样就可以大大提高博文写作的效率,将之前记录学习、工作的内容更好的进行连接。

      最后,感谢CSDN技术人员的大力支持和帮助。

    3.1 csdm程序

    #!/usr/local/bin/python
    # -*- coding: gbk -*-
    #============================================================
    # CDTM.PY                      -- by Dr. ZhuoQing 2022-01-18
    #   cdtm keys >title                #
    #        keys :title                #
    #        keys :title.               # only copy the url to clipboard
    #        keys :title;               # Open URL on chrome
    #        backup                     # backup the
    #        ?                         # Display last frame
    # Note:
    #============================================================
    
    from headm import *                 # =
    import csv
    
    csdn_title = '写文章-CSDN博客'
    csdnlist = r'D:\Python\Cmd\database\csdnlist-2022-1-18-jieba.csv'
    backupdir = r'D:\Nutdisk\Temp'
    
    #------------------------------------------------------------
    rect = tspgetwindowrect("HELP")
    linenum = int((rect[3]-rect[1]-20)//rect[4]*0.7-3)*4//5
    #------------------------------------------------------------
    
    inserttitle = ''
    keydim = []
    urlflag = 0
    openflag = 0
    searchandflag = 0
    
    #------------------------------------------------------------
    if len(sys.argv) > 1:
        if sys.argv[1] == 'backup':
            outfile = os.path.join(backupdir, os.path.basename(csdnlist))
            printf(outfile)
            import shutil
            shutil.copyfile(csdnlist, outfile)
            printf('\a')
            exit()
        elif sys.argv[1] in '??.':
            with open(csdnlist, 'r') as f:
                csvr = csv.reader(f, delimiter=',')
                titledim = []
                for count,l in enumerate(csvr):
                    if count == 0: continue     # First row of csv is title row
                    if len(l) < 2: continue    # Empty row
                    if len(l[0]) == 0: continue
                    if len(l[1]) == 0: continue
                    titledim.append(l[1])
    
                printf('Total Record:%d'%len(titledim))
                for i in range(linenum+3):
                    s = titledim[-i-1]
                    printf("%02d: %s"%(i+1, s.replace('`', '')))
    
            printf("\a")
    
            exit()
    
    #------------------------------------------------------------
    if len(sys.argv) == 1:
        urlstr = clipboard.paste()
    
        if urlstr.find('http') < 0:
            titlestr = urlstr
            urlstr = tspread()[-1]
        else:
            titlestr = tspread()[-1]
    
        if urlstr.find('http') >= 0 and len(titlestr) > 0:
            with open(csdnlist, 'r') as f:
                csvr = csv.reader(f, delimiter=',')
                titledim = []
                urldim = []
                for count,l in enumerate(csvr):
                    if count == 0: continue     # First row of csv is title row
                    if len(l) < 2: continue    # Empty row
                    if len(l[0]) == 0: continue
                    if len(l[1]) == 0: continue
                    titledim.append(l[1].replace('`',''))
                    urldim.append(l[0])
    
    
            if urlstr in urldim and titlestr in titledim:
                printf("URL has already been added into record files.\a")
                for i in range(linenum):
                    printf("%02d: %s"%(i+1, titledim[-i-1]))
            else:
                import jieba
                seg = '`'.join(jieba.cut(titlestr))
                with open(csdnlist, 'a') as f:
                    insertstr = '"%s","%s"\n'%(urlstr, seg)
                    f.write(insertstr)
    
    
                printf("\r\nAdd record: \r\n    %s\r\n    %s\r\n\a"%(titlestr, urlstr))
                for i in range(linenum):
                    printf("%02d: %s"%(i+1, titledim[-i-1]))
    
            clipboard.copy('')
    
            exit()
    
    #------------------------------------------------------------
    for s in sys.argv[1:]:
        if s[0] in ':>|:':
            inserttitle = s[1:]
            if s[0] == '>':
                urlflag = 1
        else:
            if s[-1] in ':>|:':
                s = s[:-1]
                searchandflag = 1
    
            if len(s) > 0:
                keydim.append(s)
    
    
    #------------------------------------------------------------
    if len(inserttitle) == 0:
        clipboard.copy('')
        tspsendwindowkey(csdn_title, "c", control=1, noreturn=1)
        time.sleep(.1)
        inserttitle = clipboard.paste()
    
        if len(inserttitle) > 0:
            keydim.append(inserttitle)
    
    #------------------------------------------------------------
    if len(keydim) == 0:
        printf("cdtm key[s]\a")
        exit()
    
    
    #------------------------------------------------------------
    def matchScore(line, count=0):
        global keydim
    
        #--------------------------------------------------------
        score = 0
        lsect = line.split('`')
        lstr = line.replace('`', '')
    
        #--------------------------------------------------------
        for k in keydim:
            keyscore = 0
            ktime = 1
            if k.count('.') > 0:
                ktime = 10 * k.count('.')
                k = k.replace('.', '')
    
            if k.count('。') > 0:
                ktime = 10 * k.count('。')
                k = k.replace('。', '')
    
            #----------------------------------------------------
            for s in lsect:
                if s.upper() == k.upper():              keyscore += 10
                elif s.upper().find(k.upper()) >= 0:    keyscore += 5
    
    
            if keyscore == 0:
                if lstr.upper().find(k.upper()) >= 0:  keyscore += 3
                else:
                    for c in k:
                        if lstr.find(c) >= 0: keyscore += 1
    
            if searchandflag > 0:
                if keyscore == 0: return 0
    
            score += keyscore * ktime
    
        return score + count/10000.0
    
    #------------------------------------------------------------
    alldim = []
    
    
    #------------------------------------------------------------
    def searchTitle():
    #    global urldim,titledim,urlscore
        global alldim
    
        urldim = []
        titledim = []
        urlscore = []
    
        with open(csdnlist, 'r') as f:
            csvr = csv.reader(f, delimiter=',')
            for count,l in enumerate(csvr):
                if count == 0: continue     # First row of csv is title row
                if len(l) < 2: continue    # Empty row
    
                urldim.append(l[0])
                titledim.append(l[1].replace('`', ''))
                urlscore.append(matchScore(l[1], count))
    
    
        alldim = [(s,u,t,n) for s,u,t,n in zip(urlscore, urldim, titledim, list(range(len(urlscore))))]
        alldim = sorted(alldim, key=lambda x:x[0], reverse=True)
    
        score = array([a[0] for a in alldim])
        return (score > 1.0).tolist().count(True)
    
    
    #------------------------------------------------------------
    searchnum = searchTitle()
    
    
    #------------------------------------------------------------
    selectid = -1
    frame = 0
    numstr = '1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    #numstr = '1234567890!@#$%^&*()abcdefghijklmnopqrstuvwxyz'
    frame_length = len(numstr)
    copyflag = 0
    
    if frame_length > linenum: frame_length = linenum
    
    tspfocuswindow('TEASOFT:1')
    
    
    #------------------------------------------------------------
    
    while True:
        startid = frame
        endid = startid + frame_length
        if startid < 0: startid = 0
        if endid < 0:   endid = 0
        if startid >= len(alldim):  startid = len(alldim)
        if endid >= len(alldim):    endid = len(alldim)
        if endid == startid: break
    
        if searchandflag == 0:
            searchstr = '|'.join(keydim)
        else:
            searchstr = '&'.join(keydim)
    
        hint = '[%02d-%02d](%d):%s\r\n--------------------------------\r\n'%(startid, endid, searchnum, searchstr)
    
        for i in range(startid, endid):
            id = i - startid
            if id >= searchnum: break
            idstr = numstr[id]
            lines = '%s: %s(%.3f/%d)\r\n'%(idstr, alldim[i][2], alldim[i][0], len(alldim)-alldim[i][3]+1)
    
            hint = hint + lines
    
        printf('')
        printf(hint)
        key = tspinput("Input Number and string:")
    
        #--------------------------------------------------------
        if key == '\r':
            frame += len(numstr)
            if frame + frame_length >= searchnum:
                frame = searchnum - frame_length
                if frame < 0:
                    frame = 0
    
            continue
    
        if len(key.replace(' ','').replace('\r','')) == 0:
            spacenum = key.count(' ')
            if spacenum > 0:
                frame -= spacenum * frame_length
                if frame < 0: frame = 0
                continue
    
        #--------------------------------------------------------
        if key == 'ESC':
            break
    
        key = key.strip('\r')
        if key[-1] in '.。':
            key = key[:-1]
            copyflag = 1
    
        if key[-1] in ';;':
            key = key[:-1]
            openflag = 1
    
        if key[-1] in ',,':
            key = key[:-1]
            if len(key)== 1:
                num = numstr.find(key[0])
    
            if num >= 0:
                selectid = frame + num
    
            chrome_path = r'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
            import webbrowser
            webbrowser.get(chrome_path).open_new(alldim[selectid][1])
            printf("Chrom open:%s"%alldim[selectid][1])
            continue
    
    
        #--------------------------------------------------------
        id = key.find(':')
        if id < 0:
            id = key.find(':')
    
        if id >= 0:
            inserttitle = key[id+1:]
            key = key[:id]
    
        num = -1
        if len(key)== 1:
            num = numstr.find(key[0])
    
        if num >= 0:
            selectid = frame + num
            break
        else:
            ssdim = [ss.strip('\r') for ss in key.split(' ') if len(ss) > 0]
            searchandflag = 0
    
            ss = key
            if ss.find('&') >= 0 or ss.find(':') >= 0 or \
               ss.find('|') >= 0 or ss.find('>') >= 0 or \
               ss.find(':') >= 0 or ss.find('\\') >= 0 or\
               ss.find('、') >= 0 or ss.find('/') >= 0:
                pass
            else:
                keydim = []
    
            for s in ssdim:
                if s[-1] in '&:|>:\\、/':
                    if s[-1] == '&':
                        searchandflag = 1
                    s = s[:-1]
                elif s[0] in '&:|>:、\\/':
                    if s[0] == '&':
                        searchandflag = 1
                    s = s[1:]
    
                if len(s) > 0: keydim.append(s)
    
            rect = tspgetwindowrect("HELP")
            linenum = int((rect[3]-rect[1]-20)//rect[4]*0.7-3)*4//5
            searchnum = searchTitle()
            frame = 0
    
    
    
    #printf(selectid, copyflag)
    #------------------------------------------------------------
    if selectid >= 0:
        insertstr = ''
        if len(inserttitle) == 0:
            if urlflag == 0:
                inserttitle = alldim[selectid][2]
                insertstr = '[**%s**](%s)'%(inserttitle, alldim[selectid][1])
            else: insertstr = '<%s>'%alldim[selectid][1]
        else:
            insertstr = '[**%s**](%s)'%(inserttitle, alldim[selectid][1])
            if urlflag > 0:
                insertstr = insertstr + '<%s>'%alldim[selectid][1]
    
    
        if copyflag == 0 and openflag == 0:
            if len(insertstr) > 0:
                clipboard.copy(insertstr)
                time.sleep(.05)
                tspsendwindowkey(csdn_title, "v", control=1,noreturn=1)
        elif openflag == 1:
            chrome_path = r'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
            import webbrowser
            webbrowser.get(chrome_path).open_new(alldim[selectid][1])
            printf("Chrom open:%s"%alldim[selectid][1])
        else:
            clipboard.copy(alldim[selectid][1])
            printf("Clipobard:%s"%alldim[selectid][1])
    
    
    else:
        tspfocuswindow('TEASOFT:1')
    
    
    #printf('\a')
    #------------------------------------------------------------
    #        END OF FILE : CDTM.PY
    #============================================================
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 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
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 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
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 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
    • 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
    • 383
    • 384


    ■ 相关文献链接:

    ● 相关图表链接:

  • 相关阅读:
    git 学习总结
    Java 中的八大基本数据类型、类型转换
    JS的this关键字详解
    自举电容充电回路分析
    m基于自适应门限软切换的3G和Wifi垂直切换算法的matlab仿真
    Pytorch 基于ResNet-18的服饰识别(使用Fashion-MNIST数据集)
    创建对象方式
    AtCoder Beginner Contest 345 A - E 题解
    STL教程4-输入输出流和对象序列化
    正则表达式快速入门
  • 原文地址:https://blog.csdn.net/zhuoqingjoking97298/article/details/122553999