python的几个function

时间:2022-06-19
本文章向大家介绍python的几个function,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

image.png

# coding=gbk
import jieba


def getCutSent(sent,path,flag=False):
    if   path:  #自定义词典路径
        jieba.load_userdict(path)
    seg_list=jieba.cut(sent,cut_all=flag)  # 默认是精确模式

    cutScent="/".join(seg_list)
    # word_rank = jieba.analyse.extract_tags(cutScent, topK=20, withWeight=True, allowPOS=())
    print(cutScent)
    return  cutScent



# 此处使用snownlp模块对评论进行情感分析
from snownlp import SnowNLP

def getmark(comments):
    coms = []
    coms = comments.apply(lambda x: SnowNLP(x).sentiments)
    # 情感分析,coms在0~1之间,以0.5分界,大于0.5,则为正面情感
    pos_data = comments[coms >= 0.6]  # 此处取0.6是为了使的词的情感更强烈点
    neg_data = comments[coms < 0.4]  # 负面情感数据集
    pos_data[:5]





def  getZipStr(scent):  #压缩了句首
    temp1 = scent.strip('n')  # 去掉每行最后的换行符'n'
    temp2 = temp1.lstrip('ufeff')
    temp3 = temp2.strip('r')
    char_list = list(temp3)
    list1 = ['']
    list2 = ['']
    del1 = []
    flag = ['']
    i = 0
    while (i < len(char_list)):
        if (char_list[i] == list1[0]):
            if (list2 == ['']):
                list2[0] = char_list[i]
            else:
                if (list1 == list2):
                    t = len(list1)
                    m = 0
                    while (m < t):
                        del1.append(i - m - 1)
                        m = m + 1
                    list2 = ['']
                    list2[0] = char_list[i]
                else:
                    list1 = ['']
                    list2 = ['']
                    flag = ['']
                    list1[0] = char_list[i]
                    flag[0] = i
        else:
            if (list1 == list2) and (list1 != ['']) and (list2 != ['']):
                if len(list1) >= 2:
                    t = len(list1)
                    m = 0
                    while (m < t):
                        del1.append(i - m - 1)
                        m = m + 1
                    list1 = ['']
                    list2 = ['']
                    list1[0] = char_list[i]
                    flag[0] = i
            else:
                if (list2 == ['']):
                    if (list1 == ['']):
                        list1[0] = char_list[i]
                        flag[0] = i
                    else:
                        list1.append(char_list[i])
                        flag.append(i)
                else:
                    list2.append(char_list[i])
        i = i + 1
        if (i == len(char_list)):
            if (list1 == list2):
                t = len(list1)
                m = 0
                while (m < t):
                    del1.append(i - m - 1)
                    m = m + 1
                m = 0
                while (m < t):
                    del1.append(flag[m])
                    m = m + 1
    a = sorted(del1)
    t = len(a) - 1
    while (t >= 0):
        # print(char_list[a[t]])
        del char_list[a[t]]
        t = t - 1
    str1 = "".join(char_list)
    return str1.strip()  # 删除两边空格


def stopWordList(path):  #加载停用词表
    words=[i.strip() for i in open(path,'r',encoding='utf-8').readlines()]
    return  words




print("全模式",end=" ")
senlist="小米8手机还行吧,不过小米公司喜欢饥饿营销,这一点真不怎么喜欢。"
getCutSent(senlist,False,True)

print("精确模式",end=" ")

getCutSent(senlist,False)

tmp=jieba.cut_for_search(senlist)
cutScent = "/".join(tmp)
print("搜索引擎模式",cutScent)

com=u'犹豫了很久,还是决定购买小米8。虽然小米9临近发布,但是考虑到比较难抢,另外前期的货可能在品控方面也会存在一些问题,系统软件bug也会层出不穷,所以决定还是等一等。购买已经发布一年的小米8,价格已经降到位了,这个时候购买绝对是超值的。开机大小适中,手感不错,做工也很精致,我购买的是蓝色版本,拿在手上可以说是赏心悦目。以前对于刘海屏的手机无法接受,现在用上了一段时间,感觉已经适应了。屏幕解锁的速度非常快,即便是在夜间,由于有红外面部识别,所以也能够瞬间解锁,这一点非常好。骁龙845的处理器,搭配8g运行内存,运行速度杠杠的。128g的海量存储,足够容纳我拍摄的诸多风景照片。最后说说这款手机的拍摄,由于它主摄使用的是索尼imx363,因此成像素质还是不错的,加上后期的软件调校,所以说,无论是在白天光照条件好的情况下,还是在夜间,都能够获得不错的拍照体验。系统升级之后,还有了超级夜景功能,这更是大大提升了他的夜间成像效果。我对于超广角以及变焦这些功能倒并不是特别在意,因为有些手机摄像头虽然是多了,但其实并非共同成像,只是单镜头在发挥作用,所以它的成像效果和主摄是完全不能比的。总之,很满意。",'
getCutSent(com,False)


print("加载自定义词典",end=" ")
path="D:/selfdict.txt"
getCutSent(senlist,path)

st="不错不错不错一下买了三台。"
end=getZipStr(st)
# print(end)

stfile="D:/stop.txt"

for i in stopWordList(stfile):
    print(i)