-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for PostProcessor Sequence
- Loading branch information
1 parent
0564dd1
commit 2cf18cc
Showing
4 changed files
with
72 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codewithkyrian\Transformers\PostProcessors; | ||
|
||
/** | ||
* A post-processor that applies multiple post-processors in sequence. | ||
*/ | ||
class PostProcessorSequence extends PostProcessor | ||
{ | ||
|
||
/** | ||
* List of post-processors to apply. | ||
*/ | ||
protected array $processors; | ||
|
||
/** | ||
* Creates a new instance of PostProcessorSequence. | ||
* | ||
* @param array $config The configuration array. | ||
* - 'processors' (array): The list of post-processors to apply. | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
parent::__construct($config); | ||
|
||
$this->processors = array_map( | ||
fn ($processorConfig) => PostProcessor::fromConfig($processorConfig), | ||
$config['processors'] | ||
); | ||
} | ||
|
||
/** | ||
* Post-process the given tokens. | ||
* | ||
* @param array $tokens The list of tokens for the first sequence. | ||
* @param string[]|null $tokenPair The input tokens for the second sequence in a pair. | ||
* * @param bool $addSpecialTokens Whether to add the special tokens associated with the corresponding model. | ||
* | ||
* @return PostProcessedOutput An array containing the post-processed tokens and token_type_ids. | ||
*/ | ||
public function postProcess(array $tokens, ?array $tokenPair = null, bool $addSpecialTokens = true): PostProcessedOutput | ||
{ | ||
$tokenTypeIds = null; | ||
|
||
foreach ($this->processors as $processor) { | ||
if ($processor instanceof ByteLevelPostProcessor) { | ||
// Special case where we need to pass the tokens_pair to the post-processor | ||
$output = $processor->postProcess($tokens); | ||
$tokens = $output->tokens; | ||
|
||
if ($tokenPair !== null) { | ||
$pairOutput = $processor->postProcess($tokenPair); | ||
$tokenPair = $pairOutput->tokens; | ||
} | ||
} else { | ||
$output = $processor->postProcess($tokens, $tokenPair, $addSpecialTokens); | ||
$tokens = $output->tokens; | ||
$tokenTypeIds = $output->tokenTypeIds; | ||
} | ||
} | ||
|
||
return new PostProcessedOutput($tokens, $tokenTypeIds); | ||
} | ||
} | ||
|
||
?> |
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