How to use the quantulum3.classes.Entity function in quantulum3

To help you get started, we’ve selected a few quantulum3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nielstron / quantulum3 / quantulum3 / tests.py View on Github external
for item in test['res']:
                try:
                    unit = l.NAMES[item['unit']]
                except KeyError:
                    try:
                        entity = item['entity']
                    except KeyError:
                        print(('Could not find %s, provide "derived" and'
                               ' "entity"' % item['unit']))
                        return
                    if entity == 'unknown':
                        derived = [{
                            'base': l.NAMES[i['base']].entity.name,
                            'power': i['power']
                        } for i in item['dimensions']]
                        entity = c.Entity(name='unknown', dimensions=derived)
                    elif entity in l.ENTITIES:
                        entity = l.ENTITIES[entity]
                    else:
                        print(('Could not find %s, provide "derived" and'
                               ' "entity"' % item['unit']))
                        return
                    unit = c.Unit(
                        name=item['unit'],
                        dimensions=item['dimensions'],
                        entity=entity)
                try:
                    span = next(
                        re.finditer(re.escape(item['surface']),
                                    test['req'])).span()
                except StopIteration:
                    print('Surface mismatch for "%s"' % test['req'])
github nielstron / quantulum3 / quantulum3 / parser.py View on Github external
Infer the underlying entity of a unit (e.g. "volume" for "m^3") based on
    its dimensionality.
    """

    new_derived = [
        {"base": load.units(lang).names[i["base"]].entity.name, "power": i["power"]}
        for i in dimensions
    ]

    final_derived = sorted(new_derived, key=lambda x: x["base"])
    key = load.get_key_from_dimensions(final_derived)

    ent = dis.disambiguate_entity(key, text, lang)
    if ent is None:
        _LOGGER.debug("\tCould not find entity for: %s", key)
        ent = cls.Entity(name="unknown", dimensions=new_derived)

    return ent
github nielstron / quantulum3 / quantulum3 / load.py View on Github external
path = TOPDIR.joinpath("entities.json")
        with path.open(encoding="utf-8") as file:
            general_entities = json.load(file)
        names = [i["name"] for i in general_entities]

        try:
            assert len(set(names)) == len(general_entities)
        except AssertionError:  # pragma: no cover
            raise Exception(
                "Entities with same name: %s" % [i for i in names if names.count(i) > 1]
            )

        self.names = dict(
            (
                k["name"],
                c.Entity(name=k["name"], dimensions=k["dimensions"], uri=k["URI"]),
            )
            for k in general_entities
        )

        # Update with language specific URI
        with TOPDIR.joinpath(language.topdir(lang), "entities.json").open(
            "r", encoding="utf-8"
        ) as file:
            lang_entities = json.load(file)
        for ent in lang_entities:
            general_entities[ent["name"]].uri = ent["URI"]

        # Generate derived units
        derived_ent = defaultdict(set)
        for entity in self.names.values():
            if not entity.dimensions: