将字符串列表映射到对象的层次结构中

时间:2012-03-28 19:38:00

标签: parsing logic hierarchy

这不是作业问题。这个问题在面试中被问到了我的一位朋友。

我从文件中读取list行作为输入。每行在行首都有一个标识符,如(A,B,NN,C,DD)。根据标识符,我需要将记录列表映射到单个对象A,其中包含对象的层次结构。

enter image description here

层次结构说明: 每个A可以包含零个或多个B类型。 每个B标识符可以包含零个或多个NNC作为子级。同样,每个C细分可以包含零个或多个NNDD个孩子。 Abd每个DD可以有零个或多个NN作为孩子。

映射类及其层次结构:

所有课程都有value来保留当前行的String值。

**A - will have list of B**

    class A {
        List<B> bList;
        String value;

        public A(String value) {
            this.value = value;
        }

        public void addB(B b) {
            if (bList == null) {
                bList = new ArrayList<B>();
            }
            bList.add(b);
        }
    }


**B - will have list of NN and list of C**

    class B {
            List<C> cList;
            List<NN> nnList;
            String value;
                public B(String value) {
                this.value = value;
            }
                public void addNN(NN nn) {
                if (nnList == null) {
                    nnList = new ArrayList<NN>();
                }
                nnList.add(nn);
            }
                public void addC(C c) {
                if (cList == null) {
                    cList = new ArrayList<C>();
                }
                cList.add(c);
            }
        }

**C - will have list of DDs and NNs**

    class C {
            List<DD> ddList;
            List<NN> nnList;
            String value;
            public C(String value) {
                this.value = value;
            }
            public void addDD(DD dd) {
                if (ddList == null) {
                    ddList = new ArrayList<DD>();
                }
                ddList.add(dd);
            }
            public void addNN(NN nn) {
                if (nnList == null) {
                    nnList = new ArrayList<NN>();
                }
                nnList.add(nn);
            }
        }

**DD - will have list of NNs**

    class DD {
            String value;
            List<NN> nnList;
            public DD(String value) {
                this.value = value;
            }
            public void addNN(NN nn) {
                if (nnList == null) {
                    nnList = new ArrayList<NN>();
                }
                nnList.add(nn);
            }
        }

**NN- will hold the line only**

    class NN {
        String value;

        public NN(String value) {
            this.value = value;
        }
    }

我到目前为止做了什么:

方法public A parse(List<String> lines)读取输入列表并返回对象A。因为,可能有多个B,我创建了单独的方法'parseB来解析每个匹配项。

parseB 方法中,循环遍历i = startIndex + 1 to i < lines.size()并检查行的开头。 “NN”的出现被添加到B的当前对象。如果在开始时检测到“C”,则调用另一种方法parseC。当我们在开始时检测到“B”或“A”时,循环将中断。

在parseC_DD中使用了类似的逻辑。

public class GTTest {    
    public A parse(List<String> lines) {
        A a;
        for (int i = 0; i < lines.size(); i++) {
            String curLine = lines.get(i);
            if (curLine.startsWith("A")) { 
                a = new A(curLine);
                continue;
            }
            if (curLine.startsWith("B")) {
                i = parseB(lines, i); // returns index i to skip all the lines that are read inside parseB(...)
                continue;
            }
        }
        return a; // return mapped object
    }

    private int parseB(List<String> lines, int startIndex) {
        int i;
        B b = new B(lines.get(startIndex));
        for (i = startIndex + 1; i < lines.size(); i++) {
            String curLine = lines.get(i);
            if (curLine.startsWith("NN")) {
                b.addNN(new NN(curLine));
                continue;
            }
            if (curLine.startsWith("C")) {
                i = parseC(b, lines, i);
                continue;
            }
            a.addB(b);
            if (curLine.startsWith("B") || curLine.startsWith("A")) { //ending condition
                System.out.println("B A "+curLine);
                --i;
                break;
            }
        }
        return i; // return nextIndex to read
    }

    private int parseC(B b, List<String> lines, int startIndex) {

        int i;
        C c = new C(lines.get(startIndex));

        for (i = startIndex + 1; i < lines.size(); i++) {
            String curLine = lines.get(i);
            if (curLine.startsWith("NN")) {
                c.addNN(new NN(curLine));
                continue;
            }           

            if (curLine.startsWith("DD")) {
                i = parseC_DD(c, lines, i);
                continue;
            }

            b.addC(c);
            if (curLine.startsWith("C") || curLine.startsWith("A") || curLine.startsWith("B")) {
                System.out.println("C A B "+curLine);
                --i;
                break;
            }
        }
        return i;//return next index

    }

    private int parseC_DD(C c, List<String> lines, int startIndex) {
        int i;
        DD d = new DD(lines.get(startIndex));
        c.addDD(d);
        for (i = startIndex; i < lines.size(); i++) {
            String curLine = lines.get(i);
            if (curLine.startsWith("NN")) {
                d.addNN(new NN(curLine));
                continue;
            }
            if (curLine.startsWith("DD")) {
                d=new DD(curLine);
                continue;
            }       
            c.addDD(d);
            if (curLine.startsWith("NN") || curLine.startsWith("C") || curLine.startsWith("A") || curLine.startsWith("B")) {
                System.out.println("NN C A B "+curLine);
                --i;
                break;
            }

        }
        return i;//return next index

    }
public static void main(String[] args) {
        GTTest gt = new GTTest();
        List<String> list = new ArrayList<String>();
        list.add("A1");
        list.add("B1");
        list.add("NN1");
        list.add("NN2");
        list.add("C1");
        list.add("NNXX");
        list.add("DD1");
        list.add("DD2");
        list.add("NN3");
        list.add("NN4");
        list.add("DD3");
        list.add("NN5");
        list.add("B2");
        list.add("NN6");
        list.add("C2");
        list.add("DD4");
        list.add("DD5");
        list.add("NN7");
        list.add("NN8");
        list.add("DD6");
        list.add("NN7");
        list.add("C3");
        list.add("DD7");
        list.add("DD8");
        A a = gt.parse(list);
            //show values of a 

    }
}

我的逻辑不正常。您还可以找到其他方法吗?你对我的方式有什么建议/改进吗?

3 个答案:

答案 0 :(得分:7)

使用对象的层次结构:


    public interface Node {
        Node getParent();
        Node getLastChild();
        boolean addChild(Node n);
        void setValue(String value);
        Deque  getChildren();
    }

    private static abstract class NodeBase implements Node {
        ...     
        abstract boolean canInsert(Node n);    
        public String toString() {
            return value;
        }
        ...    
    }

    public static class A extends NodeBase {
        boolean canInsert(Node n) {
            return n instanceof B;
        }
    }
    public static class B extends NodeBase {
        boolean canInsert(Node n) {
            return n instanceof NN || n instanceof C;
        }
    }

    ...

    public static class NN extends NodeBase {
        boolean canInsert(Node n) {
            return false;
        }
    }

创建树类:

public class MyTree {

    Node root;
    Node lastInserted = null;

    public void insert(String label) {
        Node n = NodeFactory.create(label);

        if (lastInserted == null) {
            root = n;
            lastInserted = n;
            return;
        }
        Node current = lastInserted;
        while (!current.addChild(n)) {
            current = current.getParent();
            if (current == null) {
                throw new RuntimeException("Impossible to insert " + n);
            }
        }
        lastInserted = n;
    }
    ...
}

然后打印树:


public class MyTree {
    ...
    public static void main(String[] args) {
        List input;
        ...
        MyTree tree = new MyTree();
        for (String line : input) {
            tree.insert(line);
        }
        tree.print();
    }

    public void print() {
        printSubTree(root, "");
    }
    private static void printSubTree(Node root, String offset) {
        Deque  children = root.getChildren();
        Iterator i = children.descendingIterator();
        System.out.println(offset + root);
        while (i.hasNext()) {
            printSubTree(i.next(), offset + " ");
        }
    }
}

答案 1 :(得分:3)

具有5种状态的mealy自动机解决方案: 等待A 看过A 见过B 见过C ,和 见过DD

解析完全用一种方法完成。除了current之外,有一个NN节点是最后看到的节点。节点具有除根之外的父节点。在状态 see(0)中,current节点表示(0)(例如,状态看到C current可以是{上例中的{1}}。最令人烦恼的是状态看到的DD ,它具有最外向的边缘(C1BCDD)。< / p>

NN

我不满意那些在层次结构上向其他地方插入节点的火车残骸(public final class Parser { private final static class Token { /* represents A1 etc. */ } public final static class Node implements Iterable<Node> { /* One Token + Node children, knows its parent */ } private enum State { ExpectA, SeenA, SeenB, SeenC, SeenDD, } public Node parse(String text) { return parse(Token.parseStream(text)); } private Node parse(Iterable<Token> tokens) { State currentState = State.ExpectA; Node current = null, root = null; while(there are tokens) { Token t = iterator.next(); switch(currentState) { /* do stuff for all states */ /* example snippet for SeenC */ case SeenC: if(t.Prefix.equals("B")) { current.PN.PN.AddChildNode(new Node(t, current.PN.PN)); currentState = State.SeenB; } else if(t.Prefix.equals("C")) { } } return root; } } )。最终,显式状态类将使私有current.PN.PN方法更具可读性。然后,解决方案更类似于@AlekseyOtrubennikov提供的解决方案。也许直接parse方法会产生更美观的代码。也许最好将语法重新改写为LL并委托解析器创建。

<小时/> 一个简单的LL解析器,一个生产规则:

BNF

// "B" ("NN" || C)* private Node rule_2(TokenStream ts, Node parent) { // Literal "B" Node B = literal(ts, "B", parent); if(B == null) { // error return null; } while(true) { // check for "NN" Node nnLit = literal(ts, "NN", B); if(nnLit != null) B.AddChildNode(nnLit); // check for C Node c = rule_3(ts, parent); if(c != null) B.AddChildNode(c); // finished when both rules did not match anything if(nnLit == null && c == null) break; } return B; } 通过允许预测流量来TokenStream来增强Iterable<Token>,因为解析器必须在文字LL(1)之间进行选择,或者在两种情况下选择深潜(NN是其中之一)。看起来不错,但在这里缺少一些C#功能...

答案 2 :(得分:3)

@Stefan和@Aleksey是正确的:这是一个简单的解析问题。 您可以在Extended Backus-Naur Form中定义层次结构约束:

A  ::= { B }
B  ::= { NN | C }
C  ::= { NN | DD }
DD ::= { NN }

此描述可以转换为状态机并实现。但是有很多工具可以为您有效地做到这一点:Parser generators

我发布我的答案只是为了表明用Haskell(或其他一些功能语言)解决这些问题非常容易。
这是完整的程序,它从stdin读取字符串并将解析后的树打印到stdout。

-- We are using some standard libraries.
import Control.Applicative ((<$>), (<*>))
import Text.Parsec
import Data.Tree

-- This is EBNF-like description of what to do.
-- You can almost read it like a prose.
yourData = nodeA +>> eof

nodeA  = node "A"  nodeB
nodeB  = node "B" (nodeC  <|> nodeNN)
nodeC  = node "C" (nodeNN <|> nodeDD)
nodeDD = node "DD" nodeNN
nodeNN = (`Node` []) <$> nodeLabel "NN"

node lbl children
  = Node <$> nodeLabel lbl <*> many children

nodeLabel xx = (xx++)
  <$> (string xx >> many digit)
  +>> newline

-- And this is some auxiliary code.
f +>> g = f >>= \x -> g >> return x

main = do
  txt <- getContents
  case parse yourData "" txt of
    Left err  -> print err
    Right res -> putStrLn $ drawTree res

使用zz.txt中的数据执行它将打印出这个漂亮的树:

$ ./xxx < zz.txt 
A1
+- B1
|  +- NN1
|  +- NN2
|  `- C1
|     +- NN2
|     +- DD1
|     +- DD2
|     |  +- NN3
|     |  `- NN4
|     `- DD3
|        `- NN5
`- B2
   +- NN6
   +- C2
   |  +- DD4
   |  +- DD5
   |  |  +- NN7
   |  |  `- NN8
   |  `- DD6
   |     `- NN9
   `- C3
      +- DD7
      `- DD8

以下是处理格式错误输入的方法:

$ ./xxx
A1
B2
DD3
(line 3, column 1):
unexpected 'D'
expecting "B" or end of input