2017-01-14 4 views
2

私はPyParsingにいくつか問題があります。私はCVからいくつかの書誌情報を解析する必要があります。例:参考文献引用をPyParsing

AuthorA、B.、AuthorB、M.R.、AuthorC、V.、およびB. LastAuthor。一部 有効性タイトル。都市名、州名、2012年12月3日

著者リストと日付を(主に)解析するコードがありました。他の情報は特に重要ではありません。

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) 

ただし、著者リストと最終作者の区別として "and"で検索されます。

--------------------------------------------------------------------------- 
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) 

任意の提案:

は、私はエラーメッセージが表示されますか?

+0

あなたのサンプルと現在のコードはどのような出力を得ていますか? (ちょうどこの文脈で "おなら"が何を意味するかを知るために) – usr2564301

+0

'Word'、' OneOrMore'などとは何ですか?これらは正規表現ライブラリからのものですか?これらのインポート文を再現するために追加できますか? – hansaplast

+1

エラーメッセージと 'pyparsing'からのインポートを追加するように編集しました。 – JudoWill

答えて

3

authorを定義した後、次の行を追加します。

author.setName("author").setDebug() 

author表現のマッチングを追跡します。その後、いくつかのより全体的な診断を得るためにあなたのテストラインを変更するには:

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.runTests(ntext) 

、次のような出力が得られます末尾に '、'の前に 'と'。また、末尾に '。'を追加する必要があります。あなたの定義はauthor_listです。

しかしそこから、sentenceのパーサは、残りの文字列全体を処理するので、問題があります。あなたの主な関心は、日付を取得しているので、これはあなたのために仕事をすることがあります

stuff = OneOrMore(Word(printables), stopOn=date) 
citation = (author_list('AuthorLst') + stuff('body') + date('Date')) 

最後に、結果の名前の使用(「FirstInitials」、「姓」、など)に関して。さて、これは機能です 私は特にpyparsingに満足しています。しかし、各著者のリファレンスから名前の一部を分離する必要があります。 それ以外の場合は、最後の著者の名前のみを取得します。これを行うには、pyparsingグループ内の各著者をラップ:

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

今すぐあなたのauthor_listはあなたの部分構造のリストを与える必要があります。あなたが行う場合は、それらを見ることができます:私の変更により

print(citation.parseString(ntext).dump()) 

を、私はあなたのサンプルテキストのためにこれを取得する:

[['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]: 
    . 

はまだ「」抑制するために必要と「」句読点は、それだけでクリーンアップです。その後、簡単に の著者リストを繰り返し、各著者の名前を取得することができます。

+1

それは素晴らしいです!私はデバッグ/テストのアーキテクチャーについて全く新しいことはありません。後ろのカンマに問題があると思ったが、うまくいかなかった。 – JudoWill

関連する問題