Преглед на файлове

Add MARC field option for Korean names.

scossu преди 1 година
родител
ревизия
2e21f8e84b
променени са 5 файла, в които са добавени 38 реда и са изтрити 13 реда
  1. 20 9
      scriptshifter/hooks/korean/romanizer.py
  2. 2 1
      scriptshifter/rest_api.py
  3. 1 1
      scriptshifter/tables/data/korean_names.yml
  4. 11 1
      scriptshifter/templates/index.html
  5. 4 1
      scriptshifter/trans.py

+ 20 - 9
scriptshifter/hooks/korean/romanizer.py

@@ -57,16 +57,16 @@ def s2r_names_post_config(ctx):
     One or more names can be transcribed. A comma or middle dot (U+00B7) is
     to be used as separator for multiple names.
     """
-    ctx.dest, ctx.warnings = _romanize_names(ctx.src)
+    ctx.dest, ctx.warnings = _romanize_names(ctx.src, ctx.options)
 
     return BREAK
 
 
-def _romanize_nonames(src, capitalize="first", hancha=True):
+def _romanize_nonames(src, options):
     """ Main Romanization function for non-name strings. """
 
     # FKR038: Convert Chinese characters to Hangul
-    if hancha:
+    if options.get("hancha", True):
         kor = _hancha2hangul(_marc8_hancha(src))
     else:
         kor = src
@@ -92,10 +92,10 @@ def _romanize_nonames(src, capitalize="first", hancha=True):
 
     logger.debug(f"Before capitalization: {rom}")
     # FKR042: Capitalize all first letters
-    if capitalize == "all":
+    if options["capitalize"] == "all":
         rom = _capitalize(rom)
     # FKR043: Capitalize the first letter
-    elif capitalize == "first":
+    elif options["capitalize"] == "first":
         rom = rom[0].upper() + rom[1:]
 
     # FKR044: Ambiguities
@@ -115,7 +115,7 @@ def _romanize_nonames(src, capitalize="first", hancha=True):
     return rom, warnings
 
 
-def _romanize_names(src):
+def _romanize_names(src, options):
     """
     Main Romanization function for names.
 
@@ -135,14 +135,15 @@ def _romanize_names(src):
     kor_ls = src.split(",") if "," in src else src.split("·")
 
     for kor in kor_ls:
-        rom, _warnings = _romanize_name(kor.strip())
+        rom, _warnings = _romanize_name(kor.strip(), options)
         rom_ls.append(rom)
+
         warnings.extend(_warnings)
 
     return ", ".join(rom_ls), warnings
 
 
-def _romanize_name(src):
+def _romanize_name(src, options):
     warnings = []
 
     # FKR001: Conversion, Family names in Chinese (dealing with 金 and 李)
@@ -184,7 +185,17 @@ def _romanize_name(src):
 
             lname_rom = " ".join(lname_rom_ls)
 
-            rom = f"{lname_rom} {fname_rom}"
+            # Add comma after the last name for certain MARC fields.
+            marc_field_str = options.get("marc_field", "0")
+            try:
+                marc_field = int(marc_field_str)
+            except TypeError:
+                raise ValueError(
+                        f"{marc_field_str} is not a valid MARC field code.")
+            if marc_field in (100, 600, 700, 800):
+                rom = f"{lname_rom}, {fname_rom}"
+            else:
+                rom = f"{lname_rom} {fname_rom}"
 
             if False:
                 # TODO add option for authoritative name.

+ 2 - 1
scriptshifter/rest_api.py

@@ -2,6 +2,7 @@ import logging
 
 from base64 import b64encode
 from copy import deepcopy
+from json import loads
 from os import environ, urandom
 
 from flask import Flask, jsonify, render_template, request
@@ -81,7 +82,7 @@ def transliterate_req():
 
     if not len(in_txt):
         return ("No input text provided! ", 400)
-    options = request.form.get("options", {})
+    options = loads(request.form.get("options", {}))
     logger.debug(f"Extra options: {options}")
 
     try:

+ 1 - 1
scriptshifter/tables/data/korean_names.yml

@@ -5,7 +5,7 @@ general:
 options:
   - id: marc_field
     label: MARC field
-    description: enter 0 if not applicable.
+    description: Romanize according to a specific MARC field format. Enter 0 if not applicable.
     type: int
     default: 0
 

+ 11 - 1
scriptshifter/templates/index.html

@@ -27,6 +27,12 @@
             display: none;
         }
 
+        p.input_descr {
+            font-size: 80%;
+            font-style: italic;
+            margin-bottom: .5rem;
+        }
+
     </style>
 
 
@@ -107,7 +113,11 @@
                         input.classList.add("option_i");
                         input.value = opt.default;
 
-                        fset.append(label, input);
+                        let descr = document.createElement("p");
+                        descr.setAttribute("class", "input_descr");
+                        descr.append(opt.description);
+
+                        fset.append(label, descr, input);
                         document.getElementById("options").append(fset);
                     });
                 });

+ 4 - 1
scriptshifter/trans.py

@@ -52,6 +52,8 @@ def transliterate(src, lang, t_dir="s2r", options={}, capitalize=False):
             `"first"` (only first letter), or `"all"` (first letter of each
             word).
 
+        options: extra script-dependent options. Defaults to the empty map.
+
     Keyword args:
         r2s (bool): If False (the default), the source is considered to be a
         non-latin script in the language and script specified, and the output
@@ -87,7 +89,8 @@ def transliterate(src, lang, t_dir="s2r", options={}, capitalize=False):
     langsec_hooks = langsec.get("hooks", {})
 
     src = src.strip()
-    ctx = Context(src, general, langsec, {"capitalize": capitalize})
+    options["capitalize"] = capitalize
+    ctx = Context(src, general, langsec, options)
 
     # This hook may take over the whole transliteration process or delegate it
     # to some external process, and return the output string directly.