TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Show HN: String Similarity Comparison in Google Sheets

1 点作者 maximedb大约 5 年前
Hi HN,<p>With the following function, you can compare the semantic of two strings in Google Sheet. It is essentially a wrapper around the Universal Sentence Encoder [1].<p>It will encode two sentences and return the cosine similarity between the two embeddings. It works with English, and 15 other languages. It can compare two sentences even if they are in different languages.<p>For example: SIMILARITY(&quot;Hacker News is a social news website focusing on computer science and entrepreneurship&quot;, &quot;Hacker News est un site d&#x27;actualités sociales axé sur l&#x27;informatique et l&#x27;entrepreneuriat&quot;) returns 0.88 While SIMILARITY(&quot;Hacker News is a social news website focusing on computer science and entrepreneurship&quot;, &quot;The site was created by Paul Graham in February 2007&quot;) returns 0.17.<p>I initially built it to align instructions in two different languages, and figured it could be useful for other people as well.<p>To load it into Google Sheets, you can open a Sheet, click on &quot;Tools&quot;, &quot;Script Editor&quot; and copy paste the function. It should now be available within the Sheet.<p>Let me know what you do with it :-)<p>Maxime.<p><pre><code> &#x2F;** * Multilingual semantic similarity between two string based on Google&#x27;s Universal Sentence Encoder and cosine similarity. * * @param {string} str_1 The first text. * @param {string} str_2 The second text. * @return {float} Number between -1 and 1, where a number of 1 means the two texts are semantically similar. * @customfunction *&#x2F; function SIMILARITY(str_1, str_2) { var data = {&#x27;str_1&#x27;: str_1, &#x27;str_2&#x27;: str_2}; var options = { &#x27;method&#x27; : &#x27;post&#x27;, &#x27;contentType&#x27;: &#x27;application&#x2F;json&#x27;, &#x27;payload&#x27; : JSON.stringify(data) }; response = UrlFetchApp.fetch(&#x27;https:&#x2F;&#x2F;neuralyst.io&#x2F;string_similarity&#x27;, options); sim = JSON.parse(response.getContentText()) return sim[&quot;similarity&quot;] } </code></pre> [1]: https:&#x2F;&#x2F;tfhub.dev&#x2F;google&#x2F;universal-sentence-encoder-multilingual-large&#x2F;3

暂无评论

暂无评论