DEV Community

IntelliTools
IntelliTools

Posted on

Analyze File Content Without API Calls Using FileAnalyzer

When working with files, especially in environments where API calls are either restricted or not desired, developers often find themselves in a bind. The need to process and understand file content without relying on external services is a real pain point. Enter FileAnalyzer, a CLI tool that allows you to analyze text files directly on your system without the need for API integrations. This article walks you through how to use it effectively, and how to implement similar functionality using Python's standard libraries.

FileAnalyzer is designed for developers who want to process files in a local environment, avoiding the overhead of API calls. It supports multiple analysis modes: sentiment analysis, pattern detection, and frequency counting. This makes it a powerful tool for tasks such as log analysis, data preprocessing, and content auditing.

Let's start by looking at how to use the tool directly, and then we'll dive into the Python implementation that mirrors its core functionality.

Using FileAnalyzer

To use FileAnalyzer, you'll need to have it installed. You can find it at https://intellitools.gumroad.com. Once installed, you can run it from the command line with the following syntax:

fileanalyzer --input path/to/input.txt --output path/to/output.json --mode sentiment
Enter fullscreen mode Exit fullscreen mode

This command will analyze the sentiment of the content in input.txt and save the results to output.json.

Python Implementation: Core Functionality

Now, let's look at how to implement similar functionality in Python. The core components of FileAnalyzer include reading a file, analyzing its content, and writing the results to an output file.

Here's a basic implementation that reads a file and performs sentiment analysis using the TextBlob library:

from textblob import TextBlob
import json

def read_file(file_path):
    with open(file_path, 'r') as file:
        return file.read()

def analyze_sentiment(text):
    analysis = TextBlob(text)
    return {
        'polarity': float(analysis.sentiment.polarity),
        'subjectivity': float(analysis.sentiment.subjectivity),
        'sentiment': 'positive' if analysis.sentiment.polarity > 0 else 'negative'
    }

def write_json(data, output_path):
    with open(output_path, 'w') as file:
        json.dump(data, file, indent=4)

def main():
    input_path = 'path/to/input.txt'
    output_path = 'path/to/output.json'
    text = read_file(input_path)
    analysis = analyze_sentiment(text)
    write_json(analysis, output_path)

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

This script reads a text file, performs sentiment analysis, and writes the results to a JSON file. It's a simple yet effective way to process file content locally.

Pattern Detection and Frequency Analysis

In addition to sentiment analysis, FileAnalyzer also supports pattern detection and frequency counting. Let's look at how to implement pattern detection using regular expressions:

import re

def analyze_pattern(text, pattern):
    matches = re.findall(pattern, text)
    return {
        'pattern': pattern,
        'matches': matches,
        'count': len(matches)
    }

def main():
    input_path = 'path/to/input.txt'
    output_path = 'path/to/output.json'
    text = read_file(input_path)
    pattern = r'\b\w{5,}\b'  # Match words of 5 or more characters
    analysis = analyze_pattern(text, pattern)
    write_json(analysis, output_path)

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

This script detects words of five or more characters in a text file and saves the results to a JSON file. It's a practical example of how pattern detection can be implemented in Python.

Conclusion

By understanding how FileAnalyzer works and implementing similar functionality using Python, you can gain a deeper appreciation for the power of local file processing. Whether you're analyzing logs, preprocessing data, or auditing content, the ability to work without API calls is a valuable skill. If you're looking for a robust, easy-to-use tool that does this for you, check out https://intellitools.gumroad.com.

Top comments (0)