python 문장 유사도

심지혜·2024년 4월 16일
0
from konlpy.tag import Okt
from collections import Counter
import numpy as np

okt = Okt()

def get_nouns(text):
    return Counter(okt.nouns(text))

def make_vec(features, data):
    return np.array([data.get(f, 0) for f in features])

def sim(a, b):
    return a@b/ (np.linalg.norm(a) * np.linalg.norm(b))

A = get_nouns(input())
B = get_nouns(input())
C = get_nouns(input())

features = set(A + B + C)

va = make_vec(features, A)
vb = make_vec(features, B)
vc = make_vec(features, C)

print('va & vb', sim(va, vb))
print('vb & vc', sim(vb, vc))
print('va & vc', sim(va, vc))

0개의 댓글