aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMario Forzanini <mf@marioforzanini.com>2024-07-02 18:35:17 +0200
committerMario Forzanini <mf@marioforzanini.com>2024-07-02 18:35:17 +0200
commit69ae24f7d7c5976a4bc3d23041cdda603b0eb2f5 (patch)
treebfcefd81967a6998f348b2a260957e5673f3f75d
parente09d746e52ade0bfc9bcd7dbb5b9a617edb622b2 (diff)
Rank files based on author and title substringsHEADv0.1master
-rw-r--r--smwh.c4
-rw-r--r--std.h28
2 files changed, 28 insertions, 4 deletions
diff --git a/smwh.c b/smwh.c
index 49d7839..f501244 100644
--- a/smwh.c
+++ b/smwh.c
@@ -437,7 +437,9 @@ main(int argc, char **argv)
if (!maps[i].error_occurred) {
maps[i].tf_idf *= _idf;
maps[i].tf_idf += maps[i].tf_idf
- * (strview_cmp(term, maps[i].author) == 0);
+ * (strview_is_substr(term, maps[i].author));
+ maps[i].tf_idf += maps[i].tf_idf
+ * (strview_is_substr(term, maps[i].title));
}
}
qsort(maps, (size_t)filenames.count, sizeof(WordMap),
diff --git a/std.h b/std.h
index 514371f..035d5e6 100644
--- a/std.h
+++ b/std.h
@@ -222,6 +222,8 @@ int strview_cmp(StrView s1, StrView s2);
bool strview_contains(const StrView s, const char *charbag, const int len);
/*! Checks whether str ends with end. */
bool strview_ends_with(StrView end, StrView str);
+/*! Checks whether sub is a substring of str. */
+bool strview_is_substr(StrView sub, StrView str);
/*! Return next line in StrView.
*
* Cannot be used to count the number of lines in a file as it will
@@ -395,12 +397,12 @@ cp(const char src[static restrict 1], const char dst[static restrict 1])
FILE *fsrc = NULL, *fdst = NULL;
int errnum = 0;
- if (!(fsrc = fopen(src, "r"))) {
+ if (!(fsrc = fopen(src, "rb"))) {
errnum = errno;
goto err;
}
- if (!(fdst = fopen(dst, "w"))) {
+ if (!(fdst = fopen(dst, "wb"))) {
errnum = errno;
goto err;
}
@@ -641,6 +643,26 @@ strview_ends_with(StrView end, StrView str)
== 0;
}
+bool
+strview_is_substr(StrView sub, StrView str)
+{
+ if (sub.len > str.len) {
+ return false;
+ } else if (sub.len == str.len) {
+ return strview_cmp(sub, str) == 0;
+ }
+ for (int i = 0; i < str.len - sub.len; i++) {
+ StrView substr = {
+ .str = &str.str[i],
+ .len = str.len - i,
+ };
+ if (strview_starts_with(sub, substr) == 0) {
+ return true;
+ }
+ }
+ return false;
+}
+
StrView
strview_next_line(StrView str[static 1])
{
@@ -895,7 +917,7 @@ strview_read_file(
{
FILE *f;
- if (!(f = fopen(filename, "rb")))
+ if (!(f = fopen(filename, "r")))
return false;
if (!strview_read(r, f, contents))
return false;