nlp文本相似度

自然语言处理 (Natural Language Processing)

什么是自然语言处理? (What is Natural Language Processing?)

Natural Language Processing (NLP) refers to developing an application that understands human languages. There are so many use cases for NLPs nowadays. Because people are generating thousands of gigabytes of text data every day through blogs, social media comments, product reviews, news archives, official reports, and many more. Search Engines are the biggest example of NLPs. I don’t think you will find very many people around you who never used search engines.

自然语言处理(NLP)是指开发能够理解人类语言的应用程序。 如今,NLP的用例很多。 因为人们每天通过博客,社交媒体评论,产品评论,新闻档案,官方报告等生成大量千兆字节的文本数据。 搜索引擎是NLP的最大示例。 我认为您周围不会有很多从未使用过搜索引擎的人。

项目概况 (Project Overview)

In my experience, the best way to learn is by doing a project. In this article, I will explain NLP with a real project. The dataset I will use is called ‘people_wiki.csv’. I found this dataset in Kaggle. Feel free to download the dataset from here:

以我的经验,最好的学习方法是做一个项目。 在本文中,我将用一个真实的项目解释NLP。 我将使用的数据集称为“ people_wiki.csv”。 我在Kaggle中找到了这个数据集。 可以从此处免费下载数据集:

The dataset contains the name of some famous people, their Wikipedia URL, and the text of their Wikipedia page. So, the dataset is very big. The goal of this project is, to find people of related backgrounds. In the end, if you provide the algorithm a name of a famous person, it will return the name of a predefined number of people who have a similar background according to the Wikipedia information. Does this sound a bit like a search engine?

数据集包含一些著名人物的姓名,他们的Wikipedia URL和他们的Wikipedia页面的文本。 因此,数据集非常大。 该项目的目标是寻找具有相关背景的人员。 最后,如果您提供算法一个名人的名字,它将根据Wikipedia信息返回预定义数量的具有相似背景的人的名字。 这听起来有点像搜索引擎吗?

分步实施 (Step By Step Implementation)

  1. Import the necessary packages and the dataset.导入必要的包和数据集。
import numpy as npimport pandas as pdfrom sklearn.neighbors import NearestNeighborsfrom sklearn.feature_extraction.text import CountVectorizerdf = pd.read_csv('people_wiki.csv')df.head()

2. Vectorize the ‘text’ column

2.向量化“文本”列

如何向量化? (How to Vectorize?)

In Python’s scikit-learn library, there is a function named ‘count vectorizer’. This function provides an index to each word and generates a vector that contains the number of appearances of each word in a piece of text. Here, I will demonstrate it with a small text for your understanding. Suppose, this is our text:

在Python的scikit-learn库中,有一个名为“ count vectorizer ”的函数。 此函数为每个单词提供一个索引,并生成一个矢量,其中包含一段文本中每个单词的出现次数。 在这里,我将用一小段文字进行演示,以供您理解。 假设这是我们的文本:

text = ["Jen is a good student. Jen plays guiter as well"]

Let’s import the function from the scikit_learn library and fit the text in the function.

让我们从scikit_learn库中导入函数,并在函数中放入文本。

vectorizer = CountVectorizer()vectorizer.fit(text)

Here, I am printing the vocabulary:

在这里,我正在打印词汇表:

print(vectorizer.vocabulary_)#Output:{'jen': 4, 'is': 3, 'good': 1, 'student': 6, 'plays': 5, 'guiter': 2, 'as': 0, 'well': 7}

Look, each word of the text received a number. Those numbers are the index of that word. It has eight significant words. So, the index is from 0 to 7. Next, we need to transform the text. I will print the transformed vector as an array.

看,文本的每个单词都收到一个数字。 这些数字是该单词的索引。 它有八个重要词。 因此,索引是从0到7。接下来,我们需要转换文本。 我将转换后的向量打印为数组。

vector = vectorizer.transform(text)print(vector.toarray())

Here is the output: [[1 1 1 1 2 1 1 1]]. ‘Jen’ has index 4 and it appeared twice. So in this output vector, the 4th indexed element is 2. All the other words appeared only once. So the elements of the vector are ones.

输出为:[[1 1 1 1 2 1 1 1]]。 “ Jen”的索引为4,它出现了两次。 因此,在此输出向量中,第四个索引元素为2。所有其他单词仅出现一次。 因此向量的元素是1。

Now, vectorize the ‘text’ column of the dataset, using the same technique.

现在,使用相同的技术对数据集的“文本”列进行矢量化处理。

vect = CountVectorizer()word_weight = vect.fit_transform(df['text'])

In the demonstration, I used ‘fit’ first and then ‘transform’ later’. But conveniently, you can use fit and transform both at once. This word_weight is the vectors of numbers as I explained before. There will be one such vector for each row of text in the ‘text’ column.

在演示中,我先使用“适合”,然后再使用“变换”。 但是很方便,您可以同时使用fit和transform。 正如我之前解释的,这个word_weight是数字的向量。 “文本”列中的每一行文本都会有一个这样的向量。

3. Fit this ‘word_weight’ from the previous step in the Nearest Neighbors function.

3.在“ 最近的邻居”功能中,按照上一步中的步骤安装此“ word_weight”。

The idea of the nearest neighbor’s function is to calculate the distance of a predefined number of training points from the required point. If it’s not clear, do not worry. Look at the implementation, it will be easier for you.

最近邻函数的想法是计算预定数量的训练点与所需点的距离。 如果不清楚,请不要担心。 看一下实现,对您来说会更容易。

nn = NearestNeighbors(metric = 'euclidean')nn.fit(word_weight)

4. Find 10 people with similar backgrounds as President Barak Obama.

4.找到10位与巴拉克·奥巴马总统背景相似的人。

First, find the index of ‘Barak Obama’ from the dataset.

首先,从数据集中找到“巴拉克·奥巴马”的索引。

obama_index = df[df['name'] == 'Barack Obama'].index[0]

Calculate the distance and the indices of 10 people who have the closest background as President Obama. In the word weight vector, the index of the text that contains the information about ‘Barak Obama’ should be in the same index as the dataset. we need to pass that index and the number of the person we want. That should return the calculated distance of those persons from ‘Barak Obama’ and the indices of those persons.

计算与奥巴马总统背景最接近的10个人的距离和指数。 在单词权重向量中,包含有关“巴拉克·奥巴马”的信息的文本索引应与数据集位于同一索引中。 我们需要传递该索引和所需人员的数量。 那应该返回这些人与“巴拉克·奥巴马”的距离以及这些人的指数。

distances, indices = nn.kneighbors(word_weight[obama_index], n_neighbors = 10)

Organize the result in a DataFrame.

在DataFrame中组织结果。

neighbors = pd.DataFrame({'distance': distances.flatten(), 'id': indices.flatten()})print(neighbors)

Let’s find the name of the persons from the indexes. There are several ways to find names from the index. I used the merge function. I just merged the ‘neighbors’ DataFrame above with the original DataFrame ‘df’ using the id column as the common column. Sorted values on distance. President Obama should have no distance from himself. So, he came on top.

让我们从索引中找到人员的名字。 有几种方法可以从索引中查找名称。 我使用了合并功能。 我只是使用id列作为公共列,将上面的'neighbors'数据框与原始DataFrame'df'合并。 距离上的排序值。 奥巴马总统与自己应该没有距离。 因此,他名列前茅。

nearest_info = (df.merge(neighbors, right_on = 'id', left_index = True).sort_values('distance')[['id', 'name', 'distance']])print(nearest_info)

These are the 10 people closest to President Obama according to the information provided in Wikipedia. Results make sense, right?

根据维基百科提供的信息,这是最接近奥巴马总统的10个人。 结果有意义,对不对?

A similar texts search could be useful in many areas such as searching for similar articles, similar resume, similar profiles as in this project, similar news items, similar songs. I hope you find this small project useful.

相似文本搜索在许多领域都可能有用,例如搜索相似的文章,相似的履历,与本项目相似的个人资料,相似的新闻条目,相似的歌曲。 我希望这个小项目对您有用。

Recommended Reading:

推荐读物:

翻译自: https://medium.com/towards-artificial-intelligence/similar-texts-search-in-python-with-a-few-lines-of-code-an-nlp-project-9ace2861d261

nlp文本相似度


http://www.taodudu.cc/news/show-863444.html

相关文章:

  • 爬虫goodreads数据_使用Python从Goodreads数据中预测好书
  • opengl层次建模_层次建模简介
  • 如何用dds实现线性调频_用神经网络生成线性调频
  • azure_Azure ML算法备忘单
  • 矩阵乘法如何去逆矩阵_矩阵乘法和求逆
  • 机器学习数据倾斜的解决方法_机器学习并不总是解决数据问题的方法
  • gan简介_GAN简介
  • 使用TensorFlow训练神经网络进行价格预测
  • 您应该如何改变数据科学教育
  • r语言解释回归模型的假设_模型假设-解释
  • 参考文献_参考文献:
  • 深度学习用于视频检测_视频如何用于检测您的个性?
  • 角距离恒星_恒星问卷调查的10倍机器学习生产率
  • apache beam_Apache Beam ML模型部署
  • 转正老板让你谈谈你的看法_让我们谈谈逻辑回归
  • openai-gpt_GPT-3报告存在的问题
  • 机器学习 凝聚态物理_机器学习遇到了凝聚的问题
  • 量子计算 qiskit_将Tensorflow和Qiskit集成到量子机器学习中
  • throw 烦人_烦人的简单句子聚类
  • 使用NumPy优于Python列表的优势
  • 迷你5和迷你4区别_可变大小的视频迷你批处理
  • power bi可视化表_如何使用Power BI可视化数据?
  • 变形金刚2_变形金刚(
  • 机器学习 测试_测试优先机器学习
  • azure机器学习_Microsoft Azure机器学习x Udacity —第4课笔记
  • 机器学习嵌入式实现_机器学习中的嵌入
  • 无监督学习 k-means_无监督学习-第3部分
  • linkedin爬虫_机器学习的学生和从业者的常见问题在LinkedIn上提问
  • lime 深度学习_用LIME解释机器学习预测并建立信任
  • 神经网络 梯度下降_梯度下降优化器对神经网络训练的影响

nlp文本相似度_用几行代码在Python中搜索相似文本:一个NLP项目相关推荐

  1. python热搜排行功能_简单几行代码用Python爬取微博的热搜榜

    简单几行代码用Python爬取微博的热搜榜 想要实时的看微博热搜 但是又不想去微博网站看!怎么办呢?其实很简单! 我们学了这个requests_html 这个库之后 就更加的简单了! 小编只用了短短的 ...

  2. python有趣的面试题_一道3行代码的Python面试题,我懵逼了...|python基础教程|python入门|python教程...

    https://www.xin3721.com/eschool/pythonxin3721/ 前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时 ...

  3. python画画100行代码_简单几步,100行代码用Python画一个蝙蝠侠的logo

    原标题:简单几步,100行代码用Python画一个蝙蝠侠的logo 转自:菜鸟学Python 蝙蝠侠作为DC漫画的核心人物之一,一直都受到广大粉丝的喜爱,而笔者作为DC的铁杆粉丝,自然也是老爷(粉丝对 ...

  4. python制作表白神器_程序员的七夕用30行代码让Python化身表白神器

    转眼又到了咱们中国传统的情人节七夕了,今天笔者就带大家来领略一下用Python表白的方式.让程序员的恋人们感受一下IT人的浪漫. 一.词云制作 首先咱们可以用之前介绍过的wordcould包制作词云. ...

  5. python代码翻译-10 行代码,Python 教你自制屏幕翻译工具,有逼格!!

    原标题:10 行代码,Python 教你自制屏幕翻译工具,有逼格!! 1. 场景 大家如果平常遇到不认识的英文,相信大部分的人都会复制内容后,使用翻译软件,或者拷贝到网站上去执行翻译. 当然,对于 I ...

  6. python爬虫代码1000行-最精简的爬虫 --仅需4行代码(python)

    最精简的爬虫 --仅需4行代码(python) 刚刚整理了下爬虫系列,于是乎就开始了第一次的技术分享 今天,我们主要讲述的是思路,思路,思路. 相比起,直接贴代码,思路显的更为重要 当初,自己的坑,希 ...

  7. 最简单的爬虫代码 python_最精简的爬虫 --仅需4行代码(python)

    最精简的爬虫 --仅需4行代码(python) 刚刚整理了下爬虫系列,于是乎就开始了第一次的技术分享 今天,我们主要讲述的是思路,思路,思路. 相比起,直接贴代码,思路显的更为重要 当初,自己的坑,希 ...

  8. python代码翻译器-10 行代码,Python 教你自制屏幕翻译工具,有逼格!!

    原标题:10 行代码,Python 教你自制屏幕翻译工具,有逼格!! 1. 场景 大家如果平常遇到不认识的英文,相信大部分的人都会复制内容后,使用翻译软件,或者拷贝到网站上去执行翻译. 当然,对于 I ...

  9. python简单爬虫代码-最精简的爬虫 --仅需4行代码(python)

    最精简的爬虫 --仅需4行代码(python) 刚刚整理了下爬虫系列,于是乎就开始了第一次的技术分享 今天,我们主要讲述的是思路,思路,思路. 相比起,直接贴代码,思路显的更为重要 当初,自己的坑,希 ...

最新文章

  1. 23-hadoop-hive的DDL和DML操作
  2. java.sql.SQLException: java.lang.StackOverflowError
  3. 牛客第七场 Sudoku Subrectangles
  4. SQLServer之创建INSTEAD OF INSERT,UPDATE,DELETE触发器
  5. Activity A 启动 Activity B 生命周期
  6. 服务器支持curl,互联网要点:服务器不支持curl_exec的解决办法
  7. 转: The Code Commandments: Best Practices for Objective-C Coding (updated for ARC)
  8. WINDOWS操作系统中可以允许最大的线程数
  9. android SDK安装以及环境变量配置
  10. 日更第4期-2015-1-19-openFrameworks系列第三讲-面向对象的小球们
  11. 芯片AD库导入(贸泽)
  12. dll加载失败,返回126错误
  13. js实现连续英文字符自动换行
  14. windows自带日语输入法快捷键
  15. ue4 unreal4 自定义网格 绘制自定义网格 绘制面 (Plus)
  16. 【问】批量删除作废会员的语句
  17. js引用本地json文件
  18. 为了旅游和梁定郊大吵一次,此行贿赠喜爱的朋友!!!
  19. 阿尔伯塔大学计算机科学专业课程,[阿尔伯塔大学]计算机/计算机工程专业
  20. UVa 1620 Lazy Susan(懒惰的苏珊)

热门文章

  1. powerdesigner奇淫技
  2. input 打开文件夹事件委托
  3. D-Link登录时密码错误输入三次而无法登录问题的处理
  4. 对“相亲节目整风”的看法
  5. java seconds_Java LocalTime minusSeconds()用法及代码示例
  6. 如何在log4j.properties文件中使用相对路径
  7. kotlin和java差别_Kotlin和Java的常用方法的区别总结
  8. c语言线程锁的原理开锁原理图,C++多线程之可重入锁
  9. android权限 启动失败,Android 6.0打开失败:EACCES(权限被拒绝)
  10. mysql my.ini my.cnf_Mysql配置文件my.ini/my.cnf