/*
 * Copyright 2018 Dmitry Ustalov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.nlpub.watset.io;

import org.jgrapht.Graph;
import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.SimpleWeightedGraph;
import org.jgrapht.graph.builder.GraphBuilder;

import java.util.stream.Stream;

public interface ABCParser {
    static Graph<String, DefaultWeightedEdge> parse(Stream<String> stream) {
        final GraphBuilder<String, DefaultWeightedEdge, SimpleWeightedGraph<String, DefaultWeightedEdge>> builder = new GraphBuilder<>(new SimpleWeightedGraph<>(DefaultWeightedEdge.class));

        stream.forEach(line -> {
            final String[] split = line.split("\t");
            if (split.length != 3 || split[0].equals(split[1])) return;
            builder.addVertices(split[0], split[1]);
            builder.addEdge(split[0], split[1], Double.valueOf(split[2]));
        });

        return builder.build();
    }
}

@@ -924,16 +924,30 @@
 %3E stream
+, String regex
 ) %7B%0A    
@@ -997,16 +997,26 @@
 tedEdge,
+ ? extends
  SimpleW
@@ -1071,31 +1071,8 @@
 er =
- new GraphBuilder%3C%3E(new
  Sim
@@ -1091,10 +1091,22 @@
 raph
-%3C%3E
+.createBuilder
 (Def
@@ -1128,17 +1128,16 @@
 e.class)
-)
 ;%0A%0A     
@@ -1214,15 +1214,17 @@
 lit(
-%22%5Ct%22
+regex
 );%0A
+%0A
     
@@ -1291,16 +1291,17 @@
 return;%0A
+%0A
         
@@ -1345,16 +1345,17 @@
 it%5B1%5D);%0A
+%0A
         
@@ -1471,11 +1471,131 @@
 );%0A    %7D
+%0A%0A    static Graph%3CString, DefaultWeightedEdge%3E parse(Stream%3CString%3E stream) %7B%0A        return parse(stream, %22%5Ct%22);%0A    %7D
 %0A%7D%0A

Simplify ABCParser