summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/phrase/PhraseCorpus.java
blob: 903e47c8376bbf4caff9b167b24db4927273b74e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package phrase;

import io.FileUtil;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class PhraseCorpus 
{
	public HashMap<String,Integer>wordLex;
	public HashMap<String,Integer>phraseLex;
	
	public String wordList[];
	public String phraseList[];
	
	//data[phrase][num context][position]
	public int data[][][];
	public int numContexts;	

	public PhraseCorpus(String filename) throws FileNotFoundException, IOException
	{
		BufferedReader r = FileUtil.reader(new File(filename));
		
		phraseLex=new HashMap<String,Integer>();
		wordLex=new HashMap<String,Integer>();
		
		ArrayList<int[][]>dataList=new ArrayList<int[][]>();
		String line=null;
		numContexts = 0;
		
		while((line=readLine(r))!=null){
			
			String toks[]=line.split("\t");
			String phrase=toks[0];
			addLex(phrase,phraseLex);
			
			toks=toks[1].split(" \\|\\|\\| ");
			
			ArrayList <int[]>ctxList=new ArrayList<int[]>();
			
			for(int i=0;i<toks.length;i+=2){
				String ctx=toks[i];
				String words[]=ctx.split(" ");
				if (numContexts == 0)
					numContexts = words.length - 1;
				else
					assert numContexts == words.length - 1;
				
				int []context=new int [numContexts+1];
				int idx=0;
				for(String word:words){
					if(word.equals("<PHRASE>")){
						continue;
					}
					addLex(word,wordLex);
					context[idx]=wordLex.get(word);
					idx++;
				}
				
				String count=toks[i+1];
				context[idx]=Integer.parseInt(count.trim().substring(2));
				
				ctxList.add(context);
			}
			
			dataList.add(ctxList.toArray(new int [0][]));
			
		}
		try{
		r.close();
		}catch(IOException ioe){
			ioe.printStackTrace();
		}
		data=dataList.toArray(new int[0][][]);
	}

	private void addLex(String key, HashMap<String,Integer>lex){
		Integer i=lex.get(key);
		if(i==null){
			lex.put(key, lex.size());
		}
	}
	
	//for debugging
	public void saveLex(String lexFilename) throws FileNotFoundException, IOException
	{
		PrintStream ps = FileUtil.printstream(new File(lexFilename));
		ps.println("Phrase Lexicon");
		ps.println(phraseLex.size());
		printDict(phraseLex,ps);
		
		ps.println("Word Lexicon");
		ps.println(wordLex.size());
		printDict(wordLex,ps);
		ps.close();
	}
	
	private static void printDict(HashMap<String,Integer>lex,PrintStream ps){
		String []dict=buildList(lex);
		for(int i=0;i<dict.length;i++){
			ps.println(dict[i]);
		}
	}
	
	public void loadLex(String lexFilename){
		Scanner sc=io.FileUtil.openInFile(lexFilename);
		
		sc.nextLine();
		int size=sc.nextInt();
		sc.nextLine();
		String[]dict=new String[size];
		for(int i=0;i<size;i++){
			dict[i]=sc.nextLine();
		}
		phraseLex=buildMap(dict);

		sc.nextLine();
		size=sc.nextInt();
		sc.nextLine();
		dict=new String[size];
		for(int i=0;i<size;i++){
			dict[i]=sc.nextLine();
		}
		wordLex=buildMap(dict);
		sc.close();
	}
	
	private HashMap<String, Integer> buildMap(String[]dict){
		HashMap<String,Integer> map=new HashMap<String,Integer>();
		for(int i=0;i<dict.length;i++){
			map.put(dict[i], i);
		}
		return map;
	}
	
	public void buildList(){
		if(wordList==null){
			wordList=buildList(wordLex);
			phraseList=buildList(phraseLex);
		}
	}
	
	private static String[]buildList(HashMap<String,Integer>lex){
		String dict[]=new String [lex.size()];
		for(String key:lex.keySet()){
			dict[lex.get(key)]=key;
		}
		return dict;
	}
	
	public String getContextString(int context[], boolean addPhraseMarker)
	{
		StringBuffer b = new StringBuffer();
		for (int i=0;i<context.length-1;i++)
		{
			if (b.length() > 0)
				b.append(" ");

			if (i == context.length/2)
				b.append("<PHRASE> ");
			
			b.append(wordList[context[i]]);
		}
		return b.toString();
	}
	
	public static String readLine(BufferedReader r){
		try{
			return r.readLine();
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws Exception 
	{
		String LEX_FILENAME="../pdata/lex.out";
		String DATA_FILENAME="../pdata/btec.con";
		PhraseCorpus c=new PhraseCorpus(DATA_FILENAME);
		c.saveLex(LEX_FILENAME);
		c.loadLex(LEX_FILENAME);
		c.saveLex(LEX_FILENAME);
	}
}