← 返回 bytedance 的题目列表Semantic Similarity (Text Embedding + Cosine Similarity)
类型:online_judge
Semantic Similarity (Embedding + Cosine Similarity)
Given two texts text1 and text2, and an available encoder function encode(text) -> vector (think of a Sentence Encoder or any text embedding model), implement a function to compute the semantic similarity between the two texts as the cosine similarity of their embedding vectors:
[ \text{sim}(a,b) = \frac{a \cdot b}{||a||_2 ||b||_2} ]
Input
Line 1: string text1
Line 2: string text2
Output
Print one floating-point number: the cosine similarity.
Constraints & Requirements
encode(text) returns a real-valued vector of fixed dimension d (possibly large, e.g., 256/768).
Handle zero-norm vectors: if ||a||==0 or ||b||==0, output 0.0.
Print the result with 6 digits after the decimal point.
Example
(Only format is shown; actual value depends on encode.)
Input:
I love machine learning.
I enjoy studying AI.
Output:
0.823456
Note
In interviews, pseudocode is acceptable if the encoder is assumed to exist; the key is the cosine similarity computation and edge cases.
Example
Input
I love machine learning.
I enjoy studying AI.
Output
0.000000