【NLP自然语言处理】智能续话小程序

本文涉及的产品
NLP自然语言处理_高级版,每接口累计50万次
NLP 自学习平台,3个模型定制额度 1个月
NLP自然语言处理_基础版,每接口每天50万次
简介: 【NLP自然语言处理】智能续话小程序

@TOC


# 代码 ```python # -*-coding:utf-8-*- import time from keras_preprocessing.sequence import pad_sequences start = time.time() import tensorflow as tf from tensorflow import keras from tensorflow.keras.preprocessing.text import Tokenizer import numpy as np import tensorflow.keras.layers as layers; tokenizer = Tokenizer() # 要学习的语句 data = "Please forgive me for falling in love with you.\n" \ "Forgive me for loving you with all my heart.\n" \ "Forgive me for never wanting to be apart.\n" \ "Two star-crossed lovers in perfect harmony Just give me a chance and you will agree.\n" \ "I was meant for you.\n" \ "And you were meant for me." print(data) corpus = data.split("\n") print("---------------------------------------------------") print(corpus) tokenizer.fit_on_texts(corpus) total_words = len(tokenizer.word_index)+1 input_seq = [] for line in corpus: token_list = tokenizer.texts_to_sequences([line])[0] for i in range(1,len(token_list)): n_gram_seq = token_list[:i+1] input_seq.append(n_gram_seq) max_seq_len = max([len(x) for x in input_seq]) # 前置0填充 input_seq = np.array(pad_sequences(input_seq,maxlen=max_seq_len,padding='pre')) print("---------------------------------------------------") print(input_seq) # 特征和标签的划分 xs = input_seq[:,:-1] label = input_seq[:,-1] # 将标签转化为one-hot独热编码 ys = tf.keras.utils.to_categorical(label,num_classes=total_words) # 模型 model = keras.Sequential() model.add(layers.Embedding(total_words,240,input_length=max_seq_len-1)) model.add(layers.Bidirectional(layers.LSTM(150))) model.add(layers.Dense(total_words,activation='softmax')) adam = keras.optimizers.Adam(lr=0.01) model.compile(loss="categorical_crossentropy",optimizer=adam,metrics=['accuracy']) # 开始训练 history = model.fit(xs,ys,epochs=50,verbose=1) # 测试 test_text = "I want to" next_words = 20 for _ in range(next_words): token_list = tokenizer.texts_to_sequences(([test_text]))[0] token_list = pad_sequences([token_list],maxlen=max_seq_len-1,padding='pre') predicted = model.predict_classes(token_list,verbose=0) # print(tokenizer.word_index.items()) output_word = "" for word,index in tokenizer.word_index.items(): if index == predicted: output_word = word test_text = (test_text + " " + output_word) break print(test_text) print("共用时:",(time.time()-start),"秒") ```
# 效果展示 - I want to … I want to meant for you with all my heart 我想全心全意为你着想 - I hate you … I hate you were meant for me for never wanting to be apart 我恨你是我命中注定永远不想分开的 - I want to eat … I want to eat star crossed lovers in perfect harmony just give me a chance 我想要完美和谐吃着满天星的恋人给我一个机会
目录
打赏
0
0
0
0
116
分享
相关文章
如何结合NLP(自然语言处理)技术提升OCR系统的语义理解和上下文感知能力?
通过结合NLP技术,提升OCR系统的语义理解和上下文感知能力。方法包括集成NLP模块、文本预处理、语义特征提取、上下文推理及引入领域知识库。代码示例展示了如何使用Tesseract进行OCR识别,并通过BERT模型进行语义理解和纠错,最终提高文本识别的准确性。相关API如医疗电子发票验真、车险保单识别等可进一步增强应用效果。
自然语言处理(Natural Language Processing,简称NLP)
自然语言处理(NLP)是人工智能的分支,旨在让计算机理解、解释和生成人类语言。NLP的关键技术和应用包括语言模型、词嵌入、文本分类、命名实体识别、机器翻译、文本摘要、问答系统、情感分析、对话系统、文本生成和知识图谱等。随着深度学习的发展,NLP的应用日益广泛且效果不断提升。
128 4
自然语言处理(NLP)是AI的重要分支,旨在让计算机理解人类语言
自然语言处理(NLP)是AI的重要分支,旨在让计算机理解人类语言。本文探讨了深度学习在NLP中的应用,包括其基本任务、优势、常见模型及具体案例,如文本分类、情感分析等,并讨论了Python的相关工具和库,以及面临的挑战和未来趋势。
165 1
Pandas数据应用:自然语言处理
本文介绍Pandas在自然语言处理(NLP)中的应用,涵盖数据准备、文本预处理、分词、去除停用词等常见任务,并通过代码示例详细解释。同时,针对常见的报错如`MemoryError`、`ValueError`和`KeyError`提供了解决方案。适合初学者逐步掌握Pandas与NLP结合的技巧。
71 20
阿里通义推出SmartVscode插件,自然语言控制VS Code,轻松开发应用,核心技术开源!
SmartVscode插件深度解析:自然语言控制VS Code的革命性工具及其开源框架App-Controller
用自然语言控制电脑,字节跳动开源 UI-TARS 的桌面版应用!内附详细的安装和配置教程
UI-TARS Desktop 是一款基于视觉语言模型的 GUI 代理应用,支持通过自然语言控制电脑操作,提供跨平台支持、实时反馈和精准的鼠标键盘控制。
578 17
用自然语言控制电脑,字节跳动开源 UI-TARS 的桌面版应用!内附详细的安装和配置教程
自然语言处理(NLP)在文本分析中的应用:从「被动收集」到「主动分析」
【10月更文挑战第9天】自然语言处理(NLP)在文本分析中的应用:从「被动收集」到「主动分析」
77 4

热门文章

最新文章