Skip to content

Commit

Permalink
Merge pull request #164 from bettyblocks/fix/replace-special-quotes-i…
Browse files Browse the repository at this point in the history
…n-chunk-DT-4221

feat: replace special quotes in chunk 1.1
  • Loading branch information
thomas9911 authored Sep 12, 2024
2 parents d2cf1dd + a3a192f commit d52e6a4
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
30 changes: 30 additions & 0 deletions __tests__/chunk/1.1/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import chunk from '../../../functions/chunk/1.1/index';

describe('chunk', () => {
it('should split the input into chunks of specified size', async () => {
const input =
'Homeworld is a real-time strategy video game developed by Relic Entertainment and published by Sierra Studios on September 28, 1999, for Microsoft Windows. Set in space, the science fiction game follows the Kushan exiles of the planet Kharak after their home planet is destroyed by the Taiidan Empire in retaliation for developing hyperspace jump technology. ';
const chunkSize = 1;
const expectedChunks = [
'Homeworld is a real-time strategy video game developed by Relic Entertainment and published by Sierra Studios on September 28, 1999, for Microsoft Windows.',
'Set in space, the science fiction game follows the Kushan exiles of the planet Kharak after their home planet is destroyed by the Taiidan Empire in retaliation for developing hyperspace jump technology.',
];

const result = await chunk({ input, chunkSize });

expect(result.result).toEqual(expectedChunks);
});
it('should still split the input into chunks of specified size why there are special quotes', async () => {
const input =
'“Homeworld" is a real-time strategy video game developed by Relic Entertainment and published by Sierra Studios on September 28, 1999, for Microsoft Windows. Set in space, the science fiction game follows the Kushan exiles of the planet Kharak after their home planet is destroyed by the Taiidan Empire in retaliation for developing hyperspace jump technology. ';
const chunkSize = 1;
const expectedChunks = [
'"Homeworld" is a real-time strategy video game developed by Relic Entertainment and published by Sierra Studios on September 28, 1999, for Microsoft Windows.',
'Set in space, the science fiction game follows the Kushan exiles of the planet Kharak after their home planet is destroyed by the Taiidan Empire in retaliation for developing hyperspace jump technology.',
];

const result = await chunk({ input, chunkSize });

expect(result.result).toEqual(expectedChunks);
});
});
2 changes: 1 addition & 1 deletion blocks/ai-search.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"dependencies": ["sentence-splitter"],
"functions": ["collectionSearch 1.0", "aiQueryGenerator 1.0", "chunk 1.0"],
"functions": ["collectionSearch 1.0", "aiQueryGenerator 1.0", "chunk 1.1"],
"includes": []
}
46 changes: 46 additions & 0 deletions functions/chunk/1.1/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"description": "Chop a text into smaller pieces.",
"label": "Chunk",
"category": "AI Search",
"icon": {
"name": "AlignSpaceBetweenIcon",
"color": "Pink"
},
"options": [
{
"meta": {
"type": "Text",
"validations": {
"required": true
}
},
"name": "input",
"label": "Input Text",
"info": "The text to chunk"
},
{
"meta": {
"type": "Number",
"default": 5,
"validations": {
"required": true
}
},
"name": "chunkSize",
"label": "Sentences in a chunk",
"info": "The amount of sentences in a chunk"
},
{
"name": "result",
"label": "Result",
"meta": {
"type": "Output",
"output": {
"type": "Array",
"dataType": "STRING"
}
}
}
],
"yields": "NONE"
}
24 changes: 24 additions & 0 deletions functions/chunk/1.1/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { split } from 'sentence-splitter';

const chunk = async ({ input, chunkSize }) => {
const updatedInput = input
.replace(/(|)/g, '"')
.replace(/(|)/g, "'")
.replace(//g, '.');

const sentences = split(updatedInput)
.filter((c) => c.raw.trim() !== '')
.map((c) => c.raw);

const chunks = [];
for (let i = 0; i < sentences.length; i += chunkSize) {
chunks.push(sentences.slice(i, i + chunkSize));
}
const result = chunks.map((c) => c.join(' '));

return {
result,
};
};

export default chunk;

0 comments on commit d52e6a4

Please sign in to comment.