首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyParsing书目引文

PyParsing书目引文
EN

Stack Overflow用户
提问于 2017-01-14 14:45:29
回答 1查看 283关注 0票数 3

我在PyParsing上遇到了麻烦。我需要从简历中分析一些书目信息。举个例子:

AuthorA,B.,AuthorB,M. R.,AuthorC,V.和B. LastAuthor。一些科学头衔。会议名称,城市,州,2012年12月3日

我想出了一些代码来解析(主要)作者列表和日期.其他信息对我来说并不特别重要。

代码语言:javascript
复制
from pyparsing import (Word, Literal, OneOrMore, alphanums, delimitedList, printables, 
    alphas, nums)

family_name = Word(alphanums+'-')
first_init = Word(alphanums+'.')
author = (family_name("LastName") + Literal(',').suppress() + 
          OneOrMore(first_init("FirstInitials") ) )
last_author = first_init("FirstInitials") + family_name("LastName")

author_list = delimitedList(author) + Literal('and').suppress() + last_author

sentence = OneOrMore(Word(printables))
location = delimitedList(Word(printables))
date = Word(alphas) + Word(nums) + Literal(',').suppress() + Word(nums)

citation = (author_list('AuthorLst') + sentence('Title') + location('Location') 
            + date('Date'))

citation.parseString(ntext)

然而,它放屁的“和”作为提交人名单和最后一个作者之间的区别。

我得到了错误消息:

代码语言:javascript
复制
---------------------------------------------------------------------------
ParseException                            Traceback (most recent call last)
<ipython-input-142-5d7946dcb775> in <module>()
     15 
     16 
---> 17 citation.parseString(ntext)

/Users/willdampier/anaconda/lib/python2.7/site-packages/pyparsing.pyc in parseString(self, instring, parseAll)
   1123             else:
   1124                 # catch and re-raise exception from here, clears out pyparsing internal stack trace
-> 1125                 raise exc
   1126         else:
   1127             return tokens

ParseException: Expected "and" (at char 40), (line:1, col:41)

有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-14 18:12:30

在定义author之后,添加以下一行:

代码语言:javascript
复制
author.setName("author").setDebug()

跟踪author表达式的匹配。然后,要获得更好的总体诊断,请将测试行更改为:

代码语言:javascript
复制
author_list.runTests(ntext)

通过这些更改,您将得到如下的输出:

代码语言:javascript
复制
Match author at loc 0(1,1)
Matched author -> ['AuthorA', 'B.']
Match author at loc 12(1,13)
Matched author -> ['AuthorB', 'M.', 'R.']
Match author at loc 28(1,29)
Matched author -> ['AuthorC', 'V.']
Match author at loc 41(1,42)
Exception raised:Expected "," (at char 46), (line:1, col:47)

AuthorA, B., AuthorB, M. R., AuthorC, V., and B. LastAuthor. Some sciency title. Name of the confernce, City, State, December 3, 2012
                                        ^
FAIL: Expected "and" (at char 40), (line:1, col:41)

因此,您当前的问题是,在“和”之前,您没有处理拖尾“和”。您还需要添加尾随‘’。对author_list的定义。

但是从那里开始,您的sentence解析器将出现问题,因为它将处理整个字符串的其余部分。因为你的主要兴趣是得到日期,所以这可能会为你做好以下工作:

代码语言:javascript
复制
stuff = OneOrMore(Word(printables), stopOn=date)
citation = (author_list('AuthorLst') + stuff('body') + date('Date'))

最后,关于结果名称的使用("FirstInitials“、"LastName”等)。做得很好,这是我特别喜欢的一个特性。但是您需要对每个作者引用的名称进行一些隔离,否则只会得到最后一个作者的名字。要做到这一点,请将每个作者包装在一个pyparsing中:

代码语言:javascript
复制
author = Group(family_name("LastName") + Literal(',').suppress() + 
          OneOrMore(first_init("FirstInitials") ) )
last_author = Group(first_init("FirstInitials") + family_name("LastName"))

现在,您的author_list应该给出一个子结构列表。如果你这样做,你就能看到它们:

代码语言:javascript
复制
print(citation.parseString(ntext).dump())

通过我的更改,我得到了这个示例文本:

代码语言:javascript
复制
[['AuthorA', 'B.'], ['AuthorB', 'M.', 'R.'], ['AuthorC', 'V.'], ',', 
 ['B.', 'LastAuthor'], '.', 'Some', 'sciency', 'title.', 'Name', 'of', 
 'the', 'confernce,', 'City,', 'State,', 'December', '3', '2012']
- AuthorLst: [['AuthorA', 'B.'], ['AuthorB', 'M.', 'R.'], 
              ['AuthorC', 'V.'], ',', ['B.', 'LastAuthor'], '.']
  [0]:
    ['AuthorA', 'B.']
    - FirstInitials: 'B.'
    - LastName: 'AuthorA'
  [1]:
    ['AuthorB', 'M.', 'R.']
    - FirstInitials: 'R.'
    - LastName: 'AuthorB'
  [2]:
    ['AuthorC', 'V.']
    - FirstInitials: 'V.'
    - LastName: 'AuthorC'
  [3]:
    ,
  [4]:
    ['B.', 'LastAuthor']
    - FirstInitials: 'B.'
    - LastName: 'LastAuthor'
  [5]:
    .

仍然需要抑制“,”和“。”标点符号,但那只是清理。然后,您将能够轻松地遍历您的作者列表并获得每个作者的姓名。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41651200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档