使用NLEmbedding判断两个句子之间的相似度
参考自苹果官方文档
import NaturalLanguage
if let sentenceEmbedding = NLEmbedding.sentenceEmbedding(for: .english) {
let sentence = "This is a sentence."
if let vector = sentenceEmbedding.vector(for: sentence) {
print(vector)
}
let distance = sentenceEmbedding.distance(between: sentence, and: "That is a sentence.")
print(distance.description)
}
输入内容为
[-0.25659626722335815, 0.1357254981994629, 0.1929783821105957, 0.15774355828762054, -0.365226686000824, 0.17114803194999695, -0.6219163537025452, -0.4776921570301056, -0.005722783971577883, -0.0836319699883461, 0.014699215069413185, -0.42567920684814453, -0.23798930644989014, 0.20926454663276672, 0.6642435193061829, -0.1963643729686737, -0.02212776616215706, 0.04451689496636391, -0.16875247657299042, -0.19401562213897705, -0.311017781496048, 0.5549156069755554, 0.11319614946842194, 0.34976163506507874, -0.25161632895469666, -0.11633099615573883, -0.09901773184537888, -0.5031660795211792, -0.09481707960367203, 0.18967662751674652, -0.1948888599872589, -0.2679033577442169, 0.08706939965486526, 0.01981808990240097, -0.20947669446468353, -0.15234096348285675, -0.4208841323852539, -0.003081216011196375, -0.4299457371234894, -0.4760977327823639, -0.33475470542907715, 0.34413230419158936, -0.7858260273933411, -0.5337638258934021, 0.48208776116371155, 0.5216966867446899, -0.21806180477142334, 0.11308607459068298, -0.11904209852218628, 0.027058692649006844, 0.44565457105636597, -0.24677792191505432, 0.2066841721534729, -0.006213911343365908, 0.06905283778905869, 0.15476097166538239, -0.024923840537667274, -0.3460434079170227, 0.2603304982185364, 0.0625147670507431, -0.42747166752815247, 0.44283875823020935, 0.7148281335830688, -0.06250900775194168, -0.31836071610450745, -0.11238574236631393, 0.06233997642993927, -0.7026103734970093, -0.1431499719619751, -0.19084951281547546, -0.38528549671173096, -0.418243944644928, -0.061228152364492416, -0.0074983579106628895, 0.6046348214149475, 0.3426325023174286, -0.5154200792312622, -0.2928774058818817, -0.262647807598114, 0.10796209424734116, 0.3054550290107727, 0.4023045003414154, 0.08453241735696793, 0.39642009139060974, 0.059937261044979095, 0.2537965476512909, 0.2081659883260727, -0.07636106014251709, -0.04274395480751991, -0.18960173428058624, 0.4529116749763489, 0.6207144260406494, -0.1589542180299759, -0.27837345004081726, 0.07854079455137253, -0.2296316772699356, 0.44727998971939087, -0.22236157953739166, -0.12015417218208313, 0.10763970017433167, -0.16836683452129364, 0.06897560507059097, 0.38094693422317505, 0.3875732123851776, -0.34038740396499634, -0.1696585863828659, -0.548845112323761, -0.4594101011753082, 0.038416534662246704, 0.15015128254890442, 0.6279408931732178, -0.23301216959953308, 0.10581537336111069, -0.6972524523735046, 0.4767455458641052, 0.10180055350065231, -0.41426634788513184, 0.22171130776405334, -0.5118834376335144, -0.5362141132354736, -0.12596988677978516, -0.12406270205974579, -0.40836575627326965, -0.2143765538930893, 0.7171411514282227, 0.47563406825065613, 0.5723217725753784, -0.40840134024620056, -0.14443008601665497, -0.015862436965107918, 0.0002315104502486065, -0.13778072595596313, -0.6511927843093872, 0.30464276671409607, -0.0679207444190979, -0.10095140337944031, 0.22920721769332886, 0.20542480051517487, -0.12307266145944595, -0.02752857841551304, 0.2835046350955963, 0.07802530378103256, -0.27916306257247925, -0.08063212782144547, 0.6234082579612732, -0.29654234647750854, 0.00432041147723794, -0.6626229882240295, -0.21256375312805176, 0.10731089115142822, -0.3976036012172699, 0.18159395456314087, 0.4228482246398926, -0.31565138697624207, 0.30963051319122314, -0.3629244565963745, -0.473956435918808, 0.4513759911060333, -0.34892621636390686, -0.2622601091861725, 0.2028331458568573, -0.40404143929481506, -0.49312710762023926, -0.7344995737075806, -0.20518061518669128, 0.20197106897830963, 0.34585824608802795, -0.38313934206962585, 0.12812277674674988, 0.3603131175041199, -0.2840372920036316, 0.014529293403029442, 0.31743404269218445, -0.06657212972640991, 0.13789282739162445, 0.4374715983867645, 0.025340868160128593, -0.07437372207641602, 0.18102698028087616, 0.0009441988659091294, 0.20942369103431702, -0.4820462763309479, 0.05358094722032547, 0.11328224837779999, -0.25377172231674194, 0.4962466359138489, -0.31318849325180054, 0.16674675047397614, 0.2631820738315582, 0.07583090662956238, 0.41168221831321716, -0.360760360956192, 0.10191415995359421, 0.09772831946611404, -0.36487218737602234, -0.041512101888656616, -0.41431012749671936, -0.21547938883304596, 0.009917166084051132, -0.39263251423835754, -0.1683722883462906, -0.2694246470928192, 0.4125969409942627, 0.02437983825802803, -0.08624843508005142, -0.4826882779598236, 0.10621637850999832, 0.2788122296333313, 0.24997010827064514, -0.0960673913359642, 0.12084110826253891, 0.1844930350780487, -0.1922929435968399, -0.0523492768406868, 0.12406837195158005, 0.019418273121118546, -0.43867257237434387, -0.29155415296554565, -0.02107563056051731, -0.07976856082677841, 0.3244590163230896, -0.243102565407753, 0.14367221295833588, -0.6501134037971497, 0.4485422968864441, 0.547630250453949, -0.2996346354484558, -0.13539889454841614, -0.22017742693424225, -0.5047897696495056, -0.25959834456443787, -0.3788253366947174, 0.4482175409793854, 0.09728521853685379, -0.19029997289180756, -0.20207984745502472, -0.27711689472198486, 0.5143901705741882, -0.24511753022670746, -0.45899131894111633, 0.2645857632160187, 0.3756083548069, 0.02637956663966179, -0.32629695534706116, -0.35500308871269226, -0.22308029234409332, 0.16146019101142883, -0.31720590591430664, 0.4130919277667999, -0.3963209092617035, -0.3034976124763489, -0.40699461102485657, -0.31102532148361206, -0.06661076843738556, 0.36926499009132385, 0.4795459508895874, 0.01952577754855156, -0.035349998623132706, 0.5328801274299622, -0.008545123040676117, -0.3934905529022217, 0.3107312023639679, 0.08881092816591263, -0.41651564836502075, -0.03796031326055527, -0.28583917021751404, 0.4103431701660156, 0.34717610478401184, -0.22284795343875885, -0.14230911433696747, 0.01541119534522295, -0.03978265821933746, 0.11480704694986343, -0.1671055108308792, 0.026644205674529076, -0.6754336953163147, -0.14664210379123688, 0.1226801797747612, 0.16086368262767792, -0.058453526347875595, -0.292252779006958, -0.49888601899147034, -0.10559208691120148, -0.0538216158747673, 0.016937382519245148, -0.048050232231616974, -0.06213676184415817, -0.08132554590702057, 0.40608224272727966, -0.029565900564193726, 0.10564029216766357, -0.49554193019866943, 0.07508430629968643, -0.6826936602592468, -0.21427451074123383, 0.22528176009655, 0.494314968585968, 0.12730686366558075, 0.34584757685661316, 0.15238365530967712, 0.22136206924915314, 0.13720527291297913, -0.49305999279022217, -0.408137708902359, -0.3651067614555359, 0.47234880924224854, 0.17336492240428925, 0.3447459042072296, -0.3944492042064667, -0.13485345244407654, 0.3458622097969055, -0.3756307065486908, 0.42383986711502075, -0.5379300117492676, -0.23995530605316162, 0.31789934635162354, 0.2952980399131775, -0.2530354857444763, -0.14266815781593323, -0.4418592154979706, -0.47465401887893677, 0.34422582387924194, 0.3869817852973938, 0.6917713284492493, 0.03590227663516998, 0.5861415863037109, 0.11357196420431137, 0.30130332708358765, -0.4141646921634674, 0.5231361389160156, 0.44032639265060425, 0.3512619435787201, -0.31521740555763245, 0.5782845616340637, 0.4394616186618805, -0.2904038429260254, 0.09380517154932022, 0.26982563734054565, 0.40993380546569824, 0.23015877604484558, 0.25424209237098694, -0.02859361656010151, -0.23593734204769135, -0.542421281337738, -0.27232280373573303, 0.38357028365135193, -0.14045414328575134, 0.6898132562637329, 0.25896233320236206, -0.3029452860355377, 0.39888542890548706, -0.1991119384765625, 0.44331812858581543, 0.13778631389141083, -0.39179542660713196, -0.5512662529945374, -0.8935235738754272, 3.5701905289897695e-05, 0.06106402352452278, 0.24462851881980896, 0.32577675580978394, 0.3839517831802368, 0.49847695231437683, 0.4380601942539215, 0.5656253099441528, -0.10804320126771927, -0.6030610203742981, -0.05058521404862404, -0.48193955421447754, -0.3987581133842468, -0.33518972992897034, 0.3697035014629364, 0.10066421329975128, -0.020082244649529457, 0.354574054479599, 0.05719191953539848, -0.22921670973300934, -0.3324872553348541, -0.23838047683238983, 0.029956029728055, -0.3273994028568268, 0.20048843324184418, 0.08805402368307114, 0.23176170885562897, 0.14956972002983093, -0.17859382927417755, 0.340751975774765, -0.04777548834681511, 0.4257888197898865, 0.6458972096443176, 0.03135702386498451, -0.19353143870830536, -0.010682101361453533, 0.6441326141357422, 0.6395446062088013, 0.650346577167511, 0.48088541626930237, -0.2704561948776245, 0.4883553683757782, -0.18177412450313568, -0.18909020721912384, 0.35521531105041504, 0.18770073354244232, 0.4878785312175751, 0.12758775055408478, -0.13231469690799713, -0.0780620276927948, 0.3115662932395935, -0.27839377522468567, -0.6880404353141785, 0.1477312594652176, 0.011088608764111996, -0.17300044000148773, 0.4077470898628235, -0.1293305605649948, 0.17159564793109894, -0.39378437399864197, 0.4323224127292633, -0.37300705909729004, -0.14214491844177246, -0.5184841752052307, -0.34659749269485474, -0.15535056591033936, -0.20086340606212616, 0.8542085886001587, -0.053139809519052505, 0.5473248958587646, -0.014263818971812725, -0.16418340802192688, -0.4171646237373352, 0.050325751304626465, 0.42336979508399963, 0.4563436210155487, -0.3453617990016937, -0.5428771376609802, 0.12894849479198456, 0.025298738852143288, 0.03890717029571533, -0.026251746341586113, -0.3650316596031189, -0.41660723090171814, 0.18777316808700562, -0.26996925473213196, -0.11506855487823486, -0.37336039543151855, 0.3339591920375824, 0.24442672729492188, -0.7886455059051514, -0.028916578739881516, -0.2729646563529968, -0.11052589863538742, 0.4273551106452942, -0.2878912091255188, 0.19260919094085693, -0.033567722886800766, -0.17571741342544556, -0.7280767560005188, -0.47906479239463806, -0.32778292894363403, -0.036383144557476044, 0.5197468400001526, -0.38086310029029846, -0.3465740382671356, 0.6573759913444519, 0.8058639168739319, -0.0909695029258728, -0.32418307662010193, 0.16189974546432495, -0.027333395555615425, 0.5328613519668579, -0.13380727171897888, 0.42215022444725037, -0.12993207573890686, 0.265167772769928, 0.20254993438720703, -0.1375361979007721, 0.36798787117004395, 0.1610689014196396, -0.28135988116264343, -0.6778460741043091, 0.10276984423398972, -0.3211008310317993, -0.090669646859169, -0.30739688873291016, -0.371553510427475, -0.006808515638113022, 0.00845095980912447, 0.03552285209298134, 0.038217976689338684, 0.04925365746021271, 0.5934272408485413, -0.16779880225658417, -0.43189212679862976, -0.6211078763008118, -0.21536199748516083, 0.36513030529022217, 0.4487934112548828, 0.25657179951667786, -0.5114122629165649, -0.02083984576165676, 0.0936826765537262, 0.09592243283987045, -0.046700503677129745, -0.048101868480443954, 0.45504364371299744, 0.28170961141586304, -0.26572108268737793, -0.1506902277469635, -0.2540648281574249, -0.0705292820930481, 0.5006968379020691, 0.29493269324302673]
0.40913236141204834