This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Added Token/Tokenizer and PostaggedToken/PosTagger Serialization #28
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,4 +95,23 @@ object PostaggedToken { | |
} | ||
} | ||
} | ||
|
||
object serialization extends Format[PostaggedToken, String]{ | ||
def write(postaggedToken: PostaggedToken): String = { | ||
Iterator(Token.serialization.write(postaggedToken),postaggedToken.postag).mkString(" ").replaceAll("\\s+", " ") | ||
} | ||
def read(str: String): PostaggedToken = { | ||
try{ | ||
val token = Token.serialization.read(str) | ||
val info = str.split(" ") | ||
val posTag = info(1) | ||
PostaggedToken.apply(posTag,token.string,token.offset) | ||
} | ||
catch{ | ||
case e: Exception => { | ||
throw new MatchError("Could not match PostaggedToken in serialized form") | ||
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. maybe append the serialized string. I.e. |
||
} | ||
} | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,22 @@ object Token { | |
val splitIndex = string.lastIndexOf('@') | ||
Token(string.take(splitIndex), 0) | ||
} | ||
|
||
object serialization extends Format[Token, String]{ | ||
def write(token: Token): String = token.string+"@"+token.offset | ||
def read(str: String): Token = { | ||
try{ | ||
val info = str.split(" ") | ||
val tokenSplit = info(0).split("@") | ||
val tokenOffset = tokenSplit(1).toInt | ||
val tokenString = tokenSplit(0) | ||
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.
|
||
Token(tokenString,tokenOffset) | ||
} | ||
catch{ | ||
case e: Exception =>{ | ||
throw new MatchError("Could not match Token in serialized form") | ||
} | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -69,6 +69,16 @@ object Tokenizer { | |
case s => throw new MatchError("Could not deserialize: " + s) | ||
}(scala.collection.breakOut) | ||
} | ||
|
||
object serialization extends Format[Seq[Token],String]{ | ||
def write(tokens: Seq[Token]):String = { | ||
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. space before return type |
||
val serializedTokens = for(tok <- tokens) yield Token.serialization.write(tok) | ||
serializedTokens.mkString("\t") | ||
} | ||
def read(str: String): Seq[Token] = { | ||
for (s <- str.split("\t")) yield Token.serialization.read(s) | ||
} | ||
} | ||
} | ||
|
||
abstract class TokenizerMain extends LineProcessor("tokenizer") { | ||
|
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 |
---|---|---|
|
@@ -5,14 +5,52 @@ package tokenize | |
import org.junit._ | ||
import org.junit.Assert._ | ||
import org.junit.runner.RunWith | ||
|
||
import org.specs2.mutable.Specification | ||
import org.specs2.runner.JUnitRunner | ||
import edu.knowitall.tool.postag.PostaggedToken | ||
import edu.knowitall.tool.postag.Postagger | ||
|
||
@RunWith(classOf[JUnitRunner]) | ||
object TokenSpecTest extends Specification { | ||
"tokens serialize and deserialize correctly" in { | ||
val token = Token("asdf", 0) | ||
Token.deserialize(token.serialize) == token | ||
|
||
val t = Token("asdf",0) | ||
val tSerialized = Token.serialization.write(t) | ||
val tDeserialized = Token.serialization.read(tSerialized) | ||
tDeserialized == t | ||
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. Use |
||
|
||
val pt = PostaggedToken("DT","in",3) | ||
PostaggedToken.serialization.read(PostaggedToken.serialization.write(pt)) == pt | ||
} | ||
|
||
"tokenizer serialization and deserialization work correctly" in { | ||
|
||
val token1 = Token("The",0) | ||
val token2 = Token("big",4) | ||
val tokens = Seq(token1,token2) | ||
val tokensSerialization = Tokenizer.serialization.write(tokens) | ||
Tokenizer.serialization.read(tokensSerialization) == tokens | ||
|
||
} | ||
|
||
"posTaggedTokenizer serialization and deserialization work correctly" in { | ||
val posToken1 = PostaggedToken("DT","The",0) | ||
val posToken2 = PostaggedToken("JJ","big",4) | ||
val posTokens = Seq(posToken1,posToken2) | ||
val posTokensSerialization = Postagger.serialization.write(posTokens) | ||
Postagger.serialization.read(posTokensSerialization) == posTokens | ||
} | ||
|
||
"deserializing Tokens from posTagger serialization works" in { | ||
val posToken1 = PostaggedToken("DT","The",0) | ||
val token1 = Token("The",0) | ||
val posToken2 = PostaggedToken("JJ","big",4) | ||
val token2 = Token("big",4) | ||
val posTokens = Seq(posToken1,posToken2) | ||
val tokens = Seq(token1,token2) | ||
val posTokensSerialization = Postagger.serialization.write(posTokens) | ||
Tokenizer.serialization.read(posTokensSerialization) == tokens | ||
} | ||
} |
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.
I would write this as
val postag = str.split(" ") match { case Array(postag) => postag }
.You could add a case here to handle the exception and give an informative error too (i.e.
case _ => throw new MatchError
)