Class AioTextProcessorPrivate Readonly Interface

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.

import { AioTextProcessor } from './TextProcessor.class';

async function main() {
const processor = new AioTextProcessor();

// Summarize a file
await processor.summarized('path/to/your/file.txt', { maxLength: 100, showStatistic: true });
console.log('Summarized:', processor.content);

// Translate a file
await processor.translate('path/to/your/file.txt', 'en-US');
console.log('Translated:', processor.content);

// Split a file into chunks
await processor.split('path/to/your/file.txt', 1000);
console.log('Split:', processor.content);

// Merge multiple files
await processor.merge('path/to/file1.txt', 'path/to/file2.txt');
console.log('Merged:', processor.content);

// Compare two files
await processor.compare('path/to/file1.txt', 'path/to/file2.txt');
console.log('Compared:', processor.content);

// Purify a file (remove extra whitespace)
await processor.purify('path/to/your/file.txt');
console.log('Purified:', processor.content);

// Analyze a file
await processor.analyze('path/to/your/file.txt');
console.log('Analyzed:', processor.content);

// Find matches in a file
await processor.findMatch('path/to/your/file.txt', { keyword: 'example', matchCase: false });
console.log('Matches:', processor.content);

// Replace text in a file
const replaceOptions = [
{ "{name}": "John Doe" },
{ "{date}": "2024-01-01" },
];
await processor.replace('path/to/your/file.txt', replaceOptions);
console.log('Replaced:', processor.content);
}

main().catch(console.error);

AioTextProcessor

InsertOptions - Defines the structure for insert options. MatchOptions - Defines the structure for match options. AnalyzeResult - Defines the structure for analysis results.

Throws an error if there is an issue with file reading, API requests, or invalid input.

Hierarchy (View Summary)

Constructors

  • 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.

    Returns AioTextProcessor

Properties

_auth: undefined | CloudRunAuthClass
_generativeModel: any
_isAuthReady: boolean = false
_options: any
_output: string = ""
_root: string
_translate: any
ext: string = ''
filename: string = ''
filepath: undefined | string = ''
outputPath: string = ''
DEFAULT_TEXT_MODEL: string = "gemini-1.5-pro"

The default text model used for generative AI tasks.

Accessors

  • get content(): any
  • Returns any

  • set content(value: any): void
  • Parameters

    • value: any

    Returns void

  • get options(): string
  • Returns string

  • set options(value: string): void
  • TODO we'll use it later

    Parameters

    • value: string

    Returns void

Methods

  • Extracts the file extension from a given filepath and returns it in lowercase.

    Parameters

    • filepath: string

      The path of the file.

    Returns string

    The file extension in lowercase.


    _getExtension('/path/to/my/file.txt') // returns '.txt'
  • Returns the filename without the extension from a given filepath.

    Parameters

    • filepath: string

      The filepath to extract the filename from.

    Returns string

    The filename without the extension.


    _getFilename('/path/to/my/file.txt') // returns 'file'
  • Analyzes 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.

    Parameters

    • filePath: string

      The path to the file to analyze.

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the analysis results stored in the content property. The content property will be an object of type AnalyzeResult.

    Throws an error if there is an issue reading the file or performing the analysis.


    const processor = new AioTextProcessor();
    await processor.analyze('path/to/your/file.txt');
    console.log(processor.content);
    // Output: { characterCount: 1234, wordCount: 250, lineCount: 20, uniqueWords: 180 }
  • 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.

    Parameters

    • ...filePaths: string[]

      The paths to the two files to compare.

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the 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.

    Parameters

    • filePath: string

      The path to the file to search.

    • matchOptions: MatchOptions

      Options for the search (keyword, case sensitivity).

    Returns Promise<AioTextProcessor>

    An array of matching lines.


    const processor = new AioTextProcessor();
    await processor.findMatch('path/to/your/file.txt', {keyword: 'lies'});
    console.log(processor.content); // Output: ['line 1', 'line 2', 'line 3']
  • Returns Promise<void>

  • Private

    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.

    Returns Promise<void>

  • Checks if a directory exists. If it doesn't, it creates the directory and any necessary parent directories.

    Parameters

    • dir: string

      The path of the directory to check or create.

    Returns Promise<void>

    • A promise that resolves when the directory exists or has been created.
    • Throws an error if there's a problem creating the directory.

    const fileOp = new AioFileOperation();
    await fileOp.isDirectoryExists('/path/to/new/directory');
    // If '/path/to/new/directory' doesn't exist, it will be created.
  • 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.

    Parameters

    • ...filePaths: string[]

      The paths to the files to merge.

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the merged content.

    Throws an error if there is an issue reading any of the files.

    const processor = new AioTextProcessor();
    await processor.merge('path/to/file1.txt', 'path/to/file2.txt');
    console.log(processor.content); // Output the merged content
  • Opens a file at the specified path and returns a file handle.

    Parameters

    • filePath: string

      The path of the file to open.

    Returns Promise<FileHandle>

    • A promise that resolves to a FileHandle object for the opened file.
    • Throws an error if the file cannot be opened.

    const fileOp = new AioFileOperation();
    const fileHandle = await fileOp.open('/path/to/file.txt');
  • 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.

    Parameters

    • filePath: string

      The path to the file to purify.

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the purified content.

    Throws an error if there is an issue reading the file or purifying the content.


    await processor.purify('path/to/your/file.txt');
  • Reads the content of a file, handling different file types.

    Parameters

    • filePath: string

      The path to the file.

    Returns Promise<string>

    The text content of the file.

  • Removes a file if it exists.

    Parameters

    • filePath: string

      The path to the file to be removed.

    Returns Promise<void>

    • A promise that resolves when the file has been removed. *

    const fileOp = new AioFileOperation();
    await fileOp.remove('/path/to/large/file.txt');
  • 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.

    Parameters

    • filePath: string

      The path to the file to modify.

    • replaceOptions: { [key: string]: string }[]

      An array of objects, where each object represents a tag-value pair for replacement.

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the modified content.

    Throws an error if replaceOptions is not an array or if there is an issue reading or modifying the file.


    const processor = new AioTextProcessor();
    const replaceOptions = [
    { "{name}": "John Doe" },
    { "{date}": "2024-01-01" },
    ];
    await processor.replace('path/to/your/file.txt', replaceOptions);
    console.log(processor.content); // Output the modified text with replacements
  • 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.

    Parameters

    • filePath: string

      The path to the file to be scanned.

    Returns Promise<string[]>

    • A promise that resolves to an array of strings, where each string is a line from the file.

    const fileOp = new AioFileOperation();
    const lines = await fileOp.scan('/path/to/large/file.txt');
    console.log(lines); // Output: ['Line 1', 'Line 2', 'Line 3', ...]
  • 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.

    Parameters

    • directory: string

      The path to the directory to search.

    • ext: undefined | string | string[]

      A single file extension or an array of file extensions to filter by. If undefined, no extension filtering is applied.

    • OptionalshowDir: boolean = false

      If true, directories will be included in the results. Defaults to false.

    Returns Promise<undefined | Dirent[]>

    • A promise that resolves to an array of Dirent objects representing the found files.
    • Throws an error if there is a problem accessing the directory, except for 'ENOENT' errors.

    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.

    Parameters

    • filePath: string

      The path to the file to split.

    • chunkSize: number

      The maximum number of characters per chunk.

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the split content.

    Throws an error if there is an issue reading the file or splitting the content.


    const processor = new AioTextProcessor();
    await processor.split('path/to/your/file.txt', 1000);
    console.log(processor.content); // Output: ['Chunk 1...', 'Chunk 2...', ...]
  • Summarizes the content of a text file using the Gemini AI model.

    Parameters

    • filePath: string

      The path to the file to summarize.

    • Optionaloptions: { maxLength?: number; showStatistic?: boolean }

      Optional parameters for summarization.

      • OptionalmaxLength?: number

        The maximum length of the summary (not currently used).

      • OptionalshowStatistic?: boolean

        Whether to show statistics (not currently used).

    Returns Promise<undefined | AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the summarized content.

    Throws an error if the Vertex AI client is not initialized or if there is an issue with the API request.

     const processor = new AioTextProcessor();
    await processor.summarized('path/to/your/file.txt', { maxLength: 100, showStatistic: true });
    console.log(processor.content); // Output the summarized text
  • Translates the content of a text file to a specified target language using the Google Translate API.

    Parameters

    • filePath: string

      The path to the file to translate.

    • OptionaltargetLanguage: string = 'id-ID'

      The target language code in 'xx-XX' format (e.g., 'en-US', 'es-ES'). Defaults to 'id-ID' (Indonesian).

    Returns Promise<AioTextProcessor>

    • A promise that resolves to the AioTextProcessor instance with the translated content.

    Throws an error if the target language code is invalid, the Google Translate client is not initialized, or if there is an issue with the API request.

    const processor = new AioTextProcessor();
    await processor.translate('path/to/your/file.txt', 'en-US'); // Translate to English (US)
    console.log(processor.content); // Output the translated text
  • Writes an array of strings to a file, overwriting any existing content.

    Parameters

    • outputFilePath: string

      The path to the file to be written.

    • content: string[]

      The array of strings to be written to the file. *

    Returns Promise<void>


    const fileOp = new AioFileOperation();
    const dataText = ['Line 1', 'Line 2', 'Line 3', ...]
    const lines = await fileOp.write('/path/to/large/file.txt', dataText);