【问题标题】:How to tokenize all currency symbols using Regex in python?如何在 python 中使用正则表达式标记所有货币符号?
【发布时间】:2016-03-03 07:32:46
【问题描述】:

我想通过使用带有正则表达式的 NLTK 标记化来标记所有货币符号。

例如这是我的句子:

The price of it is $5.00.
The price of it is RM5.00.
The price of it is €5.00.

我使用了这种正则表达式模式:

pattern = r'''(['()""\w]+|\.+|\?+|\,+|\!+|\$?\d+(\.\d+)?%?)'''
tokenize_list = nltk.regexp_tokenize(sentence, pattern)

但正如我们所见,它只考虑 $。

我尝试按照What is regex for currency symbol? 中的说明使用\p{Sc},但它仍然不适合我。

【问题讨论】:

  • 我尝试使用相同的方式,但仍然无法得到正确的答案。 @b3000
  • 您能否展示您尝试使用\p{Sc} 以及为什么它不起作用?

标签: python regex nlp nltk tokenize


【解决方案1】:

尝试用带空格的货币符号填充编号,然后标记化:

>>> import re
>>> from nltk import word_tokenize
>>> sents = """The price of it is $5.00.
... The price of it is RM5.00.
... The price of it is €5.00.""".split('\n')
>>>
>>> for sent in sents:
...     numbers_in_sent = re.findall("[-+]?\d+[\.]?\d*", sent)
...     for num in numbers_in_sent:
...             sent = sent.replace(num, ' '+num+' ')
...     print word_tokenize(sent)
... 
['The', 'price', 'of', 'it', 'is', '$', '5.00', '.']
['The', 'price', 'of', 'it', 'is', 'RM', '5.00', '.']
['The', 'price', 'of', 'it', 'is', '\xe2\x82\xac', '5.00', '.']

【讨论】:

    猜你喜欢
    • 2014-11-16
    • 1970-01-01
    • 2012-11-30
    • 1970-01-01
    • 2014-03-22
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    相关资源
    最近更新 更多