Private
Readonly
Interface
Constructor for the AioTextProcessor class.
Calls the parent class (AioFileOperation) constructor and initializes the Google Cloud clients (Vertex AI, Translate) by calling the initAuth and initGoogleClients methods.
Private
_authPrivate
_generativePrivate
_isPrivate
_optionsPrivate
_outputProtected
_rootPrivate
_translatePrivate
Static
Readonly
DEFAULT_The default text model used for generative AI tasks.
TODO we'll use it later
Protected
_getProtected
_getAnalyzes the content of a text file to provide statistics such as character count, word count, line count, and unique word count.
This method reads the content of a file and performs various analyses to extract statistical information. It calculates the total number of characters, words, and lines in the file. Additionally, it determines the number of unique words present in the file.
The path to the file to analyze.
content
property.
The content
property will be an object of type AnalyzeResult
.Compares the content of two files and identifies the differences between them.
This method reads the content of two files, compares them line by line, and
identifies the lines that are different. The differences are then stored in
the content
property of the AioTextProcessor
instance.
The paths to the two files to compare.
AioTextProcessor
instance with the comparison results.Throws an error if more than two file paths are provided or if there is an issue reading the files.
const processor = new AioTextProcessor();
await processor.compare('path/to/file1.txt', 'path/to/file2.txt');
console.log(processor.content);
// Example output:
// [
// 'Line 3:',
// '- This is line 3 from file1.',
// '+ This is line 3 from file2.',
// 'Line 5:',
// '- This is line 5 from file1.',
// '+ This is line 5 from file2.'
// ]
Finds matches of a keyword or pattern within the text.
The path to the file to search.
Options for the search (keyword, case sensitivity).
An array of matching lines.
Private
initPrivate
Initializes the Google Cloud clients (Vertex AI, Translate). This method is called internally during the constructor. It waits for the authentication to complete before attempting to initialize the clients. If the authentication fails, it throws an error.
Protected
isChecks if a directory exists. If it doesn't, it creates the directory and any necessary parent directories.
The path of the directory to check or create.
Merges the content of multiple files into a single string.
This method takes an array of file paths and reads the content of each file.
The content of each file is concatenated with a newline character and the
resulting string is stored in the content
property of the
AioTextProcessor
instance.
The paths to the files to merge.
AioTextProcessor
instance with the merged content.Removes extra whitespace (multiple spaces, tabs, newlines) from the content of a text file.
This method reads the content of a file and removes any extra whitespace characters, replacing multiple spaces, tabs, and newlines with a single space. It also trims any leading or trailing whitespace.
The path to the file to purify.
Replaces specified tags in a file with corresponding values.
This method reads the content of a file and replaces all occurrences of specified tags with their corresponding values. The tags and their values are provided as an array of key-value pairs.
The path to the file to modify.
An array of objects, where each object represents a tag-value pair for replacement.
Reads a file line by line and returns an array of strings, where each string is a line from the file.
This method opens a file, reads it line by line, and stores each line as a separate string in an array. It uses an asynchronous iterator to efficiently handle large files without loading the entire content into memory at once.
The path to the file to be scanned.
Searches for files within a specified directory, optionally filtering by file extension.
This method recursively scans a directory and its subdirectories for files. It can filter the results based on a specified file extension or an array of extensions. If no extension is provided, it returns all files. It also has an option to include directories in the results.
The path to the directory to search.
A single file extension or an array of file extensions to filter by. If undefined, no extension filtering is applied.
Optional
showDir: boolean = falseIf true, directories will be included in the results. Defaults to false.
const fileOp = new AioFileOperation();
// Search for all .txt files in the 'mydir' directory
const txtFiles = await fileOp.search('mydir', '.txt');
// Search for all .txt and .md files in the 'mydir' directory
const txtAndMdFiles = await fileOp.search('mydir', ['.txt', '.md']);
// Search for all files and directories in the 'mydir' directory
const allFilesAndDirs = await fileOp.search('mydir', undefined, true);
Splits the content of a text file into chunks of a specified size.
This method reads the content of a file and divides it into smaller
chunks, each containing a maximum number of characters defined by chunkSize
.
The resulting chunks are stored in the content
property of the
AioTextProcessor
instance as an array of strings.
The path to the file to split.
The maximum number of characters per chunk.
AioTextProcessor
instance with the split content.Summarizes the content of a text file using the Gemini AI model.
The path to the file to summarize.
Optional
options: { maxLength?: number; showStatistic?: boolean }Optional parameters for summarization.
Optional
maxLength?: numberThe maximum length of the summary (not currently used).
Optional
showStatistic?: booleanWhether to show statistics (not currently used).
Translates the content of a text file to a specified target language using the Google Translate API.
The path to the file to translate.
Optional
targetLanguage: string = 'id-ID'The target language code in 'xx-XX' format (e.g., 'en-US', 'es-ES'). Defaults to 'id-ID' (Indonesian).
AioTextProcessor The
AioTextProcessor
class provides a suite of methods for processing text files, including reading, summarizing, translating, splitting, merging, comparing, purifying, analyzing, finding matches, and replacing text. It leverages Google Cloud's Vertex AI and Translate APIs for advanced text processing capabilities.Example
AioTextProcessor
See
AioTextProcessor
and sets up Google Cloud clients.InsertOptions - Defines the structure for insert options. MatchOptions - Defines the structure for match options. AnalyzeResult - Defines the structure for analysis results.
Throws
Throws an error if there is an issue with file reading, API requests, or invalid input.