コード例 #1
0
 def test_money_annotation(self):
     ant = MoneyAnnotation(coords=(2, 20),
                           amount=1001,
                           locale='de',
                           currency='JPY')
     self.assertEqual('de', ant.locale)
     cite = ant.get_cite()
     self.assertEqual('/de/money/1001/JPY', cite)
コード例 #2
0
ファイル: money.py プロジェクト: denmonz/Orrick-Flashcards
def get_money_annotations(text: str, float_digits=4) \
        -> Generator[MoneyAnnotation, None, None]:
    for match in CURRENCY_PTN_RE.finditer(text):
        capture = match.capturesdict()
        if not (capture['prefix']
                or capture['postfix']) and not (capture['trigger_word']):
            continue
        prefix = capture['prefix']
        postfix = capture['postfix']
        amount = list(
            get_amounts(capture['amount'][0], float_digits=float_digits))
        if len(amount) != 1:
            continue
        if prefix:
            prefix = prefix[0].lower()
            currency_type = CURRENCY_SYMBOL_MAP.get(prefix)\
                            or CURRENCY_PREFIX_MAP.get(prefix)\
                            or prefix.upper()
        elif postfix:
            postfix = postfix[0].lower()
            currency_type = CURRENCY_TOKEN_MAP.get(postfix) or (
                capture['postfix'][0]).upper()
        else:
            currency_type = None
        if not currency_type:
            currency_type = DEFAULT_CURRENCY
        text = capture['text'][0].strip(
            string.punctuation.replace('$', '') + string.whitespace)
        ant = MoneyAnnotation(coords=match.span(),
                              amount=amount[0],
                              text=text,
                              currency=currency_type)
        yield ant
コード例 #3
0
 def get_money_annotations(
     self,
     text: str,
     float_digits: int = 4,
 ) -> Generator[MoneyAnnotation, None, None]:
     for match in self.currency_ptn_re.finditer(text):
         capture = match.capturesdict()
         if not (capture['prefix']
                 or capture['postfix']) and not (capture['trigger_word']):
             continue
         prefix = capture['prefix']
         postfix = capture['postfix']
         amount: List[Union[Decimal, Tuple[Decimal, str]]] = \
             list(self.get_amounts(capture['amount'][0], float_digits=float_digits))
         if len(amount) != 1:
             continue
         if prefix:
             prefix = prefix[0].lower()
             currency_type = self.currency_symbol_map.get(prefix)\
                             or self.currency_preffix_map.get(prefix)\
                             or prefix.upper()
         elif postfix:
             postfix = postfix[0].lower()
             currency_type = self.currency_token_map.get(postfix) or (
                 capture['postfix'][0]).upper()
         else:
             currency_type = None
         if not currency_type:
             currency_type = self.default_currency
         text = capture['text'][0].strip(
             string.punctuation.replace('$', '') + string.whitespace)
         yield MoneyAnnotation(locale=self.locale,
                               coords=match.span(),
                               amount=amount[0],
                               text=text,
                               currency=currency_type)