-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename methods for better clarity, move utility methods in Kotlin
- Loading branch information
Showing
5 changed files
with
98 additions
and
89 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
80 changes: 0 additions & 80 deletions
80
grobid-core/src/main/java/org/grobid/core/utilities/LabelUtils.java
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
grobid-core/src/main/kotlin/org/grobid/core/utilities/LabelUtils.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.grobid.core.utilities | ||
|
||
import org.apache.commons.lang3.StringUtils | ||
import org.grobid.core.engines.label.TaggingLabels | ||
|
||
object LabelUtils { | ||
/** | ||
* Post-process text labeled by the fulltext model on chunks that are known to be text (no table, or figure) | ||
* It converts table and figure labels to paragraph labels. | ||
*/ | ||
@JvmStatic | ||
fun postProcessFullTextLabeledText(fulltextLabeledText: String): String { | ||
val result = StringBuilder() | ||
|
||
val lines = fulltextLabeledText | ||
.split("\n".toRegex()) | ||
.dropLastWhile { it.isEmpty() } | ||
.toTypedArray() | ||
var previousLabel: String? = null | ||
|
||
for (i in lines.indices) { | ||
val line = lines[i] | ||
if (StringUtils.isBlank(line)) continue | ||
|
||
val pieces = line.split("\t".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() | ||
val label = pieces[pieces.size - 1] | ||
if (label == "I-" + TaggingLabels.FIGURE.label || label == "I-" + TaggingLabels.TABLE.label) { | ||
if (previousLabel == null || !previousLabel.endsWith(TaggingLabels.PARAGRAPH.label)) { | ||
pieces[pieces.size - 1] = "I-" + TaggingLabels.PARAGRAPH.label | ||
} else { | ||
pieces[pieces.size - 1] = TaggingLabels.PARAGRAPH.label | ||
} | ||
} else if (label == TaggingLabels.FIGURE.label || label == TaggingLabels.TABLE.label) { | ||
pieces[pieces.size - 1] = TaggingLabels.PARAGRAPH.label | ||
} | ||
result.append(pieces.joinToString("\t")) | ||
previousLabel = label | ||
result.append("\n") | ||
} | ||
|
||
return result.toString() | ||
} | ||
|
||
/** | ||
* This method correct the fulltext sequence when the model has predicted several unlikely | ||
* start sequences of table or figures. | ||
* For example: I-<figure> followed by another I-<figure> (or table) </figure></figure> | ||
**/ | ||
@JvmStatic | ||
fun postProcessFulltextFixInvalidTableOrFigure(fulltextLabeledText: String): String { | ||
val result = StringBuilder() | ||
|
||
val lines = fulltextLabeledText | ||
.split("\n".toRegex()) | ||
.dropLastWhile { it.isEmpty() } | ||
.toTypedArray() | ||
|
||
var previousLabel: String? = null | ||
for (i in lines.indices) { | ||
val line = lines[i] | ||
if (StringUtils.isBlank(line)) continue | ||
|
||
val pieces = line | ||
.split("\t".toRegex()) | ||
.dropLastWhile { it.isEmpty() } | ||
.toTypedArray() | ||
|
||
val label = pieces[pieces.size - 1] | ||
if (label == "I-" + TaggingLabels.FIGURE.label) { | ||
if (StringUtils.equals(previousLabel, "I-" + TaggingLabels.FIGURE.label)) { | ||
pieces[pieces.size - 1] = TaggingLabels.FIGURE.label | ||
} | ||
} else if (label == "I-" + TaggingLabels.TABLE.label) { | ||
if (StringUtils.equals(previousLabel, "I-" + TaggingLabels.TABLE.label)) { | ||
pieces[pieces.size - 1] = TaggingLabels.TABLE.label | ||
} | ||
} | ||
|
||
result.append(pieces.joinToString("\t")) | ||
previousLabel = label | ||
result.append("\n") | ||
} | ||
|
||
return result.toString() | ||
} | ||
|
||
} |
This file contains 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