ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

用维基百科的数据改进自然语言处理任务

2021-04-20 09:29:13  阅读:269  来源: 互联网

标签:Category Musk Tesla Wikipedia load 维基百科 改进 Elon 自然语言


使用Wikipedia来改进NLP任务,如命名实体识别和主题建模

介绍

自然语言处理(NLP)正在兴起。计算语言学和人工智能正在加入它们的力量,促进突破性发现。虽然研究集中在显著提高NLP技术上,但企业正在把这项技术视为一项战略资产。这种由NLP引导的突破性创新的主要作用是大量可用的文本数据。谈到数字化时,尤其是对于企业来说,重要的是要记住文档是知识的主要来源。

但是,当训练自然语言处理任务时,最大的瓶颈之一就是训练的数据。当涉及诸如特定领域的实词应用程序时,我们面临着资源匮乏的数据问题。训练数据有两个主要问题:(i)难以获取大量数据,以及(ii)在注释可用数据以进行训练和测试时费时的过程。

面对这些问题已经引起了计算机科学的广泛关注。特别是,最新的计算进展提出了两种解决低资源数据问题的方法:

微调预先训练好的语言模型,如BERT或GPT-3;

利用高质量的开放数据存储库,如Wikipedia或ConceptNet。

现在,大多数可用的计算语言学开放库都提供了基于这两种方法之一来开发NLP工具的体系结构。 现在,我们演示如何利用Wikipedia来提高两个NLP任务的性能:命名实体识别和主题建模。

从维基百科中提取信息

有几种工具可用于处理来自Wikipedia的信息。 对于涉及文本数据自动处理的问题,我们使用了一个名为SpikeX的spaCy项目。

SpikeX是由一家意大利公司(Erre Quadro Srl)开发的,旨在帮助构建知识提取工具。SpikeX可以理解为一个可以进行spaCy pipeline操作的集合。

pip install --no-cache -U git+https://github.com/erre-quadro/spikex.git@develop
spikex download-wikigraph enwiki_core
spacy download en_core_web_sm

SpikeX有两个好用的功能:

1、给定一个维基百科页面,它会提取出所有相应的类别。

from wasabi import msg
from time import process_time as time

page = "Natural_Language_Processing"
print(f"Categories for `{page}`:")
t = time()
for c in wg.get_categories(page):
  print("\t", c)
  for cc in wg.get_categories(c):
    print("\t\t->", cc)
msg.good(f"Success in {time() - t:.2f}s")

结果如下:

Categories for `Natural_Language_Processing`:
  Category:Artificial_intelligence
  -> Category:Emerging_technologies
  -> Category:Cybernetics
  -> Category:Subfields_of_computer_science
  -> Category:Computational_neuroscience
  -> Category:Futures_studies
  -> Category:Cognitive_science
  -> Category:Personhood
  -> Category:Formal_sciences
  Category:Speech_recognition
  -> Category:Artificial_intelligence_applications
  -> Category:Computational_linguistics
  -> Category:Human–computer_interaction
  -> Category:Digital_signal_processing
  -> Category:Speech
  Category:Natural_language_processing
  -> Category:Artificial_intelligence_applications
  -> Category:Computational_linguistics
  Category:Computational_linguistics
  -> Category:Computational_social_science

2、给定一个句子,它会找到与维基百科页面标题匹配的文本块。

from spacy import load as spacy_load
from spikex.wikigraph import load as wg_load
from spikex.pipes import WikiPageX

# load a spacy model and get a doc
nlp = spacy_load('en_core_web_sm')
doc = nlp('Elon Musk runs Tesla Motors')
# load a WikiGraph
wg = wg_load('enwiki_core')
# get a WikiPageX and extract all pages
wikipagex = WikiPageX(wg)
doc = wikipagex(doc)
# see all pages extracted from the doc
for span in doc._.wiki_spans:
  print(span)
  print(span._.wiki_pages)
  print('------')

结果如下:

Elon Musk 
('Elon_Musk', 'Elon_musk', 'Elon_Musk_(book)', 'Elon_Musk_(2015_book)', 'Elon_Musk_(2015)', 'Elon_Musk_(biography)', 'Elon_Musk_(2015_biography)', 'Elon_Musk_(Ashlee_Vance)') 
------ 
Elon 
('Elon_(Judges)', 'Elon_(name)', 'Elon_(Hebrew_Judge)', 'Elon_(Ilan)', 'Elon_(disambiguation)', 'Elon_(biblical_judge)', 'Elon_(chemical)', 'Elon') 
------ 
Musk 
('Musk', 'MuSK', 'Musk_(wine)', 'Musk_(song)', 'Musk_(Tash_Sultana_song)', 'Musk_(disambiguation)') 
------ 
runs
('Runs_(baseball_statistics)', 'Runs', 'Runs_(cricket)', 'Runs_(music)', 'Runs_(baseball)', 'Runs_(Baseball)', 'Runs_(musical)') 
------
Tesla Motors 
('Tesla_motors', 'Tesla_Motors')
------ 
Tesla 
('Tesla_(band)', 'Tesla_(unit)', 'Tesla_(Czechoslovak_company)', 'Tesla_(crater)', 'Tesla_(microarchitecture)', 'Tesla_(2020_film)', 'Tesla_(car)', 'Tesla_(GPU)', 'TESLA', 'Tesla_(physicist)', 'Tesla_(group)', 'Tesla_(opera)', 'Tesla_(Bleach)', 'Tesla_(company)', 'Tesla_(disambiguation)', 'Tesla_(2016_film)', 'TESLA_(Czechoslovak_company)', 'Tesla_(unit_of_measure)', 'Tesla_(vehicles)', 'Tesla_(vehicle)', 'Tesla_(film)', 'Tesla_(album)', 'Tesla_(Flux_Pavilion_album)', 'Tesla_(surname)', 'Tesla') 
------ 
Motors ('Motors')

如我们所见,在第一个示例中,SpikeX提取了Wikipedia页面“Natural_language_processing”所属的所有类别。例如,“Natural_language_processing”属于“人工智能”、“语音识别”和“计算语言学”的类别。可以通过更深层次的检查进一步探索Wiki类别的树形结构。

在第二个例子中,对于“Elon Musk runs Tesla Motors”这句话,SpikeX抽取了这句话中所有可能在维基百科上有页面的页面。

现在,我们将看到如何使用这两个处理特性来执行命名实体识别和主题建模。

命名实体识别

命名实体识别(NER)是一项NLP任务,旨在将文本中提到的实体定位和分类为预定义的类别(例如人名,组织,位置等)。有许多不同的方法可以处理达到高精度的任务:基于规则的系统,训练深度神经网络的方法或细化预训练的语言模型的方法。例如,Spacy嵌入了一个预先训练的命名实体识别系统,该系统能够从文本中识别常见类别。

现在,我们着手建立一个NER系统,该系统能够识别属于某个Wikipedia类别的文本。让我们考虑以下示例语句:

Named Entity Recognition and Topic Modeling are two tasks of Natural Language Processing

这句话可能包含三个实体:“Named Entity Recognition”,“Topic Modeling”和“Natural Language Processing”。这三个实体具有属于某些类别的各自的Wikipedia页面。

在这张图片中,我们可以看到不同的类别如何在三个实体之间分布。 在这种情况下,类别可以看作是我们要从文本中提取的实体的标签。 现在,我们可以利用SpikeX的两个功能来构建一个自定义NER系统,该系统接受输入两个变量:句子的(i)文本和我们要检测的(ii)类别。

from wasabi import msg
from spacy import load as spacy_load
from spikex.wikigraph import load as wg_load
from spikex.pipes import WikiPageX


def wiki_entity_recognition(text, entity_root):
	entities = []
  	wg = wg_load("enwiki_core") # load WikiGraph
  	wikipagex = WikiPageX(wg) # create WikiPageX
  	nlp = spacy_load("en_core_web_sm")
  	doc = wikipagex(nlp(text)) # get doc with wiki pages extracted
  	entity_root = entity_root.replace(" ", "_") # fix spaces, just in case
	# get context as root's categories at distance 2
  	context = set(wg.get_categories(entity_root, distance=2)) 
  	for span in doc._.wiki_spans:
		page_seen = set()
		for page in span._.wiki_pages:
			# avoid redirect duplicates
			pageid = wg.get_pageid(page)
		  	if pageid in page_seen:
				continue
		  	page_seen.add(pageid)
			# check intersection with context
		  	categories = set(wg.get_categories(page))
		  	if len(set.intersection(categories, context)) == 0:
				continue
			# good entity
		  	entities.append((span, page))
	return entities

#define the text
text = "Named Entity Recognition and Topic Modeling are two tasks of Natural Language Processing"
#define the category
entity_root = "Computational linguistic"

for ent in wiki_entity_recognition(text, entity_root):
  print("%s - %s"%(ent[0],ent[1].upper()))

将打印如下结果:

Named Entity Recognition - COMPUTATIONAL LINGUISTIC
Topic Modeling - COMPUTATIONAL LINGUISTIC
Natural Language Processing - COMPUTATIONAL LINGUISTIC

将wikipedia的类别定义为NER任务的标签,可以定义一个NER系统,从而避免数据训练问题。通过使用我们的基于Wikipedia类别的NER系统来表示提取的实体,还展示了一个进一步的示例。

在这个例子中,类别“Programming Language”和“Computational Linguistics”作为输入给出,然后在文本中搜索。

主题建模

当谈到主题建模时,我们通常指的是一种NLP工具,它能够发现文本主体的“隐藏语义结构”。最近,已经讨论了“为了自动文本分析的目的,主题的确切定义在某种程度上取决于所采用的方法” [1]。LDA(Latent Dirichlet Allocation潜在狄利克雷分布,注意:这里说的不是线性判别分析)是一种流行的主题建模方法,该方法使用概率模型在文档集中提取主题。另一个著名的方法是TextRank,它是一种使用网络分析来检测单个文档中主题的方法。最近,在NLP中的高级研究还引入了能够在句子级别提取主题的方法。语义超图(Semantic Hypergraphs)就是一个例子,“一种新颖的技术结合了机器学习和符号方法的优势,可以从句子的含义中推断出话题” [1]。

现在,我们看到如何使用Wikipedia在句子和文档级别执行主题建模。

让我们考虑专利US20130097769A1的以下内容。

Encapsulated protective suits may be worn in contaminated areas to protect the wearer of the suit. For example, workers may wear an encapsulated protective suit while working inside of a nuclear powered electrical generating plant or in the presence of radioactive materials. An encapsulated protective suit may be a one-time use type of system, wherein after a single use the suit is disposed of. An encapsulated protective suit may receive breathing air during normal operating conditions via an external air flow hose connected to the suit. The air may be supplied, for example, by a power air purifying respirator (PAPR) that may be carried by the user.

topics = Counter()

for sent in doc.sents:

  topics = Counter()
  sent = nlp(sent.text)
  sent = wikipagex(sent)

  print(sent)
  print('Topics in the sentence:')
  for span in sent._.wiki_spans:
    if (
        len(span._.wiki_pages) > 1 
        or span[0].pos_ not in good_pos
        or span[-1].pos_ not in good_pos
    ):
      continue
    topics.update(wg.get_categories(span._.wiki_pages[0], distance=2))
  for topic, count in topics.most_common():
    print('\t',topic.replace('Category:',''), "->", count)
  print('----')

结果如下:

Sentence:
Encapsulated protective suits may be worn in contaminated areas to protect the wearer of the suit. Topics in the sentence:
   Safety -> 1
   Euthenics -> 1 ----Sentence:
For example, workers may wear an encapsulated protective suit while working inside of a nuclear powered electrical generating plant or in the presence of radioactive materials.
Topics in the sentence:
   Safety -> 1
   Euthenics -> 1
   Electricity -> 1
   Electromagnetism -> 1
   Locale_(geographic) -> 1
   Electric_power_generation -> 1
   Power_stations -> 1
   Infrastructure -> 1
   Energy_conversion -> 1
   Chemistry -> 1
   Radioactivity -> 1
---- Sentence:
An encapsulated protective suit may be a one-time use type of system, wherein after a single use the suit is disposed of. Topics in the sentence:   
   Safety -> 1
   Euthenics -> 1
   Transportation_planning -> 1
   Feminist_economics -> 1
   Schools_of_economic_thought -> 1
   Land_management -> 1
   Architecture -> 1
   Planning -> 1
   Transport -> 1
   Feminism -> 1
   Urban_planning -> 1
   Feminist_theory -> 1
   Urbanization -> 1
   Spatial_planning -> 1
   Social_sciences -> 1
----Sentence:
An encapsulated protective suit may receive breathing air during normal operating conditions via an external air flow hose connected to the suit. Topics in the sentence:
   Safety -> 1
   Euthenics -> 1
   Chemical_industry -> 1
   Gases -> 1
   Industrial_gases -> 1
   Breathing_gases -> 1
   Diving_equipment -> 1
   Respiration -> 1
   Electromechanical_engineering -> 1
   Heat_transfer -> 1
   Home_appliances -> 1
   Engineering_disciplines -> 1
   Automation -> 1
   Building_engineering -> 1
   Temperature -> 1
   Heating,_ventilation,_and_air_conditioning -> 1   
---- Sentence:
The air may be supplied, for example, by a power air purifying respirator (PAPR) that may be carried by the user.Topics in the sentence:
   Personal_protective_equipment -> 1
   Air_filters -> 1
   Respirators -> 1
   Protective_gear -> 1
----

专利文本的每个句子都使用SpikeX处理,并且从句子中检测到的相应Wikipedia页面中提取了Categories。 我们将主题视为Wikipedia的类别。 这样,我们就可以首次对主题进行简单的检测。 与语义超图,文本等级或LDA不同,此方法无需直接引用术语即可查找句子主题的标签。 提取的主题标签是指与SpikeX匹配的Wikipedia页面的类别。 如果我们使用这种方法汇总每个句子的主题,那么整个文档将有更好的表示形式。

通过增加句子中类别的频率,可以更广泛地查看文本的主题分布。 “Safety”和“Euthenics”出现的频率高于其他类别。

现在,我们使用整个专利文本(可在Google Patent中找到)来查找分类分布。

如我们所见,我们可以自动检测整个文档的主题(或类别)(在这种情况下为专利)。 展望前5个类别,我们可以推断出专利的含义。 无需任何预训练任务即可完成此操作。

总结

十多年来,维基百科已被用作知识的来源,并已在多种应用中反复使用:文本注释,分类,索引,聚类,搜索和自动分类法生成。维基百科的结构实际上具有许多有用的功能,使其成为这些应用程序的理想之选。

这篇文章演示了如何使用这一强大的资源来改进NLP的简单任务。但是,并未声称此方法优于其他最新方法。这篇文章中未显示评估NLP任务准确性的典型精度和召回率度量。

而且,这种方法具有优点和缺点。主要优点在于避免了训练,从而减少了耗时的注释任务。可以将Wikipedia视为一项庞大的培训课程,其贡献者遍布全球。对于有监督的任务(例如NER)和无监督的任务(例如主题建模),这是正确的。这种方法的缺点是双重的。首先,维基百科是一项公共服务,由专家和非专家共同为知识库提供服务。其次,从主题建模结果可以看出,自然语言的歧义会导致性能出现偏差。词义歧义消除和非专家驱动的数据整理显然会影响整个系统的可靠性。

但是,还有很大的改进空间。将Wikipedia视为针对NLP任务的大型开放式知识库,这与新的即将发生的范式转换是一致的:所谓的人工智能(AGI),即系统理解或学习人类可以执行的任何智力任务的假设能力。

论文:[1] Menezes, Telmo, and Camille Roth. “Semantic hypergraphs.” arXiv preprint arXiv:1908.10784 (2019).

作者:Nicola Melluso

deephub翻译组

标签:Category,Musk,Tesla,Wikipedia,load,维基百科,改进,Elon,自然语言
来源: https://blog.csdn.net/m0_46510245/article/details/115892226

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有