Esempio n. 1
0
def main():
    """ This is the function that is run from commandline with `natura` """
    if "--lang" == sys.argv[1]:
        f = Finder(sys.argv[2])
        args = sys.argv[3:]
    else:
        f = Finder()
        args = sys.argv[1:]
    text_string = " ".join(args)
    print(f.findall(text_string))
Esempio n. 2
0
def test_default_exchange():
    from_currency = "USD"
    to_currency = "EUR"
    money = Finder(
        base_currency=to_currency,
        converter=FixerIOExchangeRate("445412d0f6101005e9c56c1bd9ea6b87"))
    import sqlite3
    try:
        mp = money.findall("500{}".format(from_currency))
        assert mp[0].base is not None
        assert mp[0].last_modified_base is not None
    except sqlite3.OperationalError:
        print("skipping sqlite on remote")
Esempio n. 3
0
def test_symbols(locale_name, money_string, currency_type):
    locale = LOCALES[locale_name]
    money = Finder(locale_name, converter=None)
    for ct in locale[currency_type]:
        if ct not in CURRENT_BAD:
            y_sim = get_default(locale[currency_type][ct])
            generic_test(money_string.format(ms=ct), money, [500, y_sim, 2])
Esempio n. 4
0
def test_mix1(locale_name, money_string):
    locale = LOCALES[locale_name]
    money = Finder(locale_name, converter=None)
    for ct, ct2 in zip(locale['symbols'], locale['currencies']):
        if ct not in CURRENT_BAD and ct2 not in CURRENT_BAD:
            y_sim = get_default(locale['symbols'][ct])
            y_sim2 = get_default(locale['currencies'][ct2])
            mons = money_string.format(ms=ct, ms2=ct2)
            generic_test(mons, money, [500, y_sim, 2], [50, y_sim2, 2])
Esempio n. 5
0
def test_symbols3(locale_name, money_string, currency_type):
    locale = LOCALES[locale_name]
    money = Finder(locale_name, converter=None)
    keys1 = list(locale[currency_type])[:-1]
    keys2 = list(locale[currency_type])[1:]
    for ct, ct2 in zip(keys1, keys2):
        if ct not in CURRENT_BAD and ct2 not in CURRENT_BAD:
            y_sim = get_default(locale[currency_type][ct])
            mons = money_string.format(ms=ct, ms2=ct2)
            generic_test(mons, money, [500, y_sim, 2])
Esempio n. 6
0
def test_local():
    d = "/Users/pascal/sky_view_collections/www.news.com.au/"
    if not os.path.isdir(d):
        return
    from natura import Finder
    import justext
    import json

    default_money = Finder()

    stoplist = justext.get_stoplist("english")
    for fn in os.listdir(d):
        with open(d + fn) as f:
            data = json.load(f)
            body_content = "\n".join([
                x.text for x in justext.justext(data["html"], stoplist)
                if not x.is_boilerplate and not x.is_heading
            ])
            if "$" in body_content:
                assert default_money.findall(body_content)
Esempio n. 7
0
def test_text_amount(locale_name):
    money = Finder(locale_name, converter=None)
    if locale_name in ['us', 'eur_min', 'us_min']:
        money_string = "one million two hundred fifty-six thousand seven hundred twenty-one dollar"
        generic_test(money_string, money, [1256721, 'USD', 12])
    elif locale_name == 'nl':
        money_string = "een miljoen tweehonderdzesenvijftigduizend zevenhonderdeenentwintig dollar"
        generic_test(money_string, money, [1256721, 'USD', 5])
    elif locale_name == 'de':
        money_string = u"eine Million zweihundertsechsundfünfzigtausendsiebenhunderteinundzwanzig Dollar"
        generic_test(money_string, money, [1256721, 'USD', 4])
    elif locale_name == 'fr':
        money_string = "un million deux cent cinquante-six mille sept cent vingt et un dollars"
        generic_test(money_string, money, [1256721, 'USD', 13])
    elif locale_name == 'es':
        money_string = u"un millón doscientos cincuenta y seis mil setecientos veintiuno de dólares"
        generic_test(money_string, money, [1256721, 'USD', 10])
    else:
        assert False, "No test_text_amount for " + locale_name + ".json"
Esempio n. 8
0
CACHE = get_cache("linked_offers")


def getter(dc, key, default=None):
    res = dc.get(key, default)
    if isinstance(res, list):
        res = res[0]
    elif isinstance(res, dict):
        res = json.dumps(res)
    return res


from natura import Finder

finder = Finder()


def get_linked_data_jd(art):
    data = None
    try:
        jdata = art.jsonld
    except json.JSONDecodeError:
        return None
    for y in jdata:
        if not y:
            continue
        if isinstance(y, list):
            y = y[0]
        if y.get("@type") != "Product":
            continue
Esempio n. 9
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import pytest
from natura import Finder
from natura.utils import load_locale
from natura.conversion_backends import FixerIOExchangeRate

LOCALES = {}
for name in os.listdir("data/money"):
    if name.endswith(".json"):
        language = name.split(".json")[0]
        LOCALES[language] = load_locale(language)

default_money = Finder(converter=None)

CURRENT_BAD = []


def proove(x, y):
    value, currency, spans = y
    return x.value == value and x.currency == currency and len(
        x.spans) == spans


def get_default(symbols):
    for s in symbols:
        if s[1]:
            return s[0]