本次主要介绍通过自然语言将关键词生成一篇原创文章,以及制作文章生成的接口api,方便需要生产内容的用户进行调用。下面就逻辑和使用方法给做个介绍。

首先是逻辑原理,原理就是人工智能深度学习,进行关键词的模型训练。从而会对目标词块也就是关键词进行相关性的输出。核心技术就不再讲解了,不是那么容易的。一时半会实力不允许。这次主要对平台的接口进行转接,也就是调用我生成的接口。不过平台的接口是付费的。

再平台调用的文章后,再对文章进行二次处理。得到一个排版好的格式文章。可以直接对接网站进行发布。

#获取文章

@retry(stop_max_attempt_number=1,retry_on_result=lambda x: x is None,wait_fixed=2000)

def getArticle(self,keyword):

result = outputText(keyword)

print(result)

if result['code'] == 200:

print('获取文章成功')

# self.collection.update_one({'keyword':result['keyword']},{'': {'content':result['content']}},upsert=True)

# return result['keyword'],result['content']

return result

return 0

#处理文章

def handle_content(self, info):

content = info['content']

if len(content) < 300:return

endflag = re.search(r'\?|\?|\。|\!|\!',content[-1])

if not endflag:

endResult = self.getArticle(content[-6:])

if endResult:content += endResult['content']

# 去除开头符号

content = re.sub(r'^\W*?(?P\w)', self.extract_one, content, re.S)

# print(content)

# 去除文章中的\

content = re.sub(r'\', '', content, re.S)

# print(content)

# 顿号、前数字是1234或一二三四,加br标签分段

content = re.sub(r'(?P\S)\s*?(?P、)',

self.extract_num, content, re.S)

# print(content)

# 句号、问号和感叹号后加br标签

content = re.sub(r'(?P\。|\?|\?|\!|\!)',

self.extract_hao, content, re.S)

# print(content)

# 两个顿号间的句号、问号和感叹号后不加br标签

content = re.sub(r'
\s*?
', '
', content, re.S)

# print(content)

info['content'] = content

print('文章处理成功')

return info

个人接口还是继续用flask进行生成,在flask中主要对接文章生成函数,通过接口地址请求,将文章生成出来。

@app.route('/nlp')

def nlp():

token = request.args.get('token','').strip()

keyword =  request.args.get('keyword','').strip()

if not token: return '还没有输入token'

if not keyword:return '还没有输入关键词'

hinfo = Article(keyword).main()

item = NlpApi.objects(token=token).first()

count = item.count - 1

item.update(keyword=keyword,nlpcont=hinfo['content'],count=count)

return jsonify({

'关键词':keyword,

'nlp文章写作':hinfo['content'],

'剩余次数':count

})

本次接口是通过get请求。只需要在浏览器中请求即可获取到自然语言生成的文章。

这样我们就能很轻松就能很轻松的生成一篇原创文章了,这在日后建站,做自媒体素材方面也有着一定作用,可以做到内容方面的积累。进行引流等作用。

如果需要这个接口生成文章,需要获取token参数。

token获取地址:

http://pan.cp30.cn

那么文章的生成计费方式简单粗暴,1毛钱/1篇。需要的话可以在商城

http://pan.cp30.cn

获取token参数,自动发token。