Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用pyparsing解析非结构化文本_Python_Pyparsing - Fatal编程技术网

在Python中使用pyparsing解析非结构化文本

在Python中使用pyparsing解析非结构化文本,python,pyparsing,Python,Pyparsing,我有数百个company report.txt文件,我想从中提取一些信息。例如,文件的一部分如下所示: Mr. Davido will receive a base salary of $700,000 during the initial and any subsequent term. The Chief Executive Officer of the Company (the CEO) and the Board (or a committee thereof) shall revie

我有数百个company report.txt文件,我想从中提取一些信息。例如,文件的一部分如下所示:

Mr. Davido will receive a base salary of $700,000 during the initial and any subsequent 
term. The Chief Executive Officer of the Company (the CEO) and the Board (or a committee
thereof) shall review Mr. Davidos base salary at least annually, and may increase it at 
any time in their sole discretion
我试图使用pyparsing来提取这个家伙的基本工资值

代码

from pyparsing import * 

# define grammar
digits = "0123456789"
integer = Word( digits )
money = Group("$"+integer+','+integer + Optional(','+integer , ' '))
start = Word("base salary") 
salary = start + money

#search
for t in text:
  result = salary.parseString( text )
print result
这总是会产生以下错误:

pyparsing.ParseException: Expected W:(base...) (at char 0), (line:1, col:1)
经过一些简单的测试,我发现使用这段代码,我只能从以下特定形式的文本中找到我想要的:

"base salary $700,000......"
它只能识别文本中出现的第一个案例

所以我想知道是否有人能帮我。而且,如果可能的话,还可以识别那个家伙的名字,并将名字和薪水存储到数据框中


非常感谢。

我先回答你的具体问题。当您定义了与文本开头的所有内容相匹配的综合语法时,将使用parseString。因为您试图从输入行中间的某个地方挑选出特定的短语,所以使用搜索字符串或扫描字符串代替。
作为pyparsing的作者,我同意@Tritium21的观点——除非你能找到一些特定的形式和短语,否则你在尝试解析这种自然语言输入时会大发雷霆。

我会继续说你不能。Pyparsing是针对结构化文本的,您遇到的是自然语言问题。NLTK可能(可能!)是使用的工具。。。虽然我会使用的工具是实习生。非常感谢@Tritium21,我会尝试一下NLTK。非常感谢Paul,我会尝试其他工具包。