farasapy

repository·master·Indexed 40 days ago

https://github.com/magedsaeed/farasapy

A Python API wrapper for the Farasa Arabic NLP toolkit, version 0.1.1. It provides access to segmentation, stemming, NER, POS tagging, diacritization, and lemmatization by automating calls to Java-based Farasa binaries. Supports standalone and interactive execution modes, as well as configurable JSON caching for repeated operations.

Tokens
1.3K
Snippets
5
Records
5
Agent score
0%

What's inside farasapy

  1. How Standalone and Interactive modes work

    master

    Farasapy supports two execution modes for its underlying Java binaries. Choosing the right mode depends on your input size and frequency of use.

    Standalone Mode (Default)

    In this mode, the object calls the binary for every task execution. It uses temporary files for input and output, which are garbage collected after the task ends.

    • Best for: Large input texts that you only need to process once.
    • Drawback: Some binaries (like the diacritizer) may have a slow startup time.

    Interactive Mode

    In this mode, the binary is run once upon instantiation. The library then feeds text to the running binary and captures the output.

    • Best for: Small input texts that you need to process multiple times (improves speed by avoiding repeated binary startup).
    • Usage: Pass interactive=True to the class constructor.
    • Warning: Avoid passing very large lines in interactive mode as it may cause unexpected behavior. It is good practice to call .terminate() on the object when finished to avoid unexpected behavior.
    # Example of Interactive Mode
    segmenter = FarasaSegmenter(interactive=True)
    segmented = segmenter.segment(sample)
    segmenter.terminate()  # Recommended practice
  2. Install farasapy via pip

    master

    Install the farasapy library using the standard Python package manager.

    Prerequisites:

    • Java: You must have Java installed on your system and it must be in your PATH.
    • Java Version: It is recommended to use Java 1.7 or higher.

    Note: The library is a wrapper for Java binaries, so some tasks may be computationally heavy or slow.

    pip install farasapy
  3. Configure Caching in Farasapy

    master

    Farasapy includes a caching mechanism to speed up repeated operations by storing results in JSON format. Caching is enabled by default.

    Cache Configuration Options

    When instantiating a class, you can control the cache behavior:

    • cache=True: Enables caching (default).
    • cache=False: Disables caching.
    • cache_dir="/path/to/dir": Sets a custom directory for the cache files.

    Cache Locations

    • Linux/macOS: ~/.cache/farasapy/
    • Windows: %LOCALAPPDATA%/farasapy/

    Managing Cache

    You can clear all cached results for a specific task instance using the .clear_cache() method.

    # Enable caching (default)
    stemmer = FarasaStemmer()
    
    # Disable caching
    stemmer = FarasaStemmer(cache=False)
    
    # Custom cache directory
    stemmer = FarasaStemmer(cache=True, cache_dir="/path/to/my/cache")
    
    # Clear cached results
    stemmer.clear_cache()
  4. Use FarasaStemmer to stem Arabic text

    master

    The FarasaStemmer class allows you to perform stemming on Arabic text. On its first use, the library will automatically download and extract the necessary Farasa toolkit binaries.

    from farasa.stemmer import FarasaStemmer
    
    stemmer = FarasaStemmer()
    sample = ''' 
    يُشار إلى أن اللغة العربية يتحدثها أكثر من 422 مليون نسمة ويتوزع متحدثوها
    في المنطقة المعروفة باسم الوطن العربي بالإضافة إلى العديد من المناطق ال
    أخرى المجاورة مثل الأهواز وتركيا وتشاد والسنغال وإريتريا وغيرها.وهي اللغ
    ة الرابعة من لغات منظمة الأمم المتحدة الرسمية الست. 
    '''
    
    stemmed_text = stemmer.stem(sample)                                     
    print(stemmed_text)
  5. Import Farasapy NLP classes

    master

    Farasapy provides separate classes for different Arabic NLP tasks. Import the specific class you need from its respective module:

    • FarasaPOSTagger for Part Of Speech tagging
    • FarasaNamedEntityRecognizer for Named Entity Recognition (NER)
    • FarasaDiacritizer for Diacritization
    • FarasaSegmenter for Segmentation
    • FarasaStemmer for Stemming
    • FarasaLemmatizer for Lemmatization
    from farasa.pos import FarasaPOSTagger 
    from farasa.ner import FarasaNamedEntityRecognizer 
    from farasa.diacratizer import FarasaDiacritizer 
    from farasa.segmenter import FarasaSegmenter 
    from farasa.stemmer import FarasaStemmer
    from farasa.lemmatizer import FarasaLemmatizer