-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `TextClassificationTask` * Add tokenization and tests for new transforms. * Add notebook for data pipeline Co-authored-by: lorenzoh <lorenz.ohly@gmail.com>
- Loading branch information
1 parent
dabd150
commit 2d396e2
Showing
10 changed files
with
590 additions
and
12 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
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
""" | ||
TextEncoding() <: Encoding | ||
Encodes `Paragraph`s by applying various textual transforms. | ||
Encodes | ||
- `Paragraph` -> `Paragraph` | ||
""" | ||
struct Sanitize <: Encoding | ||
tfms | ||
end | ||
|
||
Sanitize() = Sanitize(DEFAULT_SANITIZERS) | ||
|
||
|
||
encodedblock(::Sanitize, block::Paragraph) = block | ||
|
||
function encode(p::Sanitize, context, block::Paragraph, obs) | ||
for tfm in values(p.tfms) | ||
obs = tfm(obs) | ||
end | ||
obs | ||
end | ||
|
||
struct Tokenize <: Encoding | ||
tfms | ||
end | ||
|
||
Tokenize() = Tokenize(DEFAULT_TOKENIZERS) | ||
|
||
function encodedblock(p::Tokenize, block::Paragraph) | ||
return Tokens() | ||
end | ||
|
||
function encode(p::Tokenize, context, block::Paragraph, obs) | ||
for tfm in values(p.tfms) | ||
obs = tfm(obs) | ||
end | ||
obs | ||
end | ||
|
||
function computevocabulary(data) | ||
lookup_table = Dict{String, Int}() | ||
|
||
enc1 = Sanitize() | ||
sanitized_Data = map(i -> encode(enc1, Training(), Paragraph(), getobs(data, i)[1]), 1:numobs(data)) | ||
|
||
enc2 = Tokenize() | ||
tokenized_data = map(i -> encode(enc2, Training(), Paragraph(), getobs(sanitized_Data, i)), 1:numobs(data)) | ||
|
||
vocab = [] | ||
for sample in tokenized_data | ||
for token in sample | ||
lookup_table[token] = get(lookup_table, token, 0) + 1 | ||
end | ||
end | ||
return OrderedDict(lookup_table) | ||
end | ||
|
||
struct EmbedVocabulary <: Encoding | ||
vocab | ||
end | ||
|
||
function EmbedVocabulary(; vocab) | ||
return EmbedVocabulary(vocab) | ||
end | ||
|
||
function setup(::Type{EmbedVocabulary}, data) | ||
vocab = computevocabulary(data) | ||
return EmbedVocabulary(vocab = vocab) | ||
end | ||
|
||
function encodedblock(p::EmbedVocabulary, block::Tokens) | ||
return NumberVector() | ||
end | ||
|
||
function encode(p::EmbedVocabulary, context, block::Tokens, obs) | ||
vocabulary = p.vocab | ||
|
||
return [vocabulary[token] for token in obs] | ||
end | ||
|
||
|
||
# ## Tests | ||
|
||
@testset "TextPreprocessing [Encoding]" begin | ||
sample_input = "Unsanintized text, this has to be sanitized. Then it should be tokenized. Finally it has to be numericalized" | ||
block = Paragraph() | ||
enc1 = Sanitize() | ||
testencoding(enc1, block, sample_input) | ||
|
||
# sample_input_sanitized = "xxbos xxmaj unsanintized text sanitized xxmaj tokenized xxmaj finally numericalized" | ||
sample_input_sanitized = encode(enc1, Training(), block, sample_input) | ||
block = Paragraph() | ||
enc2 = Tokenize() | ||
testencoding(enc2, block, sample_input_sanitized) | ||
|
||
# tokenized_input = ["xxbos", "xxmaj", "unsanintized", "text", "sanitized", "tokenized", "finally", "numericalized"] | ||
tokenized_input = encode(enc2, Training(), block, sample_input_sanitized) | ||
block = Tokens() | ||
vocab = setup(EmbedVocabulary, [[sample_input]]) | ||
enc3 = EmbedVocabulary(vocab = vocab.vocab) | ||
testencoding(enc3, block, tokenized_input) | ||
|
||
|
||
end |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
TextClassificationSingle(blocks[, data]) | ||
Learning task for single-label text classification. Samples are | ||
preprocessed by applying various textual transforms and classified into one of `classes`. | ||
""" | ||
function TextClassificationSingle(blocks::Tuple{<:Paragraph,<:Label}, data) | ||
return SupervisedTask( | ||
blocks, | ||
( | ||
Sanitize(), | ||
Tokenize(), | ||
setup(EmbedVocabulary, data), | ||
# EmbedVocabulary(), | ||
OneHot() | ||
) | ||
) | ||
end | ||
|
||
_tasks["textclfsingle"] = ( | ||
id="textual/textclfsingle", | ||
name="Text classification (single-label)", | ||
constructor=TextClassificationSingle, | ||
blocks=(Paragraph, Label), | ||
category="supervised", | ||
description=""" | ||
Single-label text classification task where every text has a single | ||
class label associated with it. | ||
""", | ||
package=@__MODULE__, | ||
) | ||
|
||
# ## Tests | ||
|
||
@testset "TextClassificationSingle [task]" begin | ||
task = TextClassificationSingle((Paragraph(), Label{String}(["neg", "pos"])), [("A good review", "pos")]) | ||
testencoding(getencodings(task), getblocks(task).sample, ("A good review", "pos")) | ||
FastAI.checktask_core(task, sample = ("A good review", "pos")) | ||
|
||
@testset "`encodeinput`" begin | ||
paragraph = "A good review" | ||
|
||
xtrain = encodeinput(task, Training(), paragraph) | ||
@test eltype(xtrain) == Int64 | ||
end | ||
|
||
@testset "`encodetarget`" begin | ||
category = "pos" | ||
y = encodetarget(task, Training(), category) | ||
@test y ≈ [0, 1] | ||
end | ||
end |
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
Oops, something went wrong.