Skip to content

Commit 8b7b6d8

Browse files
author
HugoFara
committed
fix(annotation): many japanese annotations were not displaying #101
1 parent 07e7b13 commit 8b7b6d8

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ ones are marked like "v1.0.0-fork".
6767

6868
### Fixed
6969

70+
* **Japanese Annotations** ([#101](https://github.com/HugoFara/lwt/issues/101)):
71+
Fixed annotations not displaying correctly in Japanese texts. The
72+
`annotationToJson()` function was using an off-by-one index that didn't
73+
match the `Ti2Order` values used by the frontend, causing approximately
74+
50% of annotations to fail to display.
7075
* **Text Parsing** ([#114](https://github.com/HugoFara/lwt/issues/114)): Fixed
7176
last word of text not being recognized when text ends without punctuation.
7277
Words at the end of a text are now correctly identified regardless of trailing

src/backend/Services/AnnotationService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function annotationToJson(string $ann): string|false
263263
foreach ($items as $item) {
264264
$vals = preg_split('/[\t]/u', $item);
265265
if (count($vals) > 3 && $vals[0] >= 0 && $vals[2] > 0) {
266-
$arr[intval($vals[0]) - 1] = array($vals[1], $vals[2], $vals[3]);
266+
$arr[intval($vals[0])] = array($vals[1], $vals[2], $vals[3]);
267267
}
268268
}
269269
$json_data = json_encode($arr);

tests/backend/Core/Text/AnnotationManagementTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,30 @@ protected function setUp(): void
2525

2626
/**
2727
* Test annotation to JSON conversion
28+
*
29+
* @since 2.9.0 Annotation keys now match Ti2Order directly (no offset)
2830
*/
2931
public function testAnnotationToJson(): void
3032
{
3133
// Empty annotation
3234
$this->assertEquals('{}', $this->annotationService->annotationToJson(''));
3335

34-
// Single annotation
36+
// Single annotation - key should match the order value (1)
3537
$annotation = "1\tword\t5\ttranslation";
3638
$result = $this->annotationService->annotationToJson($annotation);
3739
$this->assertJson($result);
3840
$decoded = json_decode($result, true);
39-
$this->assertArrayHasKey(0, $decoded);
40-
$this->assertEquals(['word', '5', 'translation'], $decoded[0]);
41+
$this->assertArrayHasKey(1, $decoded);
42+
$this->assertEquals(['word', '5', 'translation'], $decoded[1]);
4143

42-
// Multiple annotations
44+
// Multiple annotations - keys should match order values (1 and 2)
4345
$annotation = "1\tword1\t5\ttrans1\n2\tword2\t3\ttrans2";
4446
$result = $this->annotationService->annotationToJson($annotation);
4547
$this->assertJson($result);
4648
$decoded = json_decode($result, true);
4749
$this->assertCount(2, $decoded);
48-
$this->assertEquals(['word1', '5', 'trans1'], $decoded[0]);
49-
$this->assertEquals(['word2', '3', 'trans2'], $decoded[1]);
50+
$this->assertEquals(['word1', '5', 'trans1'], $decoded[1]);
51+
$this->assertEquals(['word2', '3', 'trans2'], $decoded[2]);
5052
}
5153

5254
/**
@@ -69,11 +71,11 @@ public function testAnnotationToJsonEdgeCases(): void
6971
$result = $this->annotationService->annotationToJson($annotation);
7072
$this->assertJson($result);
7173

72-
// Unicode in annotations
74+
// Unicode in annotations - key should match order value (1)
7375
$annotation = "1\t日本語\t5\ttranslation";
7476
$result = $this->annotationService->annotationToJson($annotation);
7577
$this->assertJson($result);
7678
$decoded = json_decode($result, true);
77-
$this->assertStringContainsString('日本語', $decoded[0][0]);
79+
$this->assertStringContainsString('日本語', $decoded[1][0]);
7880
}
7981
}

0 commit comments

Comments
 (0)