-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathTextMiner.java
More file actions
223 lines (219 loc) · 7.24 KB
/
TextMiner.java
File metadata and controls
223 lines (219 loc) · 7.24 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* MIT License
*
* Copyright (c) 2005-2019 by Anton Kolonin, Aigents®
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.webstructor.data;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import net.webstructor.al.AL;
import net.webstructor.al.Set;
import net.webstructor.al.Any;
import net.webstructor.al.All;
import net.webstructor.core.Environment;
public class TextMiner extends Miner {
private Map documentFeatures = null;
private Map categoryFeatures = null;
private Map categoryDocuments = null;
long tillTime = 0;
public TextMiner(Environment env, LangPack languages, boolean debug) {
this(env, languages, 0, debug);
}
public TextMiner(Environment env, LangPack languages, long tillTime, boolean debug) {
super(env, languages, debug);
this.tillTime = tillTime;
}
/*public TextMiner setDocuments(String[] documents, String[][] exclusions){
setExclusions(exclusions);
documentFeatures = toGraph(documents,documents);
return this;
}*/
public TextMiner setDocuments(String[] documents){
documentFeatures = toGraph(documents,documents);
return this;
}
public TextMiner setDocuments(Object[] documents, String[] texts){
documentFeatures = toGraph(documents,texts);
return this;
}
public TextMiner setDocuments(String[] documents,java.util.Set words){
documentFeatures = toGraph(documents,documents,words);
return this;
}
public TextMiner setDocuments(Object[] names, String[] documents,java.util.Set vocabulary){
documentFeatures = toGraph(names,documents,vocabulary);
return this;
}
/*private void setExclusions(String[][] exclusions){
this.exclusions = new HashSet();
if (!AL.empty(exclusions)){
for (int i = 0; i < exclusions.length; i++)
if (!AL.empty(exclusions[i]))
for (int j = 0; j < exclusions[i].length; j++)
if (!AL.empty(exclusions[i][j]))
this.exclusions.add(exclusions[i][j]);
}
}*/
public TextMiner cluster(int similarityThreshold,int featureVolume,int maxCategories) {
Map[] out = cluster(documentFeatures,similarityThreshold,featureVolume,maxCategories,tillTime);
categoryDocuments = out[0];
categoryFeatures = out[1];
return this;
}
public TextMiner cluster() {
Map[] out = cluster(documentFeatures,25,50,7,tillTime);
categoryDocuments = out[0];
categoryFeatures = out[1];
return this;
}
public Graph getGraph(String doccat) {
Graph g = new Graph();
for (Object cat : categoryDocuments.keySet()) {
Linker documents = (Linker)categoryDocuments.get(cat);
for (Object doc : documents.keys()) {
Number v = documents.value(doc);
if (doccat != null)
g.addValue(doc, cat, doccat, v.intValue());
}
}
return g;
}
public Map<Object,Linker> getDocumentCatNames(){
Map<Object,Linker> doccats = new HashMap<Object,Linker>();
Map catdocs = getCategoryDocuments();
for (Object cat : catdocs.keySet()) {
Linker docs_by_cat = (Linker)catdocs.get(cat);
String cat_name = cat.toString();
for (Object doc : docs_by_cat.keys()) {
Linker l = doccats.get(doc);
if (l == null)
doccats.put(doc, l = new Counter());
l.count(cat_name);
}
}
return doccats;
}
public Map getCategoryDocuments(){
return categoryDocuments;
}
public Map getCategoryFatures(){
return categoryFeatures;
}
public java.util.Set getCategoryDocuments(Object category){
return ((Linker)categoryDocuments.get(category)).keys();
}
public java.util.Set getCategoryFatures(Object category){
return ((Linker)categoryFeatures.get(category)).keys();
}
public java.util.Set getCategories(){
return categoryFeatures.keySet();
}
public Set getCategoryNames(){
ArrayList names = new ArrayList();
java.util.Set categories = categoryFeatures.keySet();
for (Iterator it = categories.iterator(); it.hasNext();){
Object category = it.next();
names.add(new Any(((OrderedStringSet)category).toArray()));
}
Any[] sets = (Any[])names.toArray(new Any[]{});
Arrays.sort(sets);
return new All(sets);
}
public final Linker getCategoryCounts(){
java.util.Set cats = getCategories();
if (AL.empty(cats))
return null;
Counter c = new Counter();
for (Object cat : cats){
java.util.Set docs = getCategoryDocuments(cat);
c.count(cat, docs.size());
}
return c;
}
//TODO: create patterns;
//not working yet becase features in clustert name are not "flat"!!!???
public TextMiner patternize(){
//for each of categories
//remove all its members that area lso members of other categories
//replace them in all graphs
Map newCategoryFatures = new HashMap();
for (Iterator it = categoryFeatures.keySet().iterator(); it.hasNext();){
Object cat = it.next();
Linker features = (Linker)categoryFeatures.get(cat);
Linker documents = (Linker)categoryDocuments.get(cat);
if (cat instanceof String){
newCategoryFatures.put(cat,features);//don't process single strings
} else
if (cat instanceof java.util.Set){
java.util.Set catSet = (java.util.Set)cat;
java.util.Set newCatSet = new HashSet(catSet);//clone
boolean processed = false;
for (Iterator jit = categoryFeatures.keySet().iterator(); jit.hasNext();){
Object otherCat = jit.next();
if (!otherCat.equals(cat)){
if (otherCat instanceof String){
newCatSet.remove((String)otherCat);
processed = true;
}else
if (otherCat instanceof java.util.Set){
newCatSet.removeAll((java.util.Set)otherCat);
processed = true;
}else{
env.debug("Wrong category identity");
processed = false;
break;
}
}
}
//TODO:check duplication
if (processed){
if (newCatSet.isEmpty()){
env.debug("Empty category identity");
processed = false;
}
else
if (newCategoryFatures.containsKey(newCatSet)){
env.debug("Duplicate category identity");
processed = false;
}
else{
newCategoryFatures.put(newCatSet,features);
documentFeatures.remove(cat);
documentFeatures.put(newCatSet, documents);
}
}
if (!processed){
newCategoryFatures.put(cat,features);//use existing
}
}
else {
env.debug("Wrong category identity");
newCategoryFatures.put(cat,features);
}
}
categoryFeatures = newCategoryFatures;
return this;
}
}