2017-10-20 14 views
-3

필자는 특정 문장에 대한 주제, 객체 및 기타 세부 정보를 제공하는 Python "Pattern.en"패키지를 사용하고 있습니다.Pandas DataFrame의 패턴 테이블

그러나이 출력을 다른 변수 또는 Dataframe에 저장하여 추가 처리가 가능하도록하고 싶습니다.

이 정보는 도움이 될 것입니다.

샘플 코드는 참조 용으로 아래에 언급되어 있습니다.

from pattern.en import parse 
from pattern.en import pprint 
import pandas as pd 

input = parse('I want to go to the Restaurant as I am hungry very much') 
print(input)  
I/PRP/B-NP/O want/VBP/B-VP/O to/TO/I-VP/O go/VB/I-VP/O to/TO/O/O the/DT/B-NP/O Restaurant/NNP/I-NP/O as/IN/B-PP/B-PNP I/PRP/B-NP/I-PNP am/VBP/B-VP/O hungry/JJ/B-ADJP/O very/RB/I-ADJP/O much/JJ/I-ADJP/O 

pprint(input) 

     WORD TAG CHUNK ROLE ID  PNP LEMMA             
     I PRP NP  -  -  -  -  
     want VBP VP  -  -  -  -  
     to TO  VP^ -  -  -  -  
     go VB  VP^ -  -  -  -  
     to TO  -  -  -  -  -  
     the DT  NP  -  -  -  -  
Restaurant NNP NP^ -  -  -  -  
     as IN  PP  -  -  PNP -  
     I PRP NP  -  -  PNP -  
     am VBP VP  -  -  -  -  
    hungry JJ  ADJP  -  -  -  -  
     very RB  ADJP^ -  -  -  -  
     much JJ  ADJP^ -  -  -  -  

print 및 pprint 문의 출력에주의하십시오. 변수 중 하나를 저장하려고합니다. pprint 문의 출력을 표 형식으로 인쇄 할 때 Dataframe에 저장할 수 있다면 더 좋을 것입니다. 내가 그렇게하려고 할 때

는하지만 table 기능의 소스를 촬영

df = pd.DataFrame(input) 

ValueError: DataFrame constructor not properly called!

+0

팬더의 문서를 읽어 보셨습니까? https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html 오류로 인해 생성자를 올바르게 호출하지 못했다고합니다. 실제로 발생한 것 같습니다. – Jacob

+0

감사합니다. @ Jacob. 하지만 내 문제는 내가 가진 오류를 해결하는 방법이 아닙니다. pattern.en 패키지의 출력을 변수 또는 데이터 프레임에 저장하는 방법입니다. 그래서 그것에 대해 알고 싶다면 알려주세요. 희망이 기본 하나가 아니며 downvote를 제거하는 것을 생각할 수 있습니다. 기본이 아니라면 이것을 생각하십시오. – JKC

답변

1

아래에 언급 된 오류가 발생,이

from pattern.en import parse 
from pattern.text.tree import WORD, POS, CHUNK, PNP, REL, ANCHOR, LEMMA, IOB, ROLE, MBSP, Text 
import pandas as pd 

def sentence2df(sentence, placeholder="-"): 
    tags = [WORD, POS, IOB, CHUNK, ROLE, REL, PNP, ANCHOR, LEMMA] 
    tags += [tag for tag in sentence.token if tag not in tags] 
    def format(token, tag): 
     # Returns the token tag as a string. 
     if tag == WORD : s = token.string 
     elif tag == POS : s = token.type 
     elif tag == IOB : s = token.chunk and (token.index == token.chunk.start and "B" or "I") 
     elif tag == CHUNK : s = token.chunk and token.chunk.type 
     elif tag == ROLE : s = token.chunk and token.chunk.role 
     elif tag == REL : s = token.chunk and token.chunk.relation and str(token.chunk.relation) 
     elif tag == PNP : s = token.chunk and token.chunk.pnp and token.chunk.pnp.type 
     elif tag == ANCHOR : s = token.chunk and token.chunk.anchor_id 
     elif tag == LEMMA : s = token.lemma 
     else    : s = token.custom_tags.get(tag) 
     return s or placeholder 

    columns = [[format(token, tag) for token in sentence] for tag in tags] 
    columns[3] = [columns[3][i]+(iob == "I" and " ^" or "") for i, iob in enumerate(columns[2])] 
    del columns[2] 
    header = ['word', 'tag', 'chunk', 'role', 'id', 'pnp', 'anchor', 'lemma']+tags[9:] 

    if not MBSP: 
     del columns[6] 
     del header[6] 

    return pd.DataFrame(
     [[x[i] for x in columns] for i in range(len(columns[0]))], 
     columns=header, 
    ) 

사용으로 나올

>>> string = parse('I want to go to the Restaurant as I am hungry very much') 
>>> sentence = Text(string, token=[WORD, POS, CHUNK, PNP])[0] 
>>> df = sentence2df(sentence) 
>>> print(df) 
      word tag chunk role id pnp lemma 
0   I PRP  NP - - -  - 
1   want VBP  VP - - -  - 
2   to TO VP^ - - -  - 
3   go VB VP^ - - -  - 
4   to TO  - - - -  - 
5   the DT  NP - - -  - 
6 Restaurant NNP NP^ - - -  - 
7   as IN  PP - - PNP  - 
8   I PRP  NP - - PNP  - 
9   am VBP  VP - - -  - 
10  hungry JJ ADJP - - -  - 
11  very RB ADJP^ - - -  - 
12  much JJ ADJP^ - - -  - 
+0

와우. 정말 끝내주는 군. 너는 위대한 @pacholik이다. – JKC