简体   繁体   English

在 Python 中使用 NLTK 为序列解析单个树

[英]Parsing a single tree for a sequence using NLTK in Python

I want to parse a tree for an RNA sequence.我想解析一棵树的 RNA 序列。 I tokenized the RNA sequence in a list as is shown in the code below and parsed the trees:我在列表中标记了 RNA 序列,如下面的代码所示,并解析了树:

from __future__ import print_function
import nltk
import pdb
import numpy as np
import h5py
import RNA_vae
import equation_vae_copy
import RNA_grammar

sent = ['C', 'C', 'C', 'C', 'A', 'A', 'A', 'U', 'A', 'C', 'A', 'G', 'A', 'A', 'G', 'C', 'G', 'G', 'G', 'C', 'U', 'U', 'A']
parser = nltk.ChartParser(RNA_grammar.GCFG) 
parse_trees = [next(parser.parse(t)) for t in sent]

print(parse_trees)

But the output of the code is as below:但是代码的output如下:

[Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['U'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['G'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['A'])]), Tree('S', [Tree('L', ['G'])]), Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['G'])]), Tree('S', [Tree('L', ['G'])]), Tree('S', [Tree('L', ['G'])]), Tree('S', [Tree('L', ['C'])]), Tree('S', [Tree('L', ['U'])]), Tree('S', [Tree('L', ['U'])]), Tree('S', [Tree('L', ['A'])])]

I want to make a tree for the whole of the sequence, but it makes the trees for each of the characters in RNA.我想为整个序列制作一棵树,但它为 RNA 中的每个字符制作了树。 How can I generate a single tree for whole of the sequence?如何为整个序列生成一棵树?

The grammar is as below:语法如下:

# the RNA grammar
gram = """S -> LS
S -> L
LS -> L
LS -> S
L -> AFU
L -> UFA
L -> GFC
L -> CFG
L -> 'A'
L -> 'U'
L -> 'C'
L -> 'G'
F -> AFU
F -> UFA
F -> GFC
F -> CFG
F -> LS
AFU -> 'A'
AFU -> F
AFU -> 'U'
UFA -> 'U'
UFA -> F
UFA -> 'A'
GFC -> 'G'
GFC -> F
GFC -> 'C'
CFG -> 'C'
CFG -> F
CFG -> 'G'
Nothing -> Nones
"""

The grammar must be as below:语法必须如下:

RNA语法

Then, I changed the grammar as follows, but it still fails to parse a sequence:然后,我将语法更改如下,但仍然无法解析序列:

gram = """S -> L S | L
L -> 'A' F 'U' | 'A' | 'U' F 'A' | 'U' | 'C' F 'G' | 'C' | 'G' F 'C' | 'G'
F -> 'A' F 'U' | 'U' F 'A' | 'C' F 'G' | 'G' F 'C' | L S
Nothing -> Nones
"""

As discussed in the comments, you started with two fundamental problems:正如评论中所讨论的,您从两个基本问题开始:

  1. The grammar you wrote was only capable of handling a single character你写的语法只能处理一个字符

  2. You called your parser with one character each time.你每次用一个字符调用你的解析器。

The result was a vector of "parses" of each character, separately.结果是每个字符的“解析”向量,分别。

After fixing your grammar, as indicated in the editted question, changing the call to parser.parse to provide the entire sequence to be parsed produces 2100 possible parses.修正语法后,如已编辑的问题所示,更改对parser.parse的调用以提供要解析的整个序列会产生 2100 个可能的解析。

Here's what I did (and you can do it, too, by just copying the following code block into your python console):这是我所做的(您也可以这样做,只需将以下代码块复制到您的 python 控制台中):

# import only what's needed
import nltk
# The grammar
grammar = """
S -> L S | L
L -> 'A' F 'U' | 'A' | 'U' F 'A' | 'U' | 'C' F 'G' | 'C' | 'G' F 'C' | 'G'
F -> 'A' F 'U' | 'U' F 'A' | 'C' F 'G' | 'G' F 'C' | L S
"""
# Make a chartparser
parser = nltk.ChartParser(nltk.CFG.fromstring(grammar))
# The test sentence
sent = ['C', 'C', 'C', 'C', 'A', 'A', 'A',
        'U', 'A', 'C', 'A', 'G', 'A', 'A',
        'G', 'C', 'G', 'G', 'G', 'C', 'U',
        'U', 'A'
       ]
# Get all of the parses
parses = list(parser.parse(sent))
# There are a lot of them. len(parses) is 2100.
# Print one of them to the console
parses[0].pprint()

That prints:打印:

(S
  (L C)
  (S
    (L C)
    (S
      (L C)
      (S
        (L C)
        (S
          (L A)
          (S
            (L A)
            (S
              (L A)
              (S
                (L
                  U
                  (F
                    (L A)
                    (S
                      (L C)
                      (S
                        (L A)
                        (S
                          (L G)
                          (S
                            (L
                              A
                              (F
                                A
                                (F G (F C (F (L G) (S (L G))) G) C)
                                U)
                              U))))))
                  A)))))))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM