Example #1
0
 def read_config(fname):
     rdr = None
     try:
         rdr = LineReader(fname)
         for s in rdr.lines():
             # Ignore comments
             ix = s.find("#")
             if ix >= 0:
                 s = s[0:ix]
             s = s.strip()
             if not s:
                 # Blank line: ignore
                 continue
             if s[0] == "[" and s[-1] == "]":
                 # Handle section name, if found (it is redundant in this case)
                 section = s[1:-1].strip().lower()
                 if section != "meanings":
                     raise ConfigError("Unknown section name '{0}'".format(section))
                 continue
             Meanings.add_entry(s)
     except ConfigError as e:
         # Add file name and line number information to the exception
         # if it's not already there
         if rdr is not None:
             e.set_pos(rdr.fname(), rdr.line())
         raise e
Example #2
0
    def add_entry(s):
        """ Handle additional word meanings in the settings section """
        # Format: stofn ordmynd ordfl fl (default ob) beyging (default -)
        a = s.split()
        if len(a) < 2 or len(a) > 5:
            raise ConfigError("Meaning should have two to five arguments, {0} given".format(len(a)))
        stofn = None
        fl = None
        beyging = None
        if len(a) == 2:
            # Short format: only ordmynd and ordfl
            ordmynd = a[0]
            ordfl = a[1]
        else:
            # Full format: at least three arguments, stofn ordmynd ordfl
            stofn = a[0]
            ordmynd = a[1]
            ordfl = a[2]
            fl = a[3] if len(a) >= 4 else None
            beyging = a[4] if len(a) >= 5 else None

        if len(a) == 2 and "-" in ordmynd:
            # Creating new meanings by prefixing existing ones
            Meanings.add_composite(ordmynd, ordfl)
        else:
            Meanings.add(stofn, ordmynd, ordfl, fl, beyging)
Example #3
0
    def add_entry(s):
        """ Handle additional word meanings in the settings section """
        # Format: stofn ordmynd ordfl fl (default ob) beyging (default -)
        a = s.split()
        if len(a) < 2 or len(a) > 5:
            raise ConfigError(
                "Meaning should have two to five arguments, {0} given".format(len(a))
            )
        stofn = None
        fl = None
        beyging = None
        utg = None
        if 3 <= len(a) <= 4 and re.match(r"\d+$", a[-1]):
            # An id number (utg) is being given in the last argument
            utg = int(a[-1])
            # Cut it off the end and proceed
            a = a[:-1]
        if len(a) <= 3:
            # Short format: ordmynd ordfl [fl]
            ordmynd = a[0]
            ordfl = a[1]
            if len(a) == 3:
                fl = a[2]
        else:
            # Full format: at least four arguments, stofn ordmynd ordfl fl
            if utg is not None:
                raise ConfigError(
                    "An id number (utg) can't be specified with a full meaning"
                )
            stofn = a[0]
            ordmynd = a[1]
            ordfl = a[2]
            fl = a[3]
            beyging = a[4] if len(a) >= 5 else None

        if utg is not None and not "-" in ordmynd:
            raise ConfigError(
                "An id number (utg) should only be specified for a composite word"
            )
        if len(a) <= 3 and "-" in ordmynd:
            # Creating new meanings by prefixing existing ones
            Meanings.add_composite(ordmynd, ordfl, fl, utg=utg)
        else:
            assert utg is None
            Meanings.add(stofn, ordmynd, ordfl, fl, beyging)
Example #4
0
 def add_composite(stofn, ordfl):
     """ Add composite word forms by putting a prefix on existing BIN word forms.
         Called from the config file handler. """
     assert stofn is not None
     assert ordfl is not None
     a = stofn.split("-")
     if len(a) != 2:
         raise ConfigError("Composite word meaning must contain a single hyphen")
     prefix = a[0]
     stem = a[1]
     m = Meanings.forms(stem)
     if m:
         for w in m:
             if w.ordfl == ordfl:
                 t = (prefix + w.stofn, -1, ordfl, w.fl, prefix + w.ordmynd, w.beyging)
                 Meanings.DICT[prefix + w.ordmynd].append(t)
                 Meanings.ROOT[prefix + w.stofn].append(t)
Example #5
0
 def add_composite(stofn, ordfl, fl, *, utg=None):
     """ Add composite word forms by putting a prefix on existing BIN word forms.
         Called from the config file handler. """
     assert stofn is not None
     assert ordfl is not None
     # Handle cases like 'Suður-Ameríku-ríki' correctly
     a = stofn.rsplit("-", maxsplit=1)
     assert len(a) == 2
     stem = a[1]
     if stem[0].isupper():
         # Uppercase stem (such as 'Norður-Makedónía'): keep the hyphen
         prefix = a[0] + "-"
     else:
         prefix = a[0]
     m = Meanings.forms(stem)
     if m:
         last_utg = None
         for w in m:
             if w.ordfl == ordfl and (utg is None or utg == w.utg):
                 # Check for ambiguity between different lemmas
                 if last_utg is None:
                     last_utg = w.utg
                 elif w.utg != last_utg:
                     # We are encountering two different ids (utg):
                     # this probably needs to be disambiguated
                     raise ConfigError(
                         "Ambiguous word stem: {0}/{1}".format(w.stofn, w.ordfl)
                     )
                 # Matches the requested category and also the
                 # id number (utg), if given: create a new entry in the
                 # Meanings dict, having id -1
                 t = (
                     prefix + w.stofn,
                     -1,
                     ordfl,
                     fl or w.fl,
                     prefix + w.ordmynd,
                     w.beyging,
                 )
                 Meanings.DICT[prefix + w.ordmynd].append(t)
                 Meanings.ROOT[prefix + w.stofn].append(t)