我是python的新手,并且正在努力解决数据类型概念及其转换问题。
我有NLTK树格式的句子(从斯坦福解析器获得并转换为NLTK树)。我需要应用为NLTK Chunker编写的函数。但是,NLTK树格式与NLTK Chunker格式不同。两种格式都是NLTK树,但元素结构似乎不同(见下文)。
你能帮我把NLTK树转换为NLTK Chunker输出格式吗?
提前致谢!
这是一个NLTK Chunker输出:
(S
(NP Pierre/NNP Vinken/NNP)
,/,
(NP 61/CD years/NNS old/JJ)
,/,
will/MD
join/VB
(NP the/DT board/NN)
as/IN
(NP a/DT nonexecutive/JJ director/NN Nov./NNP 29/CD)
./.)
现在按元素和每种元素类型打印:
class 'nltk.tree.Tree' (NP Pierre/NNP Vinken/NNP)
type 'tuple' (',', ',')
class 'nltk.tree.Tree' (NP 61/CD years/NNS old/JJ)
type 'tuple' (',', ',')
type 'tuple' ('will', 'MD')
type 'tuple' ('join', 'VB')
class 'nltk.tree.Tree' (NP the/DT board/NN)
type 'tuple' ('as', 'IN')
class 'nltk.tree.Tree' (NP a/DT nonexecutive/JJ director/NN Nov./NNP 29/CD)
type 'tuple' ('.', '.')
这是一个NLTK“纯”树输出(与NLTK doc完全相同):
(S
(NP
(NP (NNP Pierre) (NNP Vinken))
(, ,)
(ADJP (NP (CD 61) (NNS years)) (JJ old))
(, ,))
(VP
(MD will)
(VP
(VB join)
(NP (DT the) (NN board))
(PP (IN as) (NP (DT a) (JJ nonexecutive) (NN director) (NNP Nov.) (CD 29)))
))
(. .))
现在按元素和每种元素类型打印:
class 'nltk.tree.Tree' (NP
(NP (NNP Pierre) (NNP Vinken))
(, ,)
(ADJP (NP (CD 61) (NNS years)) (JJ old))
(, ,))
class 'nltk.tree.Tree' (NP (NNP Pierre) (NNP Vinken))
class 'nltk.tree.Tree' (NNP Pierre)
type 'str' Pierre
class 'nltk.tree.Tree' (NNP Vinken)
type 'str' Vinken
class 'nltk.tree.Tree' (, ,)
type 'str' ,
class 'nltk.tree.Tree' (ADJP (NP (CD 61) (NNS years)) (JJ old))
class 'nltk.tree.Tree' (NP (CD 61) (NNS years))
class 'nltk.tree.Tree' (CD 61)
type 'str' 61
class 'nltk.tree.Tree' (NNS years)
type 'str' years
class 'nltk.tree.Tree' (JJ old)
type 'str' old
class 'nltk.tree.Tree' (, ,)
type 'str' ,
class 'nltk.tree.Tree' (VP
(MD will)
(VP
(VB join)
(NP (DT the) (NN board))
(PP (IN as) (NP (DT a) (JJ nonexecutive) (NN director)))
(NP (NNP Nov.) (CD 29))))
class 'nltk.tree.Tree' (MD will)
type 'str' will
class 'nltk.tree.Tree' (VP
(VB join)
(NP (DT the) (NN board))
(PP (IN as) (NP (DT a) (JJ nonexecutive) (NN director)))
(NP (NNP Nov.) (CD 29)))
class 'nltk.tree.Tree' (VB join)
type 'str' join
class 'nltk.tree.Tree' (NP (DT the) (NN board))
class 'nltk.tree.Tree' (DT the)
type 'str' the
class 'nltk.tree.Tree' (NN board)
type 'str' board
class 'nltk.tree.Tree' (PP (IN as) (NP (DT a) (JJ nonexecutive) (NN director)))
class 'nltk.tree.Tree' (IN as)
type 'str' as
class 'nltk.tree.Tree' (NP (DT a) (JJ nonexecutive) (NN director))
class 'nltk.tree.Tree' (DT a)
type 'str' a
class 'nltk.tree.Tree' (JJ nonexecutive)
type 'str' nonexecutive
class 'nltk.tree.Tree' (NN director)
type 'str' director
class 'nltk.tree.Tree' (NP (NNP Nov.) (CD 29))
class 'nltk.tree.Tree' (NNP Nov.)
type 'str' Nov.
class 'nltk.tree.Tree' (CD 29)
type 'str' 29
class 'nltk.tree.Tree' (. .)
type 'str' .
答案 0 :(得分:2)
部分答案(即无代码):
NLTK使用Tree
类表示分块数据,该类实际上是为任意语法树设计的。一个分块的句子是一个只有一个分组级别的树,所以要从完整的解析到一个分块结构,你需要丢弃除了一种非递归组之外的所有组。哪个组?这取决于你的应用,因为有不同种类的"块" (例如,命名实体)。
您的示例显示了NP块,因此您可以遍历树并省略除NP顶层之外的所有结构(如果您想将复杂的NP块分解为小块,则省略最低层)。