第二,我们看这个部分 。这就是《纽约时报》在最高级别对文章进行分类的方式:科学、政治、体育等等 。在网址的域名后面会进行显示,例如nytimes.com/…
第二部分是小节 。例如,一个版块可以细分为world,或者world可以细分为Australia 。并不是所有的文章都包含它,它不像以上那2个那么重要 。
第四是文风 。大多数文档比较分析只关注语义 。但是,由于我们是在实际用例中比较推荐,所以我们也需要类似的写作风格 。例如,你不想在学术期刊的“跑鞋和矫形术”之后,从商业角度阅读“十大跑鞋” 。我们将根据杰斐逊县学校的写作指导原则对文章进行分组 。该列表包括人类兴趣、个性、最佳(例如:产品评论)、新闻、操作方法、过去的事件和信息 。
5个候选算法这些是我们将要研究的算法 。
- Jaccard
- TF-IDF
- Doc2vec
- USE
- BERT
输入的是文章的全文内容 。标题被忽略 。
请注意,有些算法并不是为文档相似性而构建的 。但是在互联网上有如此不同的意见,我们将亲眼看到结果 。
我们将不关注概念理解,也不关注详细的代码审查 。相反,其目的是展示问题的设置有多简单 。如果你不明白以下算法的细节,不要担心,你可以阅读其他优秀博客进行理解
你可以在Github repo中找到整个代码库:https://github.com/massanishi/document_similarity_algorithms_experiments
如果你只想查看结果,请跳过此部分 。
JaccardJaccard 在一个多世纪前提出了这个公式 。长期以来,这一概念一直是相似性任务的标准 。
幸运的是,你会发现jaccard是最容易理解的算法 。数学很简单,没有向量化 。它可以让你从头开始编写代码 。
而且,jaccard是少数不使用余弦相似性的算法之一 。它标记单词并计算交集 。
我们使用NLTK对文本进行预处理 。
步骤:
- 小写所有文本
- 标识化
- 删除停用词
- 删除标点符号
- 词根化
- 计算两个文档中的交集/并集
import stringimport nltknltk.download('stopwords')nltk.download('wordnet')nltk.download('punkt')from nltk.corpus import stopwordsfrom nltk.tokenize import word_tokenizefrom nltk.stem import WordNetLemmatizerlemmatizer = WordNetLemmatizer()base_document = "This is an example sentence for the document to be compared"documents = ["This is the collection of documents to be compared against the base_document"]def preprocess(text):# 步骤:# 1. 小写字母# 2. 词根化# 3. 删除停用词# 4. 删除标点符号# 5. 删除长度为1的字符lowered = str.lower(text)stop_words = set(stopwords.words('english'))word_tokens = word_tokenize(lowered)words = []for w in word_tokens:if w not in stop_words:if w not in string.punctuation:if len(w) > 1:lemmatized = lemmatizer.lemmatize(w)words.append(lemmatized)return wordsdef calculate_jaccard(word_tokens1, word_tokens2):# 结合这两个标识来找到并集 。both_tokens = word_tokens1 + word_tokens2union = set(both_tokens)# 计算交集intersection = set()for w in word_tokens1:if w in word_tokens2:intersection.add(w)jaccard_score = len(intersection)/len(union)return jaccard_scoredef process_jaccard_similarity():# 标记我们要比较的基本文档 。base_tokens = preprocess(base_document)# 标记每一篇文档all_tokens = []for i, document in enumerate(documents):tokens = preprocess(document)all_tokens.append(tokens)print("making word tokens at index:", i)all_scores = []for tokens in all_tokens:score = calculate_jaccard(base_tokens, tokens)all_scores.append(score)highest_score = 0highest_score_index = 0for i, score in enumerate(all_scores):if highest_score < score:highest_score = scorehighest_score_index = imost_similar_document = documents[highest_score_index]print("Most similar document by Jaccard with the score:", most_similar_document, highest_score)process_jaccard_similarity()
TF-IDF这是自1972年以来出现的另一种成熟算法 。经过几十年的测试,它是Elasticsearch的默认搜索实现 。Scikit learn提供了不错的TF-IDF的实现 。TfidfVectorizer允许任何人尝试此操作 。
利用scikit-learn的余弦相似度计算TF-IDF词向量的结果 。我们将在其余的例子中使用这种余弦相似性 。余弦相似性是许多机器学习任务中使用的一个非常重要的概念,可能值得你花时间熟悉一下 。
多亏了scikit learn,这个算法产生了最短的代码行 。
from sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similaritybase_document = "This is an example sentence for the document to be compared"documents = ["This is the collection of documents to be compared against the base_document"]def process_tfidf_similarity():vectorizer = TfidfVectorizer()# 要生成统一的向量,首先需要将两个文档合并 。documents.insert(0, base_document)embeddings = vectorizer.fit_transform(documents)cosine_similarities = cosine_similarity(embeddings[0:1], embeddings[1:]).flatten()highest_score = 0highest_score_index = 0for i, score in enumerate(cosine_similarities):if highest_score < score:highest_score = scorehighest_score_index = imost_similar_document = documents[highest_score_index]print("Most similar document by TF-IDF with the score:", most_similar_document, highest_score)process_tfidf_similarity()
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 2020年中国搜索引擎行业市场现状及发展前景分析
- 2020年中考语文阅读答题技巧六招
- 西安全运会时间是何时?
- 2020年人气最高的十大NLP库推荐
- 谷歌软件工程师是怎样写设计文档的?
- 如何把表格做成文档?
- 2020年8月程序员工资最新统计:这些岗位月薪可达6万以上
- 进销存软件十大排名
- 文档智能:数字化转型的技术基石
- 如何用手机将pdf转换成word文档?具体怎么转?