Skip to content

Commit c2bcd87

Browse files
committed
fix(pos-tagging): parse all sentences in passage, not just first
- Changed doc.json()[0] to iterate all sentences via doc.json() - dependency-parser.js now uses flatMap to combine terms from all sentences - Fixes issue where only first sentence was analyzed in passage selection
1 parent dcc52e4 commit c2bcd87

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

demos/pos-tagging/js/dependency-parser.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ const DependencyParser = {
1313

1414
extractDependencies(doc, text) {
1515
const dependencies = [];
16-
const json = doc.json()[0];
16+
const allSentences = doc.json();
1717

18-
if (!json || !json.terms) {
18+
if (!allSentences || allSentences.length === 0) {
1919
return dependencies;
2020
}
2121

22-
const terms = json.terms;
22+
const terms = allSentences.flatMap(sentence => sentence.terms || []);
2323
const words = terms.map((t, idx) => ({
2424
id: idx,
2525
word: t.text,

demos/pos-tagging/js/pos-tagger.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,32 @@ const POSTagger = {
4242

4343
// Extract terms with their POS tags
4444
const terms = doc.terms().out('array');
45-
const json = doc.json()[0];
45+
const allSentences = doc.json();
4646

4747
let html = '<div class="pos-tokens">';
4848
const tagCounts = {};
49-
50-
if (json && json.terms) {
51-
json.terms.forEach((term, index) => {
52-
const tags = term.tags || [];
53-
const word = term.text || terms[index];
54-
const primaryTag = this.getPrimaryTag(tags);
55-
const tagClass = this.getTagClass(primaryTag);
56-
57-
tagCounts[primaryTag] = (tagCounts[primaryTag] || 0) + 1;
58-
59-
html += `
60-
<span class="pos-token ${tagClass}" data-word="${word}" data-tag="${primaryTag}" data-index="${index}">
61-
<span class="word">${word}</span>
62-
<span class="pos-tag">${primaryTag}</span>
63-
</span>
64-
`;
65-
});
66-
}
49+
let termIndex = 0;
50+
51+
allSentences.forEach((sentence) => {
52+
if (sentence && sentence.terms) {
53+
sentence.terms.forEach((term) => {
54+
const tags = term.tags || [];
55+
const word = term.text || terms[termIndex];
56+
const primaryTag = this.getPrimaryTag(tags);
57+
const tagClass = this.getTagClass(primaryTag);
58+
59+
tagCounts[primaryTag] = (tagCounts[primaryTag] || 0) + 1;
60+
61+
html += `
62+
<span class="pos-token ${tagClass}" data-word="${word}" data-tag="${primaryTag}" data-index="${termIndex}">
63+
<span class="word">${word}</span>
64+
<span class="pos-tag">${primaryTag}</span>
65+
</span>
66+
`;
67+
termIndex++;
68+
});
69+
}
70+
});
6771

6872
html += '</div>';
6973
output.innerHTML = html;

0 commit comments

Comments
 (0)