kariminf/aruudy

View on GitHub
exec/aruudy

Summary

Maintainability
Test Coverage
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#  Copyright 2019 Abdelkrime Aries <kariminfo0@gmail.com>
#
#  ---- AUTHORS ----
# 2019    Abdelkrime Aries <kariminfo0@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys, getopt
import os

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from aruudy.poetry import prosody, meter

try:
    UNICODE_EXISTS = bool(type(unicode))
except NameError:
    unicode = lambda s, u="": str(s)

def help():
    hlp = """aruudy [-s | --shatr] <string>:
    Find meter to a verse's part (shatr)
        <text> is the verse's part (shatr)

aruudy [-l, --list] [a|e|t]:
    List all Available arabic meters names
        a: in Arabic, e: in English, t: in transliteration

aruudy [-i, --info] <name>:
    List information about the bahr
        <name> is the name (english or arabic) of the meter (bahr)
    """
    print(hlp)

def shatr(text):
    text = unicode(text, "UTF-8")
    s = prosody.process_shatr(text)
    #original text
    print("original: " + s.orig)
    #Normalized text
    print("normalized: " + s.norm)
    #prosody form
    print("prosody form: " + s.prosody)
    # Farahidi meter for poetry
    print("arabic scansion: " + s.ameter)
    # Western-like metre
    print("western scansion: " + s.emeter)
    if s.bahr:
        print("Meter Arabic name: " + s.bahr.name["arabic"])
        print("Meter English name: " + s.bahr.name["english"])
        print("Meter Transliterated name: " + s.bahr.name["trans"])
    else:
        print("Meter Not Found")

langs = {
    "t": "trans",
    "a": "arabic",
    "e": "english"
}

def lst(lang):
    if len(lang)> 1 or "aet".find(lang) == -1:
        print("aruudy [-l, --list] [a|e|t]")
        exit(1)
    lang = langs[lang]
    ls = meter.get_names(lang)
    print("List of meters:")
    for l in ls:
        print(l)

def info(name):
    name = unicode(name, "UTF-8")
    b = meter.get_bahr(name, False)
    if not b:
        print("Meter not found")
    else:
        print("Arabic name: " + b.name["arabic"])
        print("English name: " + b.name["english"])
        print("Transliterated name: " + b.name["trans"])
        s = b.used_scansion
        print("Arabic meter: " + s["ameter"])
        print("English meter: " + s["emeter"])
        print("Mnemonic: " + s["mnemonic"])
        print("Key: " + b.key)

def main(argv):
    try:
        opts, args = getopt.getopt(argv,"hs:l:i:",["help", "shatr=", "list=", "info="])
    except getopt.GetoptError:
        help()
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-h", "--help"):
            help()
            sys.exit()
        elif opt in ("-s", "--shatr"):
            shatr(arg)
        elif opt in ("-l", "--list"):
            lst(arg)
        elif opt in ("-i", "--info"):
            info(arg)


if __name__ == "__main__":
    main(sys.argv[1:])