[英]Extracting information from raw text
Problem Description问题描述
Here is the text pattern I have:这是我拥有的文本模式:
05.04.0090
1
erhältlichen Tableau Interfaces
lassen sich zusätzliche GLT-Kontakte
aufschalten. Das System kann
die zwei Szenarien-Modi "Urlaub" und
Abwesenheit" verwalten. Für beide
Modi können bestimmte Parameter
programmiert werden.
Das WAREMA climatronic Bediengerät
kann preisgleich auch
in den Farben "schwarz" oder
"schwarz/silber" geliefert werden.
Liefern und montieren. 882,75 882,75
The above text block has item_code
that has this norm to write 05.04.0090
then underneath is count of the item 1
then followed by description of product
then unit price
in this case EU 882,75
and at the end total sum
that is 882,74
上面的文本块有
item_code
有这个规范写05.04.0090
然后下面是项目1
计数,然后是产品description of product
然后是unit price
在这种情况下为 EU 882,75
,最后sum
为882,74
What i want I want to make key value pairs out of it like dict[{'item_code':'5.04.009','quant':'1','description':'TEXT','unit_price':'882,74','Total_sum':'88,75'}]
我想要什么我想像
dict[{'item_code':'5.04.009','quant':'1','description':'TEXT','unit_price':'882,74','Total_sum':'88,75'}]
I will be using this pattern in spacy at the end to recognize the entities.最后我将在 spacy 中使用这种模式来识别实体。 Any suggestion regarding spacy would be great also
任何关于 spacy 的建议也会很棒
What I have tried?我尝试过什么?
pat= re.search(r'\d(.*?)\d',text,re.M)
print(pat.group())
Help or suggestion will be appreciated.帮助或建议将不胜感激。
The pprint.pprint function is used for output. pprint.pprint函数用于输出。 For an explanation of the regular expression used, please go to RegEx101 .
有关所用正则表达式的说明,请转到RegEx101 。
import re
import pprint
data = '''
05.04.0090
1
erhältlichen Tableau Interfaces
lassen sich zusätzliche GLT-Kontakte
aufschalten. Das System kann
die zwei Szenarien-Modi "Urlaub" und
Abwesenheit" verwalten. Für beide
Modi können bestimmte Parameter
programmiert werden.
Das WAREMA climatronic Bediengerät
kann preisgleich auch
in den Farben "schwarz" oder
"schwarz/silber" geliefert werden.
Liefern und montieren. 882,75 882,75
05.04.0091
100
foo bar. 170,42 17042
'''
rx = r'''(?mx)
^
(?P<item_code>\d\d\.\d\d\.\d{4})
\s+
(?P<quantity>\d+)
\s+
(?P<description>\S[\s\S]*?)
[ ]+
(?P<unit_price>\d+(?:,\d\d)?)
[ ]+
(?P<total_sum>\d+(?:,\d\d)?)
$
'''
result = [m.groupdict() for m in re.finditer(rx, data)]
pprint.pprint(result)
Output:输出:
[{'description': 'erhältlichen Tableau Interfaces\n'
'lassen sich zusätzliche GLT-Kontakte\n'
'aufschalten. Das System kann\n'
'\n'
'die zwei Szenarien-Modi "Urlaub" und\n'
'Abwesenheit" verwalten. Für beide\n'
'Modi können bestimmte Parameter\n'
'programmiert werden.\n'
'\n'
'Das WAREMA climatronic Bediengerät\n'
'kann preisgleich auch\n'
'\n'
'in den Farben "schwarz" oder\n'
'"schwarz/silber" geliefert werden.\n'
'Liefern und montieren.',
'item_code': '05.04.0090',
'quantity': '1',
'total_sum': '882,75',
'unit_price': '882,75'},
{'description': 'foo bar.',
'item_code': '05.04.0091',
'quantity': '100',
'total_sum': '17042',
'unit_price': '170,42'}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.