Python 模式表到数据帧
我正在使用Python“Pattern.en”包,该包为我提供关于特定句子的主题、对象和其他细节 但我想将这个输出存储到另一个变量或数据帧中,以便进一步处理,但我无法这样做 这方面的任何投入都会有所帮助 下面提到的示例代码仅供参考Python 模式表到数据帧,python,python-3.x,pandas,linguistics,Python,Python 3.x,Pandas,Linguistics,我正在使用Python“Pattern.en”包,该包为我提供关于特定句子的主题、对象和其他细节 但我想将这个输出存储到另一个变量或数据帧中,以便进一步处理,但我无法这样做 这方面的任何投入都会有所帮助 下面提到的示例代码仅供参考 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
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语句的输出存储到数据帧中,因为它是以表格格式打印的,那就更好了
但是当我尝试这样做时,我遇到了下面提到的错误
df = pd.DataFrame(input)
ValueError:未正确调用数据帧构造函数
以函数的来源为例,我得出了这个结论
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,
)
用法
看起来很基本,你看过熊猫的资料了吗?您的错误表明您没有正确调用构造函数——事实似乎确实如此。谢谢@Jacob。但我的问题不是如何解决我所犯的错误。它是如何将pattern.en包的输出存储到变量或数据帧中的。如果你对此有任何想法,请告诉我。希望这不是一个基本的问题,如果你认为这不是基本的问题,你可以重新考虑取消否决票。太棒了。你真是太棒了
>>> 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 ^ - - - -