Created
July 5, 2021 13:34
-
-
Save FreeFly19/4730ca9e637150f14bcf0607c3d66bc6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.Data; | |
import java.util.*; | |
public class MainIndex { | |
public static void main(String[] args) { | |
Map<String, Set<Post>> postFullText = new HashMap<>(); | |
Post p1 = new Post(); | |
p1.id = 1L; | |
p1.categoryId = 2L; | |
p1.createdAt = 123213213L; | |
p1.text = "Hello world"; | |
// image, hel, ell, llo, wor, orl, rld | |
for (String term: p1.text.split(" ")) { | |
Set<Post> postsByTerm = postFullText.getOrDefault(term.toLowerCase(), new HashSet<>()); | |
postsByTerm.add(p1); | |
} | |
// cup | |
// cat | |
// 1 - (ld / max("cat".length", "cup".length)) = 0.3333 | |
String search = "hello kittens"; | |
Map<Post, Float> searchResult = new HashMap<>(); | |
for (String term: search.split(" ")) { | |
Set<Post> posts = postFullText.getOrDefault(term.toLowerCase(), new HashSet<>()); | |
posts.forEach(p -> { | |
Float prevScore = searchResult.getOrDefault(p, 0f); | |
searchResult.put(p, prevScore + (1/posts.size())); | |
}); | |
} | |
} | |
@Data | |
static class Post { | |
private Long id; | |
private Long categoryId; | |
private Long createdAt; | |
private String text; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment