Serverless 实战:如何结合 NLP 实现文本摘要和关键词提取?

优采云 发布时间: 2020-08-25 18:17

  Serverless 实战:如何结合 NLP 实现文本摘要和关键词提取?

  对文本进行手动摘要的提取和关键词的提取,属于自然语言处理的范畴。提取摘要的一个用处是可以使阅读者通过最少的信息判别出这个文章对自己是否有意义或则价值,是否须要进行愈发详尽的阅读;而提取关键词的用处是可以使文章与文章之间形成关联,同时也可以使读者通过关键词快速定位到和该关键词相关的文章内容。

  文本摘要和关键词提取都可以和传统的 CMS 进行结合,通过对文章 / 新闻等发布功能进行整修,同步提取关键词和摘要,放到 HTML 页面中作为 Description 和 Keyworks。这样做在一定程度上有利于搜索引擎收录,属于 SEO 优化的范畴。

  关键词提取

  关键词提取的方式好多,但是最常见的应当就是tf-idf了。

  通过jieba实现基于tf-idf关键词提取的方式:

  jieba.analyse.extract_tags(text, topK=5, withWeight=False, allowPOS=('n', 'vn', 'v'))

  文本摘要

  文本摘要的方式也有好多,如果从广义上来界定,包括提取式和生成式。其中提取式就是在文章中通过TextRank等算法,找出关键句之后进行拼装,形成摘要,这种方式相对来说比较简单,但是很难提取出真实的语义等;另一种方式是生成式,通过深度学习等方式,对文本语义进行提取再生成摘要。

  如果简单理解,提取式方法生成的摘要,所有语句来自原文,而生成式方式则是独立生成的。

  为了简化难度,本文将采用提取式来实现文本摘要功能,通过 SnowNLP 第三方库,实现基于TextRank的文本摘要功能。我们以《海底两万里》部分内容作为原文,进行摘要生成:

  原文:

  这些风波发生时,我刚从英国内布拉斯加州的贫瘠地区做完一项科考工作回去。我当时是巴黎自然史博物馆的客座教授,法国政府派我出席此次考察活动。我在内布拉斯加州渡过了半年时间,采集了许多珍稀资料,满载而归,3 月底到达伦敦。我决定 5 月初动身回美国。于是,我就抓紧这段候船停留时间,把搜集到的矿物和动植物标本进行分类整理,可就在这时,斯科舍号出事了。

  我对当时的街谈巷议自然了如指掌,再说了,我岂能听而不闻、无动于衷呢?我把日本和法国的各类报刊读了又读,但无法深入了解真相。神秘莫测,百思不得其解。我左思右想,摇摆于两个极端之间,始终形不成一种看法。其中肯定有名堂,这是不容置疑的,如果有人表示怀疑,就请她们去摸一摸斯科舍号的创口好了。

  我到伦敦时,这个问题正炒得沸反盈天。某些不学无术之徒提出构想,有说是浮动的小岛,也有说是不可捉摸的暗礁,不过,这些个假定通通都被推翻了。很显然,除非这暗礁头部装有机器,不然的话,它岂能这么快速地转移呢?

  同样的道理,说它是一块浮动的舱室或是一堆大船残骸,这种假定也不能创立,理由依旧是联通速率很快。

  那么,问题只能有两种解释,人们各持己见,自然就分成观点截然不同的两派:一派说这是一个力大无比的怪物,另一派说这是一艘动力极强的“潜水船”。

  哦,最后那个假定尚且可以接受,但到欧美各国调查过后,也就无法自圆其说了。有那个普通人会拥有这么强悍动力的机械?这是不可能的。他在何地何时叫何人制造了这么个庞然大物,而且怎么能在建造中做到风声不探听呢?

  看来,只有政府才有可能拥有这些破坏性的机器,在这个灾难深重的时代,人们千方百计要提高战争装备威力,那就有此类可能,一个国家瞒着其他国家在试制这类骇人听闻的装备。继夏斯勃手*敏*感*词*以后有鱼雷,水雷以后有水下撞锤,然后剑皇高涨反应,事态愈演愈烈。至少,我是这样想的。

  通过 SnowNLP 提供的算法:

  from snownlp import SnowNLP

text = " 上面的原文内容,此处省略 "

s = SnowNLP(text)

print("。".join(s.summary(5)))

  输出结果:

  自然就分成观点截然不同的两派:一派说这是一个力大无比的怪物。这种假设也不能成立。我到纽约时。说它是一块浮动的船体或是一堆大船残片。另一派说这是一艘动力极强的“潜水船”

  初步来看,效果并不是挺好,接下来我们自己估算语句权重,实现一个简单的摘要功能,这个就须要jieba:

  import re

import jieba.analyse

import jieba.posseg

class TextSummary:

def __init__(self, text):

self.text = text

def splitSentence(self):

sectionNum = 0

self.sentences = []

for eveSection in self.text.split("\n"):

if eveSection:

sentenceNum = 0

for eveSentence in re.split("!|。|?", eveSection):

if eveSentence:

mark = []

if sectionNum == 0:

mark.append("FIRSTSECTION")

if sentenceNum == 0:

mark.append("FIRSTSENTENCE")

self.sentences.append({

"text": eveSentence,

"pos": {

"x": sectionNum,

"y": sentenceNum,

"mark": mark

}

})

sentenceNum = sentenceNum + 1

sectionNum = sectionNum + 1

self.sentences[-1]["pos"]["mark"].append("LASTSENTENCE")

for i in range(0, len(self.sentences)):

if self.sentences[i]["pos"]["x"] == self.sentences[-1]["pos"]["x"]:

self.sentences[i]["pos"]["mark"].append("LASTSECTION")

def getKeywords(self):

self.keywords = jieba.analyse.extract_tags(self.text, topK=20, withWeight=False, allowPOS=('n', 'vn', 'v'))

def sentenceWeight(self):

# 计算句子的位置权重

for sentence in self.sentences:

mark = sentence["pos"]["mark"]

weightPos = 0

if "FIRSTSECTION" in mark:

weightPos = weightPos + 2

if "FIRSTSENTENCE" in mark:

weightPos = weightPos + 2

if "LASTSENTENCE" in mark:

weightPos = weightPos + 1

if "LASTSECTION" in mark:

weightPos = weightPos + 1

sentence["weightPos"] = weightPos

# 计算句子的线索词权重

index = [" 总之 ", " 总而言之 "]

for sentence in self.sentences:

sentence["weightCueWords"] = 0

sentence["weightKeywords"] = 0

for i in index:

for sentence in self.sentences:

if sentence["text"].find(i) >= 0:

sentence["weightCueWords"] = 1

for keyword in self.keywords:

for sentence in self.sentences:

if sentence["text"].find(keyword) >= 0:

sentence["weightKeywords"] = sentence["weightKeywords"] + 1

for sentence in self.sentences:

sentence["weight"] = sentence["weightPos"] + 2 * sentence["weightCueWords"] + sentence["weightKeywords"]

def getSummary(self, ratio=0.1):

self.keywords = list()

self.sentences = list()

self.summary = list()

# 调用方法,分别计算关键词、分句,计算权重

self.getKeywords()

self.splitSentence()

self.sentenceWeight()

# 对句子的权重值进行排序

self.sentences = sorted(self.sentences, key=lambda k: k['weight'], reverse=True)

# 根据排序结果,取排名占前 ratio% 的句子作为摘要

for i in range(len(self.sentences)):

if i < ratio * len(self.sentences):

sentence = self.sentences[i]

self.summary.append(sentence["text"])

return self.summary

  这段代码主要是通过 tf-idf 实现关键词提取,然后通过关键词提取对语句进行权重赋于,最后获得到整体的结果,运行:

  testSummary = TextSummary(text)

print("。".join(testSummary.getSummary()))

  可以得到结果:

  Building prefix dict from the default dictionary ...

Loading model from cache /var/folders/yb/wvy_7wm91mzd7cjg4444gvdjsglgs8/T/jieba.cache

Loading model cost 0.721 seconds.

Prefix dict has been built successfully.

看来,只有政府才有可能拥有这种破坏性的机器,在这个灾难深重的时代,人们千方百计要增强战争武器威力,那就有这种可能,一个国家瞒着其他国家在试制这类骇人听闻的武器。于是,我就抓紧这段候船逗留时间,把收集到的矿物和动植物标本进行分类整理,可就在这时,斯科舍号出事了。同样的道理,说它是一块浮动的船体或是一堆大船残片,这种假设也不能成立,理由仍然是移动速度太快

  我们可以看见,整体疗效要比昨天的好一些。

  发布 API

  通过 Serverless 架构,将前面代码进行整理,并发布。

  代码整理结果:

  import re, json

import jieba.analyse

import jieba.posseg

class NLPAttr:

def __init__(self, text):

self.text = text

def splitSentence(self):

sectionNum = 0

self.sentences = []

for eveSection in self.text.split("\n"):

if eveSection:

sentenceNum = 0

for eveSentence in re.split("!|。|?", eveSection):

if eveSentence:

mark = []

if sectionNum == 0:

mark.append("FIRSTSECTION")

if sentenceNum == 0:

mark.append("FIRSTSENTENCE")

self.sentences.append({

"text": eveSentence,

"pos": {

"x": sectionNum,

"y": sentenceNum,

"mark": mark

}

})

sentenceNum = sentenceNum + 1

sectionNum = sectionNum + 1

self.sentences[-1]["pos"]["mark"].append("LASTSENTENCE")

for i in range(0, len(self.sentences)):

if self.sentences[i]["pos"]["x"] == self.sentences[-1]["pos"]["x"]:

self.sentences[i]["pos"]["mark"].append("LASTSECTION")

def getKeywords(self):

self.keywords = jieba.analyse.extract_tags(self.text, topK=20, withWeight=False, allowPOS=('n', 'vn', 'v'))

return self.keywords

def sentenceWeight(self):

# 计算句子的位置权重

for sentence in self.sentences:

mark = sentence["pos"]["mark"]

weightPos = 0

if "FIRSTSECTION" in mark:

weightPos = weightPos + 2

if "FIRSTSENTENCE" in mark:

weightPos = weightPos + 2

if "LASTSENTENCE" in mark:

weightPos = weightPos + 1

if "LASTSECTION" in mark:

weightPos = weightPos + 1

sentence["weightPos"] = weightPos

# 计算句子的线索词权重

index = [" 总之 ", " 总而言之 "]

for sentence in self.sentences:

sentence["weightCueWords"] = 0

sentence["weightKeywords"] = 0

for i in index:

for sentence in self.sentences:

if sentence["text"].find(i) >= 0:

sentence["weightCueWords"] = 1

for keyword in self.keywords:

for sentence in self.sentences:

if sentence["text"].find(keyword) >= 0:

sentence["weightKeywords"] = sentence["weightKeywords"] + 1

for sentence in self.sentences:

sentence["weight"] = sentence["weightPos"] + 2 * sentence["weightCueWords"] + sentence["weightKeywords"]

def getSummary(self, ratio=0.1):

self.keywords = list()

self.sentences = list()

self.summary = list()

# 调用方法,分别计算关键词、分句,计算权重

self.getKeywords()

self.splitSentence()

self.sentenceWeight()

# 对句子的权重值进行排序

self.sentences = sorted(self.sentences, key=lambda k: k['weight'], reverse=True)

# 根据排序结果,取排名占前 ratio% 的句子作为摘要

for i in range(len(self.sentences)):

if i < ratio * len(self.sentences):

sentence = self.sentences[i]

self.summary.append(sentence["text"])

return self.summary

def main_handler(event, context):

nlp = NLPAttr(json.loads(event['body'])['text'])

return {

"keywords": nlp.getKeywords(),

"summary": "。".join(nlp.getSummary())

}

  编写项目serverless.yaml文件:

  nlpDemo:

component: "@serverless/tencent-scf"

inputs:

name: nlpDemo

codeUri: ./

handler: index.main_handler

runtime: Python3.6

region: ap-guangzhou

description: 文本摘要 / 关键词功能

memorySize: 256

timeout: 10

events:

- apigw:

name: nlpDemo_apigw_service

parameters:

protocols:

- http

serviceName: serverless

description: 文本摘要 / 关键词功能

environment: release

endpoints:

- path: /nlp

method: ANY

  由于项目中使用了jieba,所以在安装的时侯推荐在 CentOS 系统下与对应的 Python 版本下安装,也可以使用我之前为了便捷做的一个依赖工具:

  

  通过sls --debug进行布署:

  

  部署完成,可以通过 PostMan 进行简单的测试:

  

  从上图可以看见,我们早已根据预期输出了目标结果。至此,文本摘要 / 关键词提取的 API 已经布署完成。

  总结

  相对来说,通过 Serveless 架构做 API 是十分容易和便捷的,可实现 API 的插拔行,组件化,希望本文才能给读者更多的思路和启发。

0 个评论

要回复文章请先登录注册


官方客服QQ群

微信人工客服

QQ人工客服


线