pos機(jī)的專業(yè)知識,NLP 與 Python

 新聞資訊  |   2023-05-20 10:26  |  投稿人:pos機(jī)之家

網(wǎng)上有很多關(guān)于pos機(jī)的專業(yè)知識,NLP 與 Python的知識,也有很多人為大家解答關(guān)于pos機(jī)的專業(yè)知識的問題,今天pos機(jī)之家(m.dsth100338.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!

本文目錄一覽:

1、pos機(jī)的專業(yè)知識

pos機(jī)的專業(yè)知識

概括

積累了一兩周,好久沒做筆記了,今天,我將展示在之前兩周的實(shí)戰(zhàn)經(jīng)驗(yàn):如何使用 Python 和自然語言處理構(gòu)建知識圖譜。

網(wǎng)絡(luò)圖是一種數(shù)學(xué)結(jié)構(gòu),用于表示點(diǎn)之間的關(guān)系,可通過無向/有向圖結(jié)構(gòu)進(jìn)行可視化展示。它是一種將相關(guān)節(jié)點(diǎn)映射的數(shù)據(jù)庫形式。

知識庫是來自不同來源信息的集中存儲庫,如維基百科、百度百科等。

知識圖譜是一種采用圖形數(shù)據(jù)模型的知識庫。簡單來說,它是一種特殊類型的網(wǎng)絡(luò)圖,用于展示現(xiàn)實(shí)世界實(shí)體、事實(shí)、概念和事件之間的關(guān)系。2012年,谷歌首次使用“知識圖譜”這個術(shù)語,用于介紹他們的模型。

目前,大多數(shù)公司都在建立數(shù)據(jù)湖,這是一個中央數(shù)據(jù)庫,它可以收集來自不同來源的各種類型的原始數(shù)據(jù)(包括結(jié)構(gòu)化和非結(jié)構(gòu)化數(shù)據(jù))。因此,人們需要工具來理解所有這些不同信息的意義。知識圖譜越來越受歡迎,因?yàn)樗梢院喕笮蛿?shù)據(jù)集的探索和發(fā)現(xiàn)。簡單來說,知識圖譜將數(shù)據(jù)和相關(guān)元數(shù)據(jù)連接起來,因此可以用來構(gòu)建組織信息資產(chǎn)的全面表示。例如,知識圖譜可以替代您需要查閱的所有文件,以查找特定的信息。

知識圖譜被視為自然語言處理領(lǐng)域的一部分,因?yàn)橐獦?gòu)建“知識”,需要進(jìn)行“語義增強(qiáng)”過程。由于沒有人想要手動執(zhí)行此任務(wù),因此我們需要使用機(jī)器和自然語言處理算法來完成此任務(wù)。

我將解析維基百科并提取一個頁面,用作本教程的數(shù)據(jù)集(下面的鏈接)。

俄烏戰(zhàn)爭 - 維基百科 俄烏戰(zhàn)爭是俄羅斯與俄羅斯支持的分離主義者之間持續(xù)的國際沖突,以及...... en.Wikipedia.org

特別是將通過:

設(shè)置:使用維基百科API進(jìn)行網(wǎng)頁爬取以讀取包和數(shù)據(jù)。NLP使用SpaCy:對文本進(jìn)行分句、詞性標(biāo)注、依存句法分析和命名實(shí)體識別。提取實(shí)體及其關(guān)系:使用Textacy庫來識別實(shí)體并建立它們之間的關(guān)系。網(wǎng)絡(luò)圖構(gòu)建:使用NetworkX庫來創(chuàng)建和操作圖形結(jié)構(gòu)。時間軸圖:使用DateParser庫來解析日期信息并生成時間軸圖。設(shè)置

首先導(dǎo)入以下庫:

## for dataimport pandas as pd #1.1.5import numpy as np #1.21.0## for plottingimport matplotlib.pyplot as plt #3.3.2## for textimport wikipediaapi #0.5.8import nltk #3.8.1import re ## for nlpimport spacy #3.5.0from spacy import displacyimport textacy #0.12.0## for graphimport networkx as nx #3.0 (also pygraphviz==1.10)## for timelineimport dateparser #1.1.7

Wikipedia-api是一個Python庫,可輕松解析Wikipedia頁面。我們將使用這個庫來提取所需的頁面,但會排除頁面底部的所有“注釋”和“參考文獻(xiàn)”內(nèi)容。

簡單地寫出頁面的名稱:

topic = "Russo-Ukrainian War"wiki = wikipediaapi.Wikipedia('en')page = wiki.page(topic)txt = page.text[:page.text.find("See also")]txt[0:500] + " ..."

通過從文本中識別和提取subjects-actions-objects來繪制歷史事件的關(guān)系圖譜(因此動詞是關(guān)系)。

自然語言處理

要構(gòu)建知識圖譜,首先需要識別實(shí)體及其關(guān)系。因此,需要使用自然語言處理技術(shù)處理文本數(shù)據(jù)集。

目前,最常用于此類任務(wù)的庫是SpaCy,它是一種開源軟件,用于高級自然語言處理,利用Cython(C+Python)進(jìn)行加速。SpaCy使用預(yù)訓(xùn)練的語言模型對文本進(jìn)行標(biāo)記化,并將其轉(zhuǎn)換為“文檔”對象,該對象包含模型預(yù)測的所有注釋。

#python -m spacy download en_core_web_smnlp = spacy.load("en_core_web_sm")doc = nlp(txt)

NLP模型的第一個輸出是句子分割(中文有自己的分詞規(guī)則):即確定句子的起始和結(jié)束位置的問題。通常,它是通過基于標(biāo)點(diǎn)符號對段落進(jìn)行分割來完成的?,F(xiàn)在我們來看看SpaCy將文本分成了多少個句子:

# from text to a list of sentenceslst_docs = [sent for sent in doc.sents]print("tot sentences:", len(lst_docs))

現(xiàn)在,對于每個句子,我們將提取實(shí)體及其關(guān)系。為了做到這一點(diǎn),首先需要了解詞性標(biāo)注(POS):即用適當(dāng)?shù)恼Z法標(biāo)簽標(biāo)記句子中的每個單詞的過程。以下是可能標(biāo)記的完整列表(截至今日):

ADJ: 形容詞,例如big,old,green,incomprehensible,firstADP: 介詞,例如in,to,duringADV: 副詞,例如very,tomorrow,down,where,thereAUX: 助動詞,例如is,has(done),will(do),should(do)CONJ: 連詞,例如and,or,butCCONJ: 并列連詞,例如and,or,butDET: 限定詞,例如a,an,theINTJ: 感嘆詞,例如psst,ouch,bravo,helloNOUN: 名詞,例如girl,cat,tree,air,beautyNUM: 數(shù)詞,例如1,2017,one,seventy-seven,IV,MMXIVPART: 助詞,例如's,notPRON: 代詞,例如I,you,he,she,myself,themselves,somebodyPROPN: 專有名詞,例如Mary,John,London,NATO,HBOPUNCT: 標(biāo)點(diǎn)符號,例如.,(,),?SCONJ: 從屬連詞,例如if,while,thatSYM: 符號,例如$,%,§,?,+,-,×,÷,=,:),表情符號VERB: 動詞,例如run,runs,running,eat,ate,eatingX: 其他,例如sfpksdpsxmsaSPACE: 空格,例如

僅有詞性標(biāo)注是不夠的,模型還會嘗試?yán)斫鈫卧~對之間的關(guān)系。這個任務(wù)稱為依存句法分析(Dependency Parsing,DEP)。以下是可能的標(biāo)簽完整列表(截至今日)。

ACL:作為名詞從句的修飾語ACOMP:形容詞補(bǔ)語ADVCL:狀語從句修飾語ADVMOD:狀語修飾語AGENT:主語中的動作執(zhí)行者AMOD:形容詞修飾語APPOS:同位語ATTR:主謂結(jié)構(gòu)中的謂語部分AUX:助動詞AUXPASS:被動語態(tài)中的助動詞CASE:格標(biāo)記CC:并列連詞CCOMP:從句補(bǔ)足語COMPOUND:復(fù)合修飾語CONJ:連接詞CSUBJ:主語從句CSUBJPASS:被動語態(tài)中的主語從句DATIVE:與雙賓語動詞相關(guān)的間接賓語DEP:未分類的依賴DET:限定詞DOBJ:直接賓語EXPL:人稱代詞INTJ:感嘆詞MARK:標(biāo)記META:元素修飾語NEG:否定修飾語NOUNMOD:名詞修飾語NPMOD:名詞短語修飾語NSUBJ:名詞從句主語NSUBJPASS:被動語態(tài)中的名詞從句主語NUMMOD:數(shù)字修飾語OPRD:賓語補(bǔ)足語PARATAXIS:并列結(jié)構(gòu)PCOMP:介詞的補(bǔ)足語POBJ:介詞賓語POSS:所有格修飾語PRECONJ:前置連詞PREDET:前置限定詞PREP:介詞修飾語PRT:小品詞PUNCT:標(biāo)點(diǎn)符號QUANTMOD:量詞修飾語RELCL:關(guān)系從句修飾語ROOT:句子主干XCOMP:開放性從句補(bǔ)足語

舉個例子來理解POS標(biāo)記和DEP解析:

# take a sentencei = 3lst_docs[i]

檢查 NLP 模型預(yù)測的 POS 和 DEP 標(biāo)簽:

for token in lst_docs[i]: print(token.text, "-->", "pos: "+token.pos_, "|", "dep: "+token.dep_, "")

SpaCy提供了一個圖形工具來可視化這些注釋:

from spacy import displacydisplacy.render(lst_docs[i], style="dep", options={"distance":100})

最重要的標(biāo)記是動詞 ( POS=VERB ),因?yàn)樗蔷渥又泻x的詞根 ( DEP=ROOT )。

助詞,如副詞和副詞 ( POS=ADV/ADP ),通常作為修飾語 ( *DEP=mod ) 與動詞相關(guān)聯(lián),因?yàn)樗鼈兛梢孕揎梽釉~的含義。例如,“ travel to ”和“ travel from ”具有不同的含義,即使詞根相同(“ travel ”)。

在與動詞相連的單詞中,必須有一些名詞(POS=PROPN/NOUN)作為句子的主語和賓語( *DEP=nsubj/obj )。

名詞通常位于形容詞 ( POS=ADJ ) 附近,作為其含義的修飾語 ( DEP=amod )。例如,在“好人”和“壞人”中,形容詞賦予名詞“人”相反的含義。

SpaCy執(zhí)行的另一個很酷的任務(wù)是命名實(shí)體識別(NER)。命名實(shí)體是“真實(shí)世界中的對象”(例如人、國家、產(chǎn)品、日期),模型可以在文檔中識別各種類型的命名實(shí)體。以下是可能的所有標(biāo)簽的完整列表(截至今日):

人名: 包括虛構(gòu)人物。國家、宗教或政治團(tuán)體:民族、宗教或政治團(tuán)體。地點(diǎn):建筑、機(jī)場、高速公路、橋梁等。公司、機(jī)構(gòu)等:公司、機(jī)構(gòu)等。地理位置:國家、城市、州。地點(diǎn):非國家地理位置,山脈、水域等。產(chǎn)品:物體、車輛、食品等(不包括服務(wù))。事件:命名颶風(fēng)、戰(zhàn)斗、戰(zhàn)爭、體育賽事等。藝術(shù)作品:書籍、歌曲等的標(biāo)題。法律:成為法律的指定文件。語言:任何命名的語言。日期:絕對或相對日期或期間。時間:小于一天的時間。百分比:百分比,包括“%”。貨幣:貨幣價值,包括單位。數(shù)量:衡量重量或距離等。序數(shù): “第一”,“第二”等。基數(shù):不屬于其他類型的數(shù)字。

for tag in lst_docs[i].ents: print(tag.text, f"({tag.label_})")

或者使用SpaCy圖形工具更好:

displacy.render(lst_docs[i], style="ent")

這對于我們想要向知識圖譜添加多個屬性的情況非常有用。

接下來,使用NLP模型預(yù)測的標(biāo)簽,我們可以提取實(shí)體及其關(guān)系。

實(shí)體和關(guān)系抽取

這個想法很簡單,但實(shí)現(xiàn)起來可能會有些棘手。對于每個句子,我們將提取主語和賓語以及它們的修飾語、復(fù)合詞和它們之間的標(biāo)點(diǎn)符號。

可以通過兩種方式完成:

手動方式:可以從基準(zhǔn)代碼開始,該代碼可能必須稍作修改并針對您特定的數(shù)據(jù)集/用例進(jìn)行調(diào)整。

def extract_entities(doc): a, b, prev_dep, prev_txt, prefix, modifier = "", "", "", "", "", "" for token in doc: if token.dep_ != "punct": ## prexif --> prev_compound + compound if token.dep_ == "compound": prefix = prev_txt +" "+ token.text if prev_dep == "compound" else token.text ## modifier --> prev_compound + %mod if token.dep_.endswith("mod") == True: modifier = prev_txt +" "+ token.text if prev_dep == "compound" else token.text ## subject --> modifier + prefix + %subj if token.dep_.find("subj") == True: a = modifier +" "+ prefix + " "+ token.text prefix, modifier, prev_dep, prev_txt = "", "", "", "" ## if object --> modifier + prefix + %obj if token.dep_.find("obj") == True: b = modifier +" "+ prefix +" "+ token.text prev_dep, prev_txt = token.dep_, token.text # clean a = " ".join([i for i in a.split()]) b = " ".join([i for i in b.split()]) return (a.strip(), b.strip())# The relation extraction requires the rule-based matching tool, # an improved version of regular expressions on raw text.def extract_relation(doc, nlp): matcher = spacy.matcher.Matcher(nlp.vocab) p1 = [{'DEP':'ROOT'}, {'DEP':'prep', 'OP':"?"}, {'DEP':'agent', 'OP':"?"}, {'POS':'ADJ', 'OP':"?"}] matcher.add(key="matching_1", patterns=[p1]) matches = matcher(doc) k = len(matches) - 1 span = doc[matches[k][1]:matches[k][2]] return span.text

讓我們在這個數(shù)據(jù)集上試試看,看看通常的例子:

## extract entitieslst_entities = [extract_entities(i) for i in lst_docs]## examplelst_entities[i]

## extract relationslst_relations = [extract_relation(i,nlp) for i in lst_docs]## examplelst_relations[i]

## extract attributes (NER)lst_attr = []for x in lst_docs: attr = "" for tag in x.ents: attr = attr+tag.text if tag.label_=="DATE" else attr+"" lst_attr.append(attr)## examplelst_attr[i]

第二種方法是使用Textacy,這是一個基于SpaCy構(gòu)建的庫,用于擴(kuò)展其核心功能。這種方法更加用戶友好,通常也更準(zhǔn)確。

## extract entities and relationsdic = {"id":[], "text":[], "entity":[], "relation":[], "object":[]}for n,sentence in enumerate(lst_docs): lst_geNERators = list(textacy.extract.subject_verb_object_triples(sentence)) for sent in lst_generators: subj = "_".join(map(str, sent.subject)) obj = "_".join(map(str, sent.object)) relation = "_".join(map(str, sent.verb)) dic["id"].append(n) dic["text"].append(sentence.text) dic["entity"].append(subj) dic["object"].append(obj) dic["relation"].append(relation)## create dataframedtf = pd.DataFrame(dic)## exampledtf[dtf["id"]==i]

讓我們也使用 NER 標(biāo)簽(即日期)提取屬性:

## extract attributesattribute = "DATE"dic = {"id":[], "text":[], attribute:[]}for n,sentence in enumerate(lst_docs): lst = list(textacy.extract.entities(sentence, include_types={attribute})) if len(lst) > 0: for attr in lst: dic["id"].append(n) dic["text"].append(sentence.text) dic[attribute].append(str(attr)) else: dic["id"].append(n) dic["text"].append(sentence.text) dic[attribute].append(np.nan)dtf_att = pd.DataFrame(dic)dtf_att = dtf_att[~dtf_att[attribute].isna()]## exampledtf_att[dtf_att["id"]==i]

已經(jīng)提取了“知識”,接下來可以構(gòu)建圖表了。

網(wǎng)絡(luò)圖

Python標(biāo)準(zhǔn)庫中用于創(chuàng)建和操作圖網(wǎng)絡(luò)的是NetworkX。我們可以從整個數(shù)據(jù)集開始創(chuàng)建圖形,但如果節(jié)點(diǎn)太多,可視化將變得混亂:

## create full graphG = nx.from_pandas_edgelist(dtf, source="entity", target="object", edge_attr="relation", create_using=nx.DiGraph())## plotplt.figure(figsize=(15,10))pos = nx.spring_layout(G, k=1)node_color = "skyblue"edge_color = "black"nx.draw(G, pos=pos, with_labels=True, node_color=node_color, edge_color=edge_color, cmap=plt.cm.Dark2, node_size=2000, connectionstyle='arc3,rad=0.1')nx.draw_networkx_edge_labels(G, pos=pos, label_pos=0.5, edge_labels=nx.get_edge_attributes(G,'relation'), font_size=12, font_color='black', alpha=0.6)plt.show()

知識圖譜可以讓我們從大局的角度看到所有事物的相關(guān)性,但是如果直接看整張圖就沒有什么用處。因此,最好根據(jù)我們所需的信息應(yīng)用一些過濾器。對于這個例子,我將只選擇涉及最常見實(shí)體的部分(基本上是最連接的節(jié)點(diǎn)):

dtf["entity"].value_counts().head()

## filterf = "Russia"tmp = dtf[(dtf["entity"]==f) | (dtf["object"]==f)]## create small graphG = nx.from_pandas_edgelist(tmp, source="entity", target="object", edge_attr="relation", create_using=nx.DiGraph())## plotplt.figure(figsize=(15,10))pos = nx.nx_agraph.graphviz_layout(G, prog="neato")node_color = ["red" if node==f else "skyblue" for node in G.nodes]edge_color = ["red" if edge[0]==f else "black" for edge in G.edges]nx.draw(G, pos=pos, with_labels=True, node_color=node_color, edge_color=edge_color, cmap=plt.cm.Dark2, node_size=2000, node_shape="o", connectionstyle='arc3,rad=0.1')nx.draw_networkx_edge_labels(G, pos=pos, label_pos=0.5, edge_labels=nx.get_edge_attributes(G,'relation'), font_size=12, font_color='black', alpha=0.6)plt.show()

上面的效果已經(jīng)不錯了。如果想讓它成為 3D 的話,可以使用以下代碼:

from mpl_toolkits.mplot3d import Axes3Dfig = plt.figure(figsize=(15,10))ax = fig.add_subplot(111, projection="3d")pos = nx.spring_layout(G, k=2.5, dim=3)nodes = np.array([pos[v] for v in sorted(G) if v!=f])center_node = np.array([pos[v] for v in sorted(G) if v==f])edges = np.array([(pos[u],pos[v]) for u,v in G.edges() if v!=f])center_edges = np.array([(pos[u],pos[v]) for u,v in G.edges() if v==f])ax.scatter(*nodes.T, s=200, ec="w", c="skyblue", alpha=0.5)ax.scatter(*center_node.T, s=200, c="red", alpha=0.5)for link in edges: ax.plot(*link.T, color="grey", lw=0.5)for link in center_edges: ax.plot(*link.T, color="red", lw=0.5) for v in sorted(G): ax.text(*pos[v].T, s=v)for u,v in G.edges(): attr = nx.get_edge_attributes(G, "relation")[(u,v)] ax.text(*((pos[u]+pos[v])/2).T, s=attr)ax.set(xlabel=None, ylabel=None, zlabel=None, xticklabels=[], yticklabels=[], zticklabels=[])ax.grid(False)for dim in (ax.xaxis, ax.yaxis, ax.zaxis): dim.set_ticks([])plt.show()

需要注意一點(diǎn),圖形網(wǎng)絡(luò)可能很有用且漂亮,但它不是本教程的重點(diǎn)。知識圖譜最重要的部分是“知識”(文本處理),然后可以在數(shù)據(jù)幀、圖形或其他圖表上顯示結(jié)果。例如,我可以使用NER識別的日期來構(gòu)建時間軸圖。

時間軸圖

首先,需要將被識別為“日期”的字符串轉(zhuǎn)換為日期時間格式。DateParser庫可以解析幾乎在網(wǎng)頁上常見的任何字符串格式中的日期。

def utils_parsetime(txt): x = re.match(r'.*([1-3][0-9]{3})', txt) #<--check if there is a year if x is not None: try: dt = dateparser.parse(txt) except: dt = np.nan else: dt = np.nan return dt

將它應(yīng)用于屬性的數(shù)據(jù)框:

dtf_att["dt"] = dtf_att["date"].apply(lambda x: utils_parsetime(x))## exampledtf_att[dtf_att["id"]==i]

將把它與實(shí)體關(guān)系的主要數(shù)據(jù)框結(jié)合起來:

tmp = dtf.copy()tmp["y"] = tmp["entity"]+" "+tmp["relation"]+" "+tmp["object"]dtf_att = dtf_att.merge(tmp[["id","y"]], how="left", on="id")dtf_att = dtf_att[~dtf_att["y"].isna()].sort_values("dt", ascending=True).drop_duplicates("y", keep='first')dtf_att.head()

最后,我可以繪制時間軸(繪制完整的圖表可能不會用到):

dates = dtf_att["dt"].valuesnames = dtf_att["y"].valuesl = [10,-10, 8,-8, 6,-6, 4,-4, 2,-2]levels = np.tile(l, int(np.ceil(len(dates)/len(l))))[:len(dates)]fig, ax = plt.subplots(figsize=(20,10))ax.set(title=topic, yticks=[], yticklabels=[])ax.vlines(dates, ymin=0, ymax=levels, color="tab:red")ax.plot(dates, np.zeros_like(dates), "-o", color="k", markerfacecolor="w")for d,l,r in zip(dates,levels,names): ax.annotate(r, xy=(d,l), xytext=(-3, np.sign(l)*3), textcoords="offset points", horizontalalignment="center", verticalalignment="bottom" if l>0 else "top")plt.xticks(rotation=90) plt.show()

過濾特定時間:

yyyy = "2022"dates = dtf_att[dtf_att["dt"]>yyyy]["dt"].valuesnames = dtf_att[dtf_att["dt"]>yyyy]["y"].valuesl = [10,-10, 8,-8, 6,-6, 4,-4, 2,-2]levels = np.tile(l, int(np.ceil(len(dates)/len(l))))[:len(dates)]fig, ax = plt.subplots(figsize=(20,10))ax.set(title=topic, yticks=[], yticklabels=[])ax.vlines(dates, ymin=0, ymax=levels, color="tab:red")ax.plot(dates, np.zeros_like(dates), "-o", color="k", markerfacecolor="w")for d,l,r in zip(dates,levels,names): ax.annotate(r, xy=(d,l), xytext=(-3, np.sign(l)*3), textcoords="offset points", horizontalalignment="center", verticalalignment="bottom" if l>0 else "top")plt.xticks(rotation=90) plt.show()

提取“知識”后,可以根據(jù)自己喜歡的風(fēng)格重新繪制它。

結(jié)論

本文是關(guān)于**如何使用 Python 構(gòu)建知識圖譜的教程。**從維基百科解析的數(shù)據(jù)使用了幾種 NLP 技術(shù)來提取“知識”(即實(shí)體和關(guān)系)并將其存儲在網(wǎng)絡(luò)圖對象中。

現(xiàn)利用 NLP 和知識圖來映射來自多個來源的相關(guān)數(shù)據(jù)并找到對業(yè)務(wù)有用的見解。試想一下,將這種模型應(yīng)用于與單個實(shí)體(即 Apple Inc)相關(guān)的所有文檔(即財務(wù)報告、新聞、推文)可以提取多少價值。您可以快速了解與該實(shí)體直接相關(guān)的所有事實(shí)、人員和公司。然后,通過擴(kuò)展網(wǎng)絡(luò),即使信息不直接連接到起始實(shí)體 (A — > B — > C)。

以上就是關(guān)于pos機(jī)的專業(yè)知識,NLP 與 Python的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機(jī)的專業(yè)知識的知識,希望能夠幫助到大家!

轉(zhuǎn)發(fā)請帶上網(wǎng)址:http://m.dsth100338.com/news/49915.html

你可能會喜歡:

版權(quán)聲明:本文內(nèi)容由互聯(lián)網(wǎng)用戶自發(fā)貢獻(xiàn),該文觀點(diǎn)僅代表作者本人。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如發(fā)現(xiàn)本站有涉嫌抄襲侵權(quán)/違法違規(guī)的內(nèi)容, 請發(fā)送郵件至 babsan@163.com 舉報,一經(jīng)查實(shí),本站將立刻刪除。