-
Notifications
You must be signed in to change notification settings - Fork 6
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
develop MergeListTag filter #28
Merged
eiennohito
merged 6 commits into
WorksApplications:main
from
asahi-research:merge-list-tags
Nov 16, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4f0dac4
implemented MergeListTag filter
fa6380f
add test pattern for multiple list tags
71a78ee
simplify MergeListTag logics
2dc6598
follow test codes for updating MergeListTag
f1bb6c2
apply scalafmt
1c1004a
simplify imprementations
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
59 changes: 59 additions & 0 deletions
59
lib/src/main/scala/com/worksap/nlp/uzushio/lib/filters/MergeListTag.scala
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,59 @@ | ||
package com.worksap.nlp.uzushio.lib.filters | ||
|
||
import com.worksap.nlp.uzushio.lib.filters.base.DocFilter | ||
import com.worksap.nlp.uzushio.lib.cleaning.Document | ||
|
||
class MergeListTag extends DocFilter { | ||
final val acceptedTags = Seq("li", "option") | ||
final val mdListSymbol = "- " | ||
|
||
def containsAcceptedTag(cssSelectorStrs: Seq[String]): Boolean = { | ||
extractDescendantTag(cssSelectorStrs, acceptedTags) match { | ||
case Some(_) => true | ||
case None => false | ||
} | ||
} | ||
|
||
def extractDescendantTag( | ||
cssSelectorStrs: Seq[String], | ||
tagNames: Seq[String] | ||
): Option[String] = { | ||
val iter = cssSelectorStrs.reverse.iterator | ||
var i = 0 | ||
|
||
while (iter.hasNext) { | ||
val tagWithCSS = iter.next() | ||
val tagWithAttrs = tagWithCSS.split("[#\\.]") | ||
i += 1 | ||
if (acceptedTags.contains(tagWithAttrs.head)) { | ||
return Option(tagWithCSS) | ||
} | ||
} | ||
return None | ||
} | ||
|
||
override def checkDocument(doc: Document): Document = { | ||
var paragraphs = doc.aliveParagraphs.to[Seq] | ||
|
||
(0 until paragraphs.length - 1).foreach { i => | ||
val paragraph = paragraphs(i) | ||
val nextParagraph = paragraphs(i + 1) | ||
val isAccteptedTags = containsAcceptedTag(paragraph.cssSelectors) && containsAcceptedTag( | ||
nextParagraph.cssSelectors | ||
) | ||
|
||
if (isAccteptedTags && paragraph.path == nextParagraph.path) { | ||
val mergedParagraph = nextParagraph.copy( | ||
text = List(paragraph.text, nextParagraph.text) | ||
.map(s => if (s.startsWith(mdListSymbol)) s else mdListSymbol + s).mkString("\n"), | ||
exactFreq = math.min(paragraph.exactFreq, nextParagraph.exactFreq), | ||
nearFreq = math.min(paragraph.nearFreq, nextParagraph.nearFreq) | ||
) | ||
paragraphs = paragraphs.updated(i, paragraph.copy(remove = paragraph)) | ||
paragraphs = paragraphs.updated(i + 1, mergedParagraph) | ||
} | ||
} | ||
|
||
doc.copy(paragraphs = paragraphs) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
lib/src/test/scala/com/worksap/nlp/uzushio/lib/cleaning/ParagraphSpec.scala
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,12 @@ | ||
package com.worksap.nlp.uzushio.lib.cleaning | ||
|
||
import org.scalatest.freespec.AnyFreeSpec | ||
|
||
class ParagraphSpec extends AnyFreeSpec { | ||
"Paragraph" - { | ||
"return css selector strings" in { | ||
val par = Paragraph("body>p.text", "hello") | ||
assert(par.cssSelectors == Seq("body", "p.text")) | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
lib/src/test/scala/com/worksap/nlp/uzushio/lib/filters/MergeListTagSpec.scala
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,35 @@ | ||
package com.worksap.nlp.uzushio.lib.filters | ||
|
||
import com.worksap.nlp.uzushio.lib.cleaning.Document | ||
import org.scalatest.freespec.AnyFreeSpec | ||
|
||
class MergeListTagSpec extends AnyFreeSpec { | ||
"MergeListTag" - { | ||
val filter = new MergeListTag() | ||
"do no operation for empty document" in { | ||
val doc = testDoc("") | ||
assert("" == filter.checkDocument(doc).aliveParagraphs.map(_.text).mkString("")) | ||
} | ||
|
||
"do no operation for no list document" in { | ||
val texts = Seq("test 1", "test 2", "test 3") | ||
val doc = Document(testParagraphs(texts)) | ||
assert(texts.mkString("") == filter.checkDocument(doc).aliveParagraphs.map(_.text).mkString("")) | ||
} | ||
|
||
"merge list tag texts and put 'remove' sign on rests for document including list tags" in { | ||
val texts = Seq("test 1", "li test 1", "li test 2", "li test 3", "li test 4", "li test 5", "li test 6", "option test 1", "option test 2", "test 2") | ||
val nearFreqs = Seq(1, 2, 3, 5, 4, 1, 1, 1, 2, 1) | ||
val exactFreqs = Seq(1, 2, 3, 5, 4, 1, 1, 1, 2, 1) | ||
val paths = Seq("body>p.text", "body>ul>li.text", "body>ul>li.text", "body>ul>li.text2", "body>ul>li.text2", "body>ul>li.text2", "body>ul>li.text3", "body>datalist>option.text", "body>datalist>option.text", "body>p.text") | ||
val paragraphs = testParagraphs(texts, nearFreqs, exactFreqs, paths) | ||
val doc = Document(paragraphs) | ||
|
||
assert(Seq("test 1", "- li test 1\n- li test 2", "- li test 3\n- li test 4\n- li test 5", "li test 6", "- option test 1\n- option test 2", "test 2") == filter.checkDocument(doc).aliveParagraphs.map(_.text).toSeq) | ||
assert(2 == filter.checkDocument(doc).aliveParagraphs.map(_.nearFreq).drop(1).toList.head) | ||
assert(2 == filter.checkDocument(doc).aliveParagraphs.map(_.exactFreq).drop(1).toList.head) | ||
assert(Seq(false, true, false, true, true, false, false, true, false, false) == filter.checkDocument(doc).paragraphs.map(_.remove != null)) | ||
assert(Seq("body>p.text", "body>ul>li.text", "body>ul>li.text2", "body>ul>li.text3", "body>datalist>option.text", "body>p.text") == filter.checkDocument(doc).aliveParagraphs.map(_.path).toSeq) | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This line will have O(n^2) complexity because strings are immutable in Java. Should not still cause much problems I think.
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.
If I can afford to change this line after completing various tasks, I refactor it.
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.
I think we are OK with leaving it as is because