-
Notifications
You must be signed in to change notification settings - Fork 11
Table extraction #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Table extraction #58
Changes from all commits
2ecbd00
09bfffc
98857c4
5791864
4b54f48
c66f094
1e3894d
922aca7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ class BasicSymbolRule( | |
| when (direction) { | ||
| LEFT -> sideTexts.removeAt(1) | ||
| RIGHT -> sideTexts.removeAt(0) | ||
| else -> {} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| } | ||
|
|
||
| val neighbors = (if (notIgnoredNeighbors.isNotEmpty()) sideTexts | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.github.darderion.mundaneassignmentpolice.checker.rule.table | ||
|
|
||
| import com.github.darderion.mundaneassignmentpolice.checker.RuleViolation | ||
| import com.github.darderion.mundaneassignmentpolice.checker.RuleViolationType | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.Rule | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.PDFDocument | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.PDFRegion | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.tables.Table | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.text.Line | ||
|
|
||
| class TableRule ( | ||
| val predicates: MutableList<(Table) -> List<Line>>, | ||
| type: RuleViolationType, | ||
| area: PDFRegion, | ||
| name: String | ||
| ): Rule(area, name, type){ | ||
| override fun process(document: PDFDocument): List<RuleViolation> { | ||
| val rulesViolations: MutableSet<RuleViolation> = mutableSetOf() | ||
|
|
||
| predicates.forEach { predicate -> | ||
| rulesViolations.addAll( | ||
| document.tables.map { | ||
| predicate(it) | ||
| }.filter { it.isNotEmpty() }.map { | ||
| RuleViolation(it, name, type) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| return rulesViolations.toList() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.github.darderion.mundaneassignmentpolice.checker.rule.table | ||
|
|
||
| import com.github.darderion.mundaneassignmentpolice.checker.RuleViolationType | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.PDFRegion | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.tables.Table | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.text.Line | ||
|
|
||
| class TableRuleBuilder { | ||
| private val predicates: MutableList<(Table) -> List<Line>> = mutableListOf() | ||
| private var type: RuleViolationType = RuleViolationType.Error | ||
| private var region: PDFRegion = PDFRegion.EVERYWHERE | ||
| private var name: String = "Rule name" | ||
|
|
||
| fun called(name: String) = this.also { this.name = name } | ||
|
|
||
| fun disallow(predicate: (table: Table) -> List<Line>) = this.also { predicates.add(predicate) } | ||
| fun getRule() = TableRule(predicates, type, region, name) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ class BasicWordRule( | |
| when (direction) { | ||
| Direction.LEFT -> sideWords.removeAt(1) | ||
| Direction.RIGHT -> sideWords.removeAt(0) | ||
| else -> {} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| } | ||
|
|
||
| val filteredSideWords = sideWords | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.github.darderion.mundaneassignmentpolice.pdfdocument.tables | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.text.Coordinate | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.text.Line | ||
|
|
||
| data class Cell( | ||
| val page: Int, | ||
| val cellText: MutableList<String>, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| var cellLines: MutableList<Line>, | ||
| val leftCorner: Coordinate, | ||
| val rightCorner: Coordinate | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package com.github.darderion.mundaneassignmentpolice.pdfdocument.tables | ||
|
|
||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.text.* | ||
| import com.github.darderion.mundaneassignmentpolice.wrapper.PDFBox | ||
| import org.jetbrains.kotlinx.dataframe.AnyFrame | ||
| import org.jetbrains.kotlinx.dataframe.DataFrame | ||
| import org.jetbrains.kotlinx.dataframe.api.* | ||
|
|
||
| class Table(val df: DataFrame<Any?>){ | ||
|
|
||
| val page : Int | ||
| val x1 : Double | ||
| val y1 : Double | ||
| val x2 : Double | ||
| val y2 : Double | ||
| val rowCount : Int | ||
| val colCount : Int | ||
| val cells: MutableList<Cell> = mutableListOf() | ||
| init { | ||
| val indexTableInf = df.select{ cols(0) }.last { it[0] == "table information"}.index() | ||
| val tableInf = df.select{cols(0)}.filter { it.index() >= indexTableInf } | ||
|
|
||
| this.page = tableInf[pageTableIndex][0].toString().toInt() - 1 | ||
| this.x1 = tableInf[x1TableIndex][0].toString().toDouble() | ||
| this.y1 = defaultPageHeight - tableInf[y1TableIndex][0].toString().toDouble() | ||
| this.x2 = tableInf[x2TableIndex][0].toString().toDouble() | ||
| this.y2 = defaultPageHeight - tableInf[y2TableIndex][0].toString().toDouble() | ||
| this.rowCount = tableInf[rowTableIndex][0].toString().toInt() | ||
| this.colCount = tableInf[colTableIndex][0].toString().toInt() | ||
| val tableData = df.filter { it.index() < indexTableInf } | ||
|
|
||
| tableData.forEachColumn { it.forEach { getCell(it.toString()) } } | ||
| } | ||
|
|
||
| private fun getCell(text: String){ | ||
|
|
||
| val coordinates = text.lines().first().split(" ") | ||
| val x1 = coordinates[x1CellIndex].toDouble() | ||
| val y1 = defaultPageHeight - coordinates[y1CellIndex].toDouble() | ||
| val x2 = coordinates[x2CellIndex].toDouble() | ||
| val y2 = defaultPageHeight - coordinates[y2CellIndex].toDouble() | ||
|
|
||
| val cellText = text.lines().filterIndexed{ index, _ -> index > 0 }.toMutableList() | ||
|
|
||
| cells.add(Cell(page, cellText, mutableListOf(), Coordinate(x1,y1), Coordinate(x2,y2))) | ||
| } | ||
|
|
||
| fun getLines(): List<Line>{ | ||
| val lines = mutableListOf<Line>() | ||
| cells.forEach{ lines.addAll(it.cellLines) } | ||
| return lines | ||
| } | ||
|
|
||
| companion object { | ||
| private const val defaultPageHeight = 842.0 | ||
| private const val x1CellIndex = 2 | ||
| private const val y1CellIndex = 5 | ||
| private const val x2CellIndex = 8 | ||
| private const val y2CellIndex = 11 | ||
|
|
||
| private const val pageTableIndex = 2 | ||
| private const val x1TableIndex = 4 | ||
| private const val y1TableIndex = 5 | ||
| private const val x2TableIndex = 6 | ||
| private const val y2TableIndex = 7 | ||
| private const val rowTableIndex = 9 | ||
| private const val colTableIndex = 11 | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,12 @@ package com.github.darderion.mundaneassignmentpolice.pdfdocument.text | |
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.PDFArea | ||
|
|
||
| data class Line(val index: Int, val page: Int, val documentIndex: Int, | ||
| val text: List<Word>, var area: PDFArea? = null | ||
| val text: List<Word>, var area: PDFArea? = null, var endPosition: Coordinate | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| ) { | ||
| val content: String | ||
| get() = text.joinToString("") { it.text } | ||
|
|
||
| val position: Coordinate | ||
| val startPosition: Coordinate | ||
| get() = if (text.isNotEmpty()) text.first().position else Coordinate(0, 0) | ||
|
|
||
| val first: String? | ||
|
|
@@ -17,7 +17,10 @@ data class Line(val index: Int, val page: Int, val documentIndex: Int, | |
| val second: String? | ||
| get() = if (text.count() > 1) text[1].text else null | ||
|
|
||
| override fun toString() = "[$documentIndex -- $index, p.$page, $area, ${position.x}] --> '$content'" | ||
| override fun toString() = "[$documentIndex -- $index, p.$page, $area, ${startPosition.x}] --> '$content'" | ||
|
|
||
| fun drop(numberOfItems: Int) = Line(index, page, documentIndex, text.drop(numberOfItems), area) | ||
| fun drop(numberOfItems: Int) = Line(index, page, documentIndex, text.drop(numberOfItems), area, Coordinate(0,0)) | ||
| companion object{ | ||
| private const val defaultPageWidth = 595.22 | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,9 @@ import com.github.darderion.mundaneassignmentpolice.checker.rule.Rule | |
|
|
||
| val RULE_SET_RU = RuleSet( | ||
| mutableListOf( | ||
| RULE_LITLINK, | ||
| TABLE_RULE, | ||
|
|
||
| /*RULE_LITLINK, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| RULE_SHORT_DASH, | ||
| RULE_MEDIUM_DASH, | ||
| RULE_LONG_DASH, | ||
|
|
@@ -22,9 +24,12 @@ val RULE_SET_RU = RuleSet( | |
| RULE_VARIOUS_ABBREVIATIONS, | ||
| RULE_SECTIONS_ORDER, | ||
| RULE_LOW_QUALITY_CONFERENCES, | ||
|
|
||
| */ | ||
| ) | ||
| + RULES_SPACE_AROUND_BRACKETS | ||
| /*+ RULES_SPACE_AROUND_BRACKETS | ||
| + RULES_SMALL_NUMBERS | ||
| ) | ||
|
|
||
| */ | ||
| ) | ||
| class RuleSet(val rules: List<Rule>) {} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import com.github.darderion.mundaneassignmentpolice.checker.rule.regex.RegexRule | |
| import com.github.darderion.mundaneassignmentpolice.checker.rule.symbol.SymbolRuleBuilder | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.symbol.and | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.symbol.or | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.table.TableRuleBuilder | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.tableofcontent.TableOfContentRuleBuilder | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.url.URLRuleBuilder | ||
| import com.github.darderion.mundaneassignmentpolice.checker.rule.url.then | ||
|
|
@@ -15,6 +16,7 @@ import com.github.darderion.mundaneassignmentpolice.checker.rule.word.WordRuleBu | |
| import com.github.darderion.mundaneassignmentpolice.checker.rule.word.or | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.PDFArea | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.PDFRegion | ||
| import com.github.darderion.mundaneassignmentpolice.pdfdocument.text.Line | ||
| import com.github.darderion.mundaneassignmentpolice.utils.InvalidOperationException | ||
| import com.github.darderion.mundaneassignmentpolice.utils.LowQualityConferencesUtil | ||
| import com.github.darderion.mundaneassignmentpolice.utils.ResourcesUtil | ||
|
|
@@ -416,3 +418,12 @@ val RULE_LOW_QUALITY_CONFERENCES = URLRuleBuilder() | |
| .any { conference -> url.text.contains(conference) } | ||
| }.map { it to it.lines } | ||
| }.getRule() | ||
|
|
||
| val TABLE_RULE = TableRuleBuilder() | ||
| .called("Все клетки") | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| .disallow { table -> | ||
| val lines = mutableListOf<Line>() | ||
| table.cells.forEach { cell -> lines.addAll(cell.cellLines) } | ||
| lines | ||
| } | ||
| .getRule() | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?