6
* Copyright (C) 2010-2011 Université Paris-Est Marne-la-Vallée
8
* This library is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2.1 of the License, or (at your option) any later version.
13
* This library is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this library; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24
package fr.upemlv.lgtools.parsing.mwe;
27
import java.io.FileNotFoundException;
28
import java.io.FilenameFilter;
29
import java.io.UnsupportedEncodingException;
30
import java.util.HashMap;
31
import java.util.List;
33
import fr.upemlv.lgtools.parsing.ParseTree;
34
import fr.upemlv.lgtools.parsing.Treebank;
38
* @author Matthieu Constant
42
public class GoldMweSubtreeGetter extends SubtreeGetter {
43
private final String dirPath;
44
private final HashMap<String, ParseTree> map = new HashMap<String, ParseTree>();
49
* @throws FileNotFoundException
50
* @throws UnsupportedEncodingException
52
public GoldMweSubtreeGetter(String dirPath) throws UnsupportedEncodingException, FileNotFoundException {
53
this.dirPath = dirPath;
57
private void fillMap() throws UnsupportedEncodingException, FileNotFoundException{
58
File dir = new File(this.dirPath);
59
if(!dir.isDirectory()){
60
throw new RuntimeException(dir + " should be a directory!");
62
for(String f:dir.list(new FilenameFilter() {
64
public boolean accept(File dir, String name) {
65
return name.endsWith(".mrg");
68
fillMap(new Treebank(dir.getAbsolutePath()+File.separator+f));
74
private void fillMap(Treebank treebank){
75
for(ParseTree tree:treebank){
76
ParseTree t = new ParseTree(tree.getTree().getChildren().get(0));
77
map.put(getItemKey(t.getTerminalSymbols(), t.getTree().getLabel()),t);
82
private static String getItemKey(List<String> symbols,String preterminal){
83
preterminal = preterminal.replaceAll("-", "");
84
//character - is a special symbol for a node label
85
return preterminal+"::"+symbols.toString();
89
public ParseTree get(List<String> item, String nodeLabel) {
91
ParseTree res = map.get(getItemKey(item, nodeLabel));
92
//System.err.println(getItemKey(item, nodeLabel));
94
throw new RuntimeException("Item "+getItemKey(item, nodeLabel)+" does not exist! You should use the force mode...");
96
return res.createCopy();