|
| 1 | +/** |
| 2 | + * Copyright 2015, Emory University |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package edu.emory.clir.clearnlp.extraction.attribute.corpus; |
| 17 | + |
| 18 | +import java.io.BufferedInputStream; |
| 19 | +import java.io.BufferedReader; |
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.InputStreamReader; |
| 22 | +import java.io.PrintWriter; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.stream.Collectors; |
| 28 | + |
| 29 | +import edu.emory.clir.clearnlp.collection.pair.IntIntPair; |
| 30 | +import edu.emory.clir.clearnlp.dependency.DEPTree; |
| 31 | +import edu.emory.clir.clearnlp.extraction.attribute.utils.chunk.AbstractChucker; |
| 32 | +import edu.emory.clir.clearnlp.extraction.attribute.utils.chunk.Chunk; |
| 33 | +import edu.emory.clir.clearnlp.extraction.attribute.utils.chunk.EnglishProperNounChunker; |
| 34 | +import edu.emory.clir.clearnlp.reader.AbstractReader; |
| 35 | +import edu.emory.clir.clearnlp.reader.TSVReader; |
| 36 | +import edu.emory.clir.clearnlp.util.IOUtils; |
| 37 | +import edu.emory.clir.clearnlp.util.Joiner; |
| 38 | +import edu.emory.clir.clearnlp.util.Splitter; |
| 39 | +import edu.emory.clir.clearnlp.util.constant.StringConst; |
| 40 | +import edu.emory.clir.clearnlp.util.lang.TLanguage; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Yu-Hsin(Henry) Chen ({@code [email protected]}) |
| 44 | + * @version 1.0 |
| 45 | + * @since Sep 22, 2015 |
| 46 | + */ |
| 47 | +public class EntityTokenCorpusReconstructor extends AbstractCorpusRecontructor{ |
| 48 | + |
| 49 | + private NLPDecoder decoder; |
| 50 | + private Set<String> s_NERLables; |
| 51 | + private AbstractReader<DEPTree> reader; |
| 52 | + |
| 53 | + public EntityTokenCorpusReconstructor(CorpusType type, String in_dir, String out_dir) { |
| 54 | + super(type, in_dir, out_dir); init(); |
| 55 | + } |
| 56 | + |
| 57 | + public EntityTokenCorpusReconstructor(CorpusType type, String in_dir, String out_dir, Set<String> extensions) { |
| 58 | + super(type, in_dir, out_dir, extensions); init(); |
| 59 | + } |
| 60 | + |
| 61 | + public EntityTokenCorpusReconstructor(CorpusType type, String in_dir, String out_dir, Set<String> extensions, Set<String> NERLabels) { |
| 62 | + super(type, in_dir, out_dir, extensions); |
| 63 | + s_NERLables = NERLabels; init(); |
| 64 | + } |
| 65 | + |
| 66 | + public EntityTokenCorpusReconstructor(CorpusType type, List<String> filePaths, String in_dir, String out_dir, Set<String> NERLabels){ |
| 67 | + super(type, filePaths, in_dir, out_dir); |
| 68 | + s_NERLables = NERLabels; init(); |
| 69 | + } |
| 70 | + |
| 71 | + private void init(){ |
| 72 | + switch (type) { |
| 73 | + case RAW: decoder = new NLPDecoder(TLanguage.ENGLISH); break; |
| 74 | + case TSV: reader = new TSVReader(0, 1, 2, 3, 7, 4, 5, 6, -1, -1); break; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void reconstruct(List<String> filePaths) { |
| 80 | + |
| 81 | + String line; |
| 82 | + DEPTree tree; |
| 83 | + List<Chunk> chunks; |
| 84 | + List<DEPTree> trees; |
| 85 | + List<String> sentence; |
| 86 | + AbstractChucker chunker = new EnglishProperNounChunker(s_NERLables); |
| 87 | + |
| 88 | + InputStream input; |
| 89 | + PrintWriter writer; |
| 90 | + BufferedReader raw_reader; |
| 91 | + |
| 92 | + for(String filePath : filePaths){ |
| 93 | + try { |
| 94 | + input = IOUtils.createFileInputStream(filePath); |
| 95 | + writer = new PrintWriter(IOUtils.createBufferedPrintStream(getOutputPath(filePath))); |
| 96 | + |
| 97 | + switch(type){ |
| 98 | + case RAW: |
| 99 | + raw_reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(input))); |
| 100 | + raw_reader.readLine(); |
| 101 | + |
| 102 | + while( (line = raw_reader.readLine()) != null){ |
| 103 | + trees = decoder.toDEPTrees(Splitter.splitTabs(line)[1]); |
| 104 | + |
| 105 | + for(DEPTree t : trees){ |
| 106 | + chunks = chunker.getChunk(t); |
| 107 | + sentence = reconstructAux(t, chunks); |
| 108 | + writer.println(Joiner.join(sentence, StringConst.SPACE)); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + break; |
| 113 | + case TSV: |
| 114 | + reader.open(new BufferedInputStream(input)); |
| 115 | + |
| 116 | + while( (tree = reader.next()) != null){ |
| 117 | + chunks = chunker.getChunk(tree); |
| 118 | + sentence = reconstructAux(tree, chunks); |
| 119 | + writer.println(Joiner.join(sentence, StringConst.SPACE)); |
| 120 | + } |
| 121 | + break; |
| 122 | + } |
| 123 | + |
| 124 | + input.close(); |
| 125 | + writer.close(); |
| 126 | + } catch (Exception e) { e.printStackTrace(); } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private List<String> reconstructAux(DEPTree tree, List<Chunk> chunks){ |
| 131 | + IntIntPair span; |
| 132 | + List<String> sentence = new ArrayList<>(); |
| 133 | + int i, j = 0, t_size = tree.size(), c_size = chunks.size(); |
| 134 | + |
| 135 | + List<String> entityToken; |
| 136 | + span = (j < c_size)? chunks.get(j).getChunkSpan() : null; |
| 137 | + for(i = 1; i < t_size; i++){ |
| 138 | + if(span != null && span.i1 == i){ |
| 139 | + entityToken = chunks.get(j).getStrippedChunkNodes().stream() |
| 140 | + .map(n -> n.getWordForm()).collect(Collectors.toList()); |
| 141 | + |
| 142 | + sentence.add(Joiner.join(entityToken, StringConst.EMPTY)); |
| 143 | + span = (j < c_size)? chunks.get(j).getChunkSpan() : null; |
| 144 | + i = span.i2; continue; |
| 145 | + } |
| 146 | + sentence.add(tree.get(i).getLemma()); |
| 147 | + } |
| 148 | + return sentence; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public AbstractCorpusRecontructor clone() { |
| 153 | + if(l_extensions != null && s_NERLables != null) |
| 154 | + return new EntityTokenCorpusReconstructor(type, inputDirPath, outputDirPath, new HashSet<String>(l_extensions), new HashSet<String>(s_NERLables)); |
| 155 | + else if(l_extensions == null) |
| 156 | + return new EntityTokenCorpusReconstructor(type, inputDirPath, outputDirPath, null, new HashSet<String>(s_NERLables)); |
| 157 | + else if(s_NERLables == null) |
| 158 | + return new EntityTokenCorpusReconstructor(type, inputDirPath, outputDirPath, new HashSet<String>(l_extensions), null); |
| 159 | + else |
| 160 | + return new EntityTokenCorpusReconstructor(type, inputDirPath, outputDirPath, null, null); |
| 161 | + } |
| 162 | +} |
0 commit comments