本文目錄一覽:
百度AI新聞摘要python可以怎麼寫
完整代碼
from collections import OrderedDict
import numpy as np
import spacy
from spacy.lang.en.stop_words import STOP_WORDS
nlp = spacy.load(‘en_core_web_sm’)
class TextRank4Keyword():
“””Extract keywords from text”””
def __init__(self):
self.d = 0.85 # damping coefficient, usually is .85
self.min_diff = 1e-5 # convergence threshold
self.steps = 10 # iteration steps
self.node_weight = None # save keywords and its weight
def set_stopwords(self, stopwords):
“””Set stop words”””
for word in STOP_WORDS.union(set(stopwords)):
lexeme = nlp.vocab[word]
lexeme.is_stop = True
def sentence_segment(self, doc, candidate_pos, lower):
“””Store those words only in cadidate_pos”””
sentences = []
for sent in doc.sents:
selected_words = []
for token in sent:
# Store words only with cadidate POS tag
if token.pos_ in candidate_pos and token.is_stop is False:
if lower is True:
selected_words.append(token.text.lower())
else:
selected_words.append(token.text)
sentences.append(selected_words)
return sentences
def get_vocab(self, sentences):
“””Get all tokens”””
vocab = OrderedDict()
i = 0
for sentence in sentences:
for word in sentence:
if word not in vocab:
vocab[word] = i
i += 1
return vocab
def get_token_pairs(self, window_size, sentences):
“””Build token_pairs from windows in sentences”””
token_pairs = list()
for sentence in sentences:
for i, word in enumerate(sentence):
for j in range(i+1, i+window_size):
if j = len(sentence):
break
pair = (word, sentence[j])
if pair not in token_pairs:
token_pairs.append(pair)
return token_pairs
def symmetrize(self, a):
return a + a.T – np.diag(a.diagonal())
def get_matrix(self, vocab, token_pairs):
“””Get normalized matrix”””
# Build matrix
vocab_size = len(vocab)
g = np.zeros((vocab_size, vocab_size), dtype=’float’)
for word1, word2 in token_pairs:
i, j = vocab[word1], vocab[word2]
g[i][j] = 1
# Get Symmeric matrix
g = self.symmetrize(g)
# Normalize matrix by column
norm = np.sum(g, axis=0)
g_norm = np.divide(g, norm, where=norm!=0) # this is ignore the 0 element in norm
return g_norm
def get_keywords(self, number=10):
“””Print top number keywords”””
node_weight = OrderedDict(sorted(self.node_weight.items(), key=lambda t: t[1], reverse=True))
for i, (key, value) in enumerate(node_weight.items()):
print(key + ‘ – ‘ + str(value))
if i number:
break
def analyze(self, text,
candidate_pos=[‘NOUN’, ‘PROPN’],
window_size=4, lower=False, stopwords=list()):
“””Main function to analyze text”””
# Set stop words
self.set_stopwords(stopwords)
# Pare text by spaCy
doc = nlp(text)
# Filter sentences
sentences = self.sentence_segment(doc, candidate_pos, lower) # list of list of words
# Build vocabulary
vocab = self.get_vocab(sentences)
# Get token_pairs from windows
token_pairs = self.get_token_pairs(window_size, sentences)
# Get normalized matrix
g = self.get_matrix(vocab, token_pairs)
# Initionlization for weight(pagerank value)
pr = np.array([1] * len(vocab))
# Iteration
previous_pr = 0
for epoch in range(self.steps):
pr = (1-self.d) + self.d * np.dot(g, pr)
if abs(previous_pr – sum(pr)) self.min_diff:
break
else:
previous_pr = sum(pr)
# Get weight for each node
node_weight = dict()
for word, index in vocab.items():
node_weight[word] = pr[index]
self.node_weight = node_weight
為什麼做AI的都選Python?
相對於其他語言:
1、更加人性化的設計
Python的設計更加人性化,具有快速、堅固、可移植性、可擴展性的特點,十分適合人工智能;開源免費,而且學習簡單,很容易實現普及;內置強大的庫,可以輕鬆實現更大強大的功能。
2、總體的AI庫
AIMA:Python實現了從Russell到Norvigs的「人工智能:一種現代的方法」的算法;
pyDatalog:Python中的邏輯編程引擎;
SimpleAI:Python實現在「人工智能:一種現代的方法」這本書中描述過的人工智能的算法,它專註於提供一個易於使用,有良好文檔和測試的庫;
EasyAI:一個雙人AI遊戲的python引擎。
3、機器學習庫
PyBrain 一個靈活,簡單而有效的針對機器學習任務的算法,它是模塊化的Python機器學習庫,它也提供了多種預定義好的環境來測試和比較你的算法;
PyML 一個用Python寫的雙邊框架,重點研究SVM和其他內核方法,它支持Linux和Mac OS X;
scikit-learn旨在提供簡單而強大的解決方案,可以在不同的上下文中重用:機器學習作為科學和工程的一個多功能工具,它是python的一個模塊,集成了經典的機器學習的算法,這些算法是和python科學包緊密聯繫在一起的;
MDP-Toolkit這是一個Python數據處理的框架,可以很容易的進行擴展。它海收集了有監管和沒有監管的學習算飯和其他數據處理單元,可以組合成數據處理序列或者更複雜的前饋網絡結構。新算法的實現是簡單和直觀的。可用的算法是在不斷的穩定增加的,包括信號處理方法,流型學習方法,集中分類,概率方法,數據預處理方法等等。
4、自然語言和文本處理庫
NLTK開源的Python模塊,語言學數據和文檔,用來研究和開發自然語言處理和文本分析,有windows、Mac OSX和Linux版本。
Python具有豐富而強大的庫,能夠將其他語言製作的各種模塊很輕鬆的聯結在一起,因此,Python編程對人工智能是一門非常有用的語言。可以說人工智能和Python是緊密相連的。如果你想要抓住人工智能的風口,Python是必不可少的助力。
人工智能上使用Python比其他編程語言的好處
1、優質的文檔
2、平台無關,可以在現在每一個*nix版本上使用
3、和其他面向對象編程語言比學習更加簡單快速
4、Python有許多圖像加強庫像Python Imaging Libary,VTK和Maya 3D可視化工具包,Numeric Python, Scientific Python和其他很多可用工具可以於數值和科學應用。
5、Python的設計非常好,快速,堅固,可移植,可擴展。很明顯這些對於人工智能應用來說都是非常重要的因素。
6、對於科學用途的廣泛編程任務都很有用,無論從小的shell腳本還是整個網站應用。
7、它是開源的。可以得到相同的社區支持。
AI的Python庫
一、總體的AI庫
AIMA:Python實現了從Russell到Norvigs的「人工智能:一種現代的方法」的算法
pyDatalog:Python中的邏輯編程引擎
SimpleAI:Python實現在「人工智能:一種現代的方法」這本書中描述過的人工智能的算法。它專註於提供一個易於使用,有良好文檔和測試的庫。
EasyAI:一個雙人AI遊戲的python引擎(負極大值,置換表、遊戲解決)
二、機器學習庫
PyBrain 一個靈活,簡單而有效的針對機器學習任務的算法,它是模塊化的Python機器學習庫。它也提供了多種預定義好的環境來測試和比較你的算法。
PyML 一個用Python寫的雙邊框架,重點研究SVM和其他內核方法。它支持Linux和Mac OS X。
scikit-learn 旨在提供簡單而強大的解決方案,可以在不同的上下文中重用:機器學習作為科學和工程的一個多功能工具。它是python的一個模塊,集成了經典的機器學習的算法,這些算法是和python科學包(numpy,scipy.matplotlib)緊密聯繫在一起的。
MDP-Toolkit 這是一個Python數據處理的框架,可以很容易的進行擴展。它海收集了有監管和沒有監管的學習算飯和其他數據處理單元,可以組合成數據處理序列或者更複雜的前饋網絡結構。新算法的實現是簡單和直觀的。可用的算法是在不斷的穩定增加的,包括信號處理方法(主成分分析、獨立成分分析、慢特徵分析),流型學習方法(局部線性嵌入),集中分類,概率方法(因子分析,RBM),數據預處理方法等等。
python百度雲AI語音合成參數怎麼改
參數說明
per :發音人選擇, 0為普通女聲,1為普通男生,3為情感合成-度逍遙,4為情感合成-度丫丫,默認為普通女聲
spd:語速,取值0-15,默認為5中語速
pit:音調,取值0-15,默認為5中語調
vol:音量,取值0-15,默認為5中音量
原創文章,作者:PKZO,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/143330.html