1. 程式人生 > 其它 >Python 實驗課 jieba和def

Python 實驗課 jieba和def

技術標籤:python 基礎python列表自然語言處理字串

1

import jieba
import jieba.posseg as psg
f = open('李白的詩.txt','r',encoding='utf-8')
lbPoetry=f.read()
f.close()
lst_poetry_word=psg.lcut(lbPoetry)
dic_one_noun={}
dic_two_noun={}
for x in lst_poetry_word:
    if len(x.word)==1 and x.flag=='n':
        dic_one_noun[
x.word]=dic_one_noun.get(x.word,0)+1 if len(x.word)==2 and x.flag=='n': dic_two_noun[x.word]=dic_two_noun.get(x.word,0)+1 result_one_noun=sorted(dic_one_noun.items(),key=lambda x:x[1],reverse=True) result_one_noun=[x[0]+str(x[1])+'\n' for x in result_one_noun[:10]] result_two_noun=sorted(dic_two_noun.
items(),key=lambda x:x[1],reverse=True) result_two_noun=[x[0]+str(x[1])+'\n' for x in result_two_noun[:10]] f=open('李白高頻詞.txt','w',encoding='utf-8') f.writelines(result_one_noun) f.writelines(result_two_noun) f.close()

2

import jieba
s='外觀很好,畫質也不錯,但是音質真的太糟糕了!操作也不方便。'
positive_lst=['好','不錯','方便','贊']
negtive_lst=
['糟糕','爛'] degree_dict={'很':2,'太':3,'非常':2,'較':1,'極':3,'無比':4} lst=jieba.lcut(s) positive_words=0 negtive_words=0 print(lst) for i in range(len(lst)): if lst[i] in positive_lst: if lst[i-1]!='不': positive_words+=1*degree_dict.get(lst[i-1],1) else: negtive_words+=-1*degree_dict.get(lst[i-1],1) elif lst[i] in negtive_lst: if lst[i-1]!='不': negtive_words+=-1*degree_dict.get(lst[i-1],1) else: positive_words+=1*degree_dict.get(lst[i-1],1) total=positive_words+negtive_words result='積極情感' if total>=0 else '消極情感' print('情感分為:{}分,情感分析結果為:{}'.format(total,result))

3

####請在以下空白處定義函式
def f(n):
    lst=[]
    for i in range(1,n+1):
        if i%11==0 or ('9' in str(i)):
            lst.append(i)
    return lst


####請在以上空白處定義函式

a=int(input())
gifts=f(a)
print("禮物份數:",len(gifts))
print("應發放禮物的序號:",end=' ')
for i in gifts: 
    print(i,end=' ')


4

####請在以下空白處定義函式
def isLucky(s,digit):
    if s[-2:-1]==digit:
        count=s.count(digit)
        score=count/len(s)*1.2
    else:
        count=s.count(digit)
        score=count/len(s)*1
    if score>=0.6:
        return True
    else:
        return False




####請在以上空白處定義函式

s, digit = input().split()
if isLucky(s, digit):
    print('Lucky!')
else:
    print('So so.')


5

m=input()
count=0
while count<=20:
    m=str(int(m)+int(m[-1::-1]))
    count+=1
    if m==m[-1::-1]:
        print(count)
        break
else:
    print(">20")