~loic-grobol/cteslo/trunk

« back to all changes in this revision

Viewing changes to cteslo/external_tools/lgtools/src/fr/upemlv/lgtools/parsing/mwe/GoldMweSubtreeGetter.java

  • Committer: Loïc Grobol
  • Date: 2012-09-11 20:48:16 UTC
  • Revision ID: loic.grobol@gmail.com-20120911204816-fvl8me0w3lsb0cnf
makefiles for lgtools

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * 
 
3
 *
 
4
 * Software: LGTools
 
5
 *
 
6
 * Copyright (C) 2010-2011 Université Paris-Est Marne-la-Vallée
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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.
 
21
 *
 
22
 */
 
23
 
 
24
package fr.upemlv.lgtools.parsing.mwe;
 
25
 
 
26
import java.io.File;
 
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;
 
32
 
 
33
import fr.upemlv.lgtools.parsing.ParseTree;
 
34
import fr.upemlv.lgtools.parsing.Treebank;
 
35
 
 
36
/**
 
37
 * 
 
38
 * @author Matthieu Constant
 
39
 *
 
40
 */
 
41
 
 
42
public class GoldMweSubtreeGetter extends SubtreeGetter {
 
43
    private final String dirPath;
 
44
    private final HashMap<String, ParseTree> map = new HashMap<String, ParseTree>();
 
45
    
 
46
        
 
47
        /**
 
48
         * @param dirPath
 
49
         * @throws FileNotFoundException 
 
50
         * @throws UnsupportedEncodingException 
 
51
         */
 
52
        public GoldMweSubtreeGetter(String dirPath) throws UnsupportedEncodingException, FileNotFoundException {
 
53
                this.dirPath = dirPath;
 
54
                fillMap();
 
55
        }
 
56
 
 
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!");
 
61
                }
 
62
                for(String f:dir.list(new FilenameFilter() {                    
 
63
                        @Override
 
64
                        public boolean accept(File dir, String name) {
 
65
                                return name.endsWith(".mrg");
 
66
                        }
 
67
                })){
 
68
                        fillMap(new Treebank(dir.getAbsolutePath()+File.separator+f));
 
69
                        
 
70
                }               
 
71
        }
 
72
 
 
73
 
 
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);
 
78
                }
 
79
        }
 
80
        
 
81
 
 
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();  
 
86
        }
 
87
        
 
88
        @Override
 
89
        public ParseTree get(List<String> item, String nodeLabel) {
 
90
 
 
91
                ParseTree res = map.get(getItemKey(item, nodeLabel));
 
92
                //System.err.println(getItemKey(item, nodeLabel));
 
93
                if(res == null){
 
94
                        throw new RuntimeException("Item "+getItemKey(item, nodeLabel)+" does not exist! You should use the force mode...");
 
95
                }               
 
96
                return res.createCopy();
 
97
        }
 
98
 
 
99
}