Skip to content

Commit bc09ef4

Browse files
committed
feat(importer): add RelatedWorkAnnotator to append related-work summaries to comment-<username> (#14085)
This helper takes a BibEntry, a username, the citing paper's key, and a summary sentence, and appends a block like: [LunaOstos_2024]: <summary> to the field comment-<username>. If that field already has content, the new block is appended after a blank line. Includes unit tests verifying first append and multi-append behavior.
1 parent d1172c2 commit bc09ef4

File tree

2 files changed

+206
-0
lines changed

2 files changed

+206
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.jabref.logic.importer;
2+
3+
import java.util.Optional;
4+
5+
import org.jabref.model.entry.BibEntry;
6+
import org.jabref.model.entry.field.Field;
7+
import org.jabref.model.entry.field.FieldFactory;
8+
9+
/**
10+
* Enriches
11+
* a
12+
* BibEntry
13+
* with
14+
* contextual
15+
* summaries
16+
* extracted
17+
* from
18+
* a
19+
* paper's
20+
* "Related
21+
* Work"
22+
* /
23+
* literature
24+
* review
25+
* section.
26+
* For
27+
* each
28+
* reference
29+
* mentioned
30+
* in
31+
* the
32+
* related-work
33+
* section,
34+
* this
35+
* class
36+
* adds
37+
* a
38+
* descriptive
39+
* note
40+
* to
41+
* the
42+
* corresponding
43+
* BibEntry
44+
* field
45+
* in
46+
* the
47+
* format:
48+
* [CitingPaperKey]:
49+
* Summary
50+
* sentence.
51+
* The
52+
* note
53+
* is
54+
* stored
55+
* under
56+
* a
57+
* user-specific
58+
* field
59+
* name
60+
* like
61+
* "comment-<username>".
62+
* If
63+
* the
64+
* field
65+
* already
66+
* contains
67+
* text,
68+
* the
69+
* new
70+
* block
71+
* is
72+
* appended
73+
* after
74+
* a
75+
* blank
76+
* line.
77+
*/
78+
public class RelatedWorkAnnotator {
79+
80+
/**
81+
* Appends
82+
* a
83+
* related-work
84+
* summary
85+
* to
86+
* the
87+
* given
88+
* BibEntry.
89+
*
90+
* @param entry The
91+
* BibEntry
92+
* being
93+
* annotated
94+
* @param username The
95+
* username
96+
* to
97+
* build
98+
* the
99+
* comment
100+
* field
101+
* (e.g.,
102+
* "koppor"
103+
* →
104+
* "comment-koppor")
105+
* @param citingPaperKey The
106+
* citation
107+
* key
108+
* of
109+
* the
110+
* source
111+
* paper
112+
* (e.g.,
113+
* "LunaOstos_2024")
114+
* @param summarySentence The
115+
* extracted
116+
* related-work
117+
* summary
118+
* text
119+
*/
120+
public static void appendSummaryToEntry(
121+
BibEntry entry,
122+
String username,
123+
String citingPaperKey,
124+
String summarySentence
125+
) {
126+
// Field name pattern requested by maintainers: comment-<username>
127+
String fieldName = "comment-" + username;
128+
129+
// Resolve to a Field implementation (UserSpecificCommentField via FieldFactory)
130+
Field commentField = FieldFactory.parseField(fieldName);
131+
132+
// Format the new summary block
133+
String formattedBlock = "[" + citingPaperKey + "]: " + summarySentence.trim();
134+
135+
// Retrieve any existing content for this user-specific comment field
136+
Optional<String> existing = entry.getField(commentField);
137+
138+
// Append the new summary, separated by a single blank line if content already exists
139+
String newValue = existing
140+
.map(old -> old.strip() + "\n\n" + formattedBlock)
141+
.orElse(formattedBlock);
142+
143+
// Write it back into the BibEntry
144+
entry.setField(commentField, newValue);
145+
}
146+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.jabref.logic.importer;
2+
3+
import java.util.Optional;
4+
5+
import org.jabref.model.entry.BibEntry;
6+
import org.jabref.model.entry.field.Field;
7+
import org.jabref.model.entry.field.FieldFactory;
8+
9+
import org.junit.jupiter.api.Test;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
13+
public class RelatedWorkAnnotatorTest {
14+
15+
@Test
16+
public void firstAppendCreatesFieldWithOneBlock() {
17+
BibEntry entry = new BibEntry();
18+
19+
RelatedWorkAnnotator.appendSummaryToEntry(
20+
entry,
21+
"yourusername",
22+
"LunaOstos_2024",
23+
"Colombia is a middle-income country with a population of approximately 50 million."
24+
);
25+
26+
Field commentField = FieldFactory.parseField("comment-yourusername");
27+
Optional<String> value = entry.getField(commentField);
28+
29+
String expected =
30+
"[LunaOstos_2024]: Colombia is a middle-income country with a population of approximately 50 million.";
31+
32+
assertEquals(expected, value.orElseThrow());
33+
}
34+
35+
@Test
36+
void secondAppendAddsBlankLineAndSecondBlock() {
37+
BibEntry entry = new BibEntry();
38+
RelatedWorkAnnotator.appendSummaryToEntry(
39+
entry,
40+
"koppor",
41+
"LunaOstos_2024",
42+
"Colombia is a middle-income country with a population of approximately 50 million."
43+
);
44+
45+
RelatedWorkAnnotator.appendSummaryToEntry(
46+
entry,
47+
"koppor",
48+
"CIA_2021",
49+
"Colombia has ~50 million people."
50+
);
51+
52+
Optional<String> value = entry.getField(FieldFactory.parseField("comment-koppor"));
53+
54+
String expected =
55+
"[LunaOstos_2024]: Colombia is a middle-income country with a population of approximately 50 million.\n\n" +
56+
"[CIA_2021]: Colombia has ~50 million people.";
57+
58+
assertEquals(expected, value.orElseThrow());
59+
}
60+
}

0 commit comments

Comments
 (0)