iOS开发 - Natural Language Processing(NLP)其五

使用NLEmbedding判断两个词之间的相似度,或者找出相近的词

参考自苹果官方文档

import NaturalLanguage

if let embedding = NLEmbedding.wordEmbedding(for: .english) {
        let word = "bicycle"
        
        if let vector = embedding.vector(for: word) {
            // 这里输出的是一堆坐标, 我也不太懂,具体可以看苹果文档
            print(vector)
        }
        
        // 判断两个单词的相似度 
        let specificDistance = embedding.distance(between: word, and: "motorcycle")
        print(specificDistance.description)
        
        // 找出相近的单词
        embedding.enumerateNeighbors(for: word, maximumCount: 5) { neighbor, distance in
            print("\(neighbor): \(distance.description)")
            return true
        }
    }

输入内容为

[0.058414656668901443, -0.25087594985961914, 0.12101634591817856, -0.034709520637989044, -0.36045578122138977, 0.19146504998207092, 0.26516926288604736, 0.12462664395570755, -0.35444536805152893, -0.31913524866104126, -0.026910871267318726, 0.13916222751140594, 0.3668704926967621, 0.2724900245666504, -0.19654138386249542, -0.12179755419492722, 0.1707746833562851, 0.01962774433195591, -0.1324276626110077, 0.07456113398075104, 0.18366341292858124, -0.030035221949219704, -0.014665620401501656, 0.09057152271270752, -0.16684590280056, -0.19787098467350006, -0.09259337931871414, 0.16191668808460236, 0.7499357461929321, 0.15257801115512848, 0.03841571882367134, 0.20841209590435028, -0.02809247560799122, -0.08845147490501404, 0.16226859390735626, -0.2384243756532669, -0.20345163345336914, -0.09253140538930893, 0.13795681297779083, -0.14881429076194763, 0.2510822117328644, -0.18238943815231323, -0.007291943300515413, 0.16027987003326416, 0.04753401130437851, 0.12628954648971558, -0.1103496104478836, 0.10222633928060532, 0.18639017641544342, -0.32423022389411926, -0.044082775712013245, -0.219167098402977, 0.19944719970226288, 0.008337260223925114, 0.0806577205657959, 0.08079825341701508, 0.06795304268598557, -0.17127525806427002, -0.13504379987716675, 0.09130847454071045, -0.16369464993476868, 0.19706866145133972, 0.018244672566652298, -0.06518466770648956, 0.04954404756426811, 0.07646973431110382, -0.1417507380247116, 0.16250942647457123, -0.2452232390642166, -0.21835957467556, 0.135381817817688, 0.5148354768753052, -0.06402509659528732, -0.13192516565322876, 0.15186171233654022, 0.019197646528482437, 0.007794803008437157, 0.04603588208556175, -0.1263083815574646, -0.22878339886665344, -0.1557217687368393, 0.372063547372818, -0.0373426117002964, 0.1363808959722519, 0.12275811284780502, 0.09846553206443787, 0.31667762994766235, 0.16145482659339905, -0.14684531092643738, 0.06128164380788803, 0.12848253548145294, -0.1661820411682129, -0.4681834280490875, -0.03138761967420578, 0.1807822585105896, 0.08985965698957443, 0.417726069688797, 0.2400333434343338, 0.2225472629070282, -0.2301950454711914, -0.05511731281876564, 0.3110302686691284, -0.052057355642318726, -0.0715303122997284, 0.08471546322107315, 0.0393986739218235, 0.27300217747688293, -0.2617819309234619, -0.17688460648059845, 0.13667422533035278, -0.24435320496559143, -0.24743685126304626, -0.48924022912979126, 0.2075742483139038, -0.126471608877182, 0.14269497990608215, 0.18492640554904938, -0.13177749514579773, 0.346725195646286, 0.04800662398338318, 0.16631579399108887, 0.21135462820529938, -0.11171478778123856, 0.24323169887065887, -0.08035250008106232, -0.10653434693813324, 0.21002019941806793, 0.14387564361095428, -0.1184711903333664, 0.38746213912963867, 0.034577030688524246, 0.010906713083386421, -0.18139947950839996, 0.07238200306892395, 0.12513509392738342, 0.00238217250443995, -0.026031941175460815, -0.20359167456626892, 0.0881090834736824, -0.15074731409549713, -0.1838686764240265, -0.180923193693161, 0.14445866644382477, -0.054731741547584534, 0.29237496852874756, 0.16877496242523193, -0.2067742943763733, 0.08828303962945938, 0.056671611964702606, -0.1566367894411087, -0.09450973570346832, -0.07833779603242874, 0.14653263986110687, 0.2449011355638504, 0.21840690076351166, 0.19449476897716522, 0.3678646683692932, 0.328596293926239, 0.03698339685797691, -0.034400466829538345, 0.2241678237915039, -0.29276254773139954, -0.31935128569602966, -0.1684442162513733, 0.1294090747833252, -0.39031752943992615, -0.22752465307712555, 0.1553145796060562, 0.3134559094905853, 0.19595462083816528, -0.08359473943710327, 0.06348927319049835, 0.0721411481499672, -0.3869245946407318, -0.13961222767829895, 0.053616978228092194, 0.09641863405704498, 0.19515110552310944, 0.030837612226605415, 0.06092118099331856, -0.1998605728149414, -0.018254505470395088, 0.19289228320121765, 0.19957603514194489, -0.5865831971168518, 0.05474434047937393, -0.0029402091167867184, -0.08911344408988953, 0.06713080406188965, 0.16638575494289398, 0.04814984276890755, -0.10231300443410873, -0.09877048432826996, 0.07186438143253326, -0.08074533939361572, -0.11819453537464142, 0.1967034786939621, 0.1433805525302887, -0.40883350372314453, -0.008211582899093628, 0.27367520332336426, -0.10528986901044846, 0.07435476779937744, -0.20895391702651978, 0.13156025111675262, -0.0808970257639885, 0.20709556341171265, -0.3241032063961029, 0.16446498036384583, -0.33715930581092834, 0.3724488317966461, 0.09043803066015244, -0.07771096378564835, 0.09610052406787872, -0.12316540628671646, -0.10862048715353012, 0.1704767644405365, 0.11810661852359772, 0.16911953687667847, 0.25428569316864014, 0.08935682475566864, 0.022980881854891777, -0.2574755549430847, 0.24424013495445251, -0.3177327513694763, -0.18773534893989563, -0.12423103302717209, -0.007335025817155838, 0.18181395530700684, -0.12843608856201172, -0.09545213729143143, 0.3326270580291748, -0.05838458240032196, -0.0424540713429451, -0.20610007643699646, 0.1339200735092163, -0.02889547497034073, -0.19694668054580688, 0.026576591655611992, -0.010353518649935722, -0.051649466156959534, 0.12474855780601501, -0.05421314388513565, -0.1695546656847, -0.06450922787189484, -0.18623238801956177, 0.10362828522920609, -0.12101973593235016, -0.028737079352140427, 0.09171650558710098, 0.0550367496907711, 0.17494677007198334, 0.3356688320636749, -0.03701328486204147, -0.1130589172244072, -0.08170109242200851, 0.28500068187713623, 0.28781673312187195, -0.09859047085046768, -0.09122917056083679, -0.27212661504745483, 0.06395787745714188, -0.2636392414569855, 0.0875888541340828, -0.3750556707382202, 0.09118355810642242, 0.1583615243434906, 0.014971546828746796, 0.10588812083005905, 0.15917468070983887, 0.1820458322763443, 0.19777913391590118, 0.3957436978816986, -0.013862145133316517, -0.09172341972589493, -0.047548308968544006, -0.051934514194726944, -0.13626350462436676, -0.07495589554309845, 0.20618733763694763, 0.19354082643985748, 0.043335918337106705, -0.07557958364486694, -0.23410408198833466, 0.3000260889530182, -0.10355589538812637, -0.17611752450466156, -0.15831159055233002, 0.12410072982311249, 0.010538061149418354, -0.023112887516617775, -0.015310563147068024, 0.034895893186330795, -0.08509046584367752, -0.18931564688682556, -0.09818512201309204, 0.09296619892120361, 0.17092865705490112, -0.23091039061546326, 0.18858332931995392]
0.7607445120811462
bike: 0.6568421125411987
motorcycle: 0.7607445120811462
scooter: 0.78237384557724
tricycle: 0.8188069462776184
motorbike: 0.8712705373764038
Program ended with exit code: 0
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,001评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,210评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,874评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,001评论 1 291
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,022评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,005评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,929评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,742评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,193评论 1 309
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,427评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,583评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,305评论 5 342
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,911评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,564评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,731评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,581评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,478评论 2 352

推荐阅读更多精彩内容