Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b6BHcz4muRH44T3br3
61 lines
2.6 KiB
Java
61 lines
2.6 KiB
Java
package com.ankurm.codesearch;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import java.util.List;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
/** Ranking behaviour of the hashing embedder: what it finds, and the honest limit of a word-overlap embedder. */
|
|
class SearchTest {
|
|
|
|
private static String top(VectorIndex index, String query) {
|
|
return index.search(query, 1).get(0).unit().shortName();
|
|
}
|
|
|
|
@Test
|
|
void findsMethodsByWhatTheyDo() throws Exception {
|
|
VectorIndex index = Fixtures.hashingIndex();
|
|
Transcript t = new Transcript("02-search.txt", "search_code ranking with the " + index.embedder().name()
|
|
+ " embedder (top 3 per query)");
|
|
List<String> queries = List.of(
|
|
"charge the customer's card",
|
|
"take units off the shelf for an order",
|
|
"email the customer that the order was cancelled",
|
|
"find an order by its id",
|
|
"return money to the customer");
|
|
for (String q : queries) {
|
|
t.section("query: " + q);
|
|
for (VectorIndex.Hit h : index.search(q, 3)) {
|
|
t.line(String.format("%.3f %-34s %s", h.score(), h.unit().shortName(), h.unit().location()));
|
|
}
|
|
}
|
|
|
|
assertThat(top(index, "charge the customer's card")).isEqualTo("PaymentService#charge");
|
|
assertThat(top(index, "take units off the shelf for an order")).isEqualTo("InventoryService#reserve");
|
|
assertThat(top(index, "email the customer that the order was cancelled"))
|
|
.isEqualTo("EmailNotifier#sendCancellation");
|
|
assertThat(top(index, "find an order by its id")).isEqualTo("OrderRepository#findById");
|
|
assertThat(top(index, "return money to the customer")).isEqualTo("PaymentService#refund");
|
|
|
|
// The same query typed in the vocabulary the code does NOT use. No shared words, no score.
|
|
t.section("the limit: a query that shares no words with the code");
|
|
String paraphrase = "chargeback";
|
|
List<VectorIndex.Hit> none = index.search(paraphrase, 3);
|
|
t.line("query: " + paraphrase + " -> " + none.size() + " result(s)");
|
|
t.write();
|
|
assertThat(none).isEmpty();
|
|
}
|
|
|
|
@Test
|
|
void identicalTextEmbedsToIdenticalVectors() {
|
|
HashingEmbedder e = new HashingEmbedder(512);
|
|
assertThat(e.embed("placeOrder reserve stock")).isEqualTo(e.embed("placeOrder reserve stock"));
|
|
}
|
|
|
|
@Test
|
|
void camelCaseIsSplitIntoWords() {
|
|
assertThat(HashingEmbedder.words("placeOrderHTTPClient")).containsExactly("place", "order", "http", "client");
|
|
}
|
|
}
|