id stringlengths 36 36 | text stringlengths 1 1.25M |
|---|---|
aff250f9-5480-417e-97c6-a675255e87b7 | public Matrix copy () {
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j];
}
}
return X;
} |
7757810b-810a-4ead-9cbc-9df9ff6c9685 | public Object clone () {
return this.copy();
} |
a3416f1a-e62f-430c-bee1-d42ddbaa0c4c | public double[][] getArray () {
return A;
} |
dcc094e9-a951-46a0-8445-6e29d3ddbea0 | public double[][] getArrayCopy () {
double[][] C = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j];
}
}
return C;
} |
a82c53b8-ad43-4208-9285-01ac281f6453 | public double[] getColumnPackedCopy () {
double[] vals = new double[m*n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vals[i+j*m] = A[i][j];
}
}
return vals;
} |
7ca068de-2837-4e78-9d34-f025c840d2e3 | public double[] getRowPackedCopy () {
double[] vals = new double[m*n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
vals[i*n+j] = A[i][j];
}
}
return vals;
} |
71e7c533-9a8b-4d3f-96bb-468228945f3a | public int getRowDimension () {
return m;
} |
96e27ec8-8a1f-4741-8608-5f7f92599e7f | public int getColumnDimension () {
return n;
} |
6b4f92fd-05dc-4b98-90eb-d8654e0e8369 | public double get (int i, int j) {
return A[i][j];
} |
20d029d5-d947-4676-b6a2-b4e24d636e2e | public Matrix getMatrix (int i0, int i1, int j0, int j1) {
Matrix X = new Matrix(i1-i0+1,j1-j0+1);
double[][] B = X.getArray();
try {
for (int i = i0; i <= i1; i++) {
for (int j = j0; j <= j1; j++) {
B[i-i0][j-j0] = A[i][j];
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
return X;
} |
696b8cd0-62dd-4b26-b52c-515c2ea92abd | public Matrix getMatrix (int[] r, int[] c) {
Matrix X = new Matrix(r.length,c.length);
double[][] B = X.getArray();
try {
for (int i = 0; i < r.length; i++) {
for (int j = 0; j < c.length; j++) {
B[i][j] = A[r[i]][c[j]];
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
return X;
} |
ef1021d7-630c-4b09-ab1e-553fb0277bd0 | public Matrix getMatrix (int i0, int i1, int[] c) {
Matrix X = new Matrix(i1-i0+1,c.length);
double[][] B = X.getArray();
try {
for (int i = i0; i <= i1; i++) {
for (int j = 0; j < c.length; j++) {
B[i-i0][j] = A[i][c[j]];
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
return X;
} |
fe0006ea-a712-4406-a56a-d5bdbf31bf7a | public Matrix getMatrix (int[] r, int j0, int j1) {
Matrix X = new Matrix(r.length,j1-j0+1);
double[][] B = X.getArray();
try {
for (int i = 0; i < r.length; i++) {
for (int j = j0; j <= j1; j++) {
B[i][j-j0] = A[r[i]][j];
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
return X;
} |
67898ab4-3387-4c2b-81ef-7da3cbedf473 | public void set (int i, int j, double s) {
A[i][j] = s;
} |
f44ce9eb-77ca-4676-bbe8-73f91fa0b3fb | public void setMatrix (int i0, int i1, int j0, int j1, Matrix X) {
try {
for (int i = i0; i <= i1; i++) {
for (int j = j0; j <= j1; j++) {
A[i][j] = X.get(i-i0,j-j0);
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
} |
15c6905d-0cc9-45fb-b582-7aee0b15ba41 | public void setMatrix (int[] r, int[] c, Matrix X) {
try {
for (int i = 0; i < r.length; i++) {
for (int j = 0; j < c.length; j++) {
A[r[i]][c[j]] = X.get(i,j);
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
} |
0cd2da39-9c43-49cc-8ea9-846bf09283d1 | public void setMatrix (int[] r, int j0, int j1, Matrix X) {
try {
for (int i = 0; i < r.length; i++) {
for (int j = j0; j <= j1; j++) {
A[r[i]][j] = X.get(i,j-j0);
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
} |
bf1939eb-5172-4dfd-aad7-bb4e94850165 | public void setMatrix (int i0, int i1, int[] c, Matrix X) {
try {
for (int i = i0; i <= i1; i++) {
for (int j = 0; j < c.length; j++) {
A[i][c[j]] = X.get(i-i0,j);
}
}
} catch(ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Submatrix indices");
}
} |
29e559e9-01bb-4782-8102-f55b84f35d2c | public Matrix transpose () {
Matrix X = new Matrix(n,m);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[j][i] = A[i][j];
}
}
return X;
} |
31b2b0c2-fbb2-4f8c-a4b4-cdf0292d6038 | public double norm1 () {
double f = 0;
for (int j = 0; j < n; j++) {
double s = 0;
for (int i = 0; i < m; i++) {
s += Math.abs(A[i][j]);
}
f = Math.max(f,s);
}
return f;
} |
680b4194-1a30-442d-91e0-1f34af418fd8 | public double norm2 () {
return (new SingularValueDecomposition(this).norm2());
} |
df6c319b-acdf-4f28-97a3-b55a8deea6c2 | public double normInf () {
double f = 0;
for (int i = 0; i < m; i++) {
double s = 0;
for (int j = 0; j < n; j++) {
s += Math.abs(A[i][j]);
}
f = Math.max(f,s);
}
return f;
} |
56a7ec6e-5d03-420c-87b3-d097b9463bb3 | public double normF () {
double f = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
f = Maths.hypot(f,A[i][j]);
}
}
return f;
} |
0bd7d473-6f20-4b5d-9701-f311bb4dd4b3 | public Matrix uminus () {
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = -A[i][j];
}
}
return X;
} |
9e3c2ff6-f922-4bc2-be94-4a5dfd29178e | public Matrix plus (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B.A[i][j];
}
}
return X;
} |
bd68401a-c9e8-468a-b67e-763c0cd22586 | public Matrix plusEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] + B.A[i][j];
}
}
return this;
} |
aa6a2972-35eb-4463-9d52-1c898cae0e03 | public Matrix minus (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] - B.A[i][j];
}
}
return X;
} |
e8f63ab3-5c0e-479b-a657-7ef5947771f3 | public Matrix minusEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] - B.A[i][j];
}
}
return this;
} |
3a936877-a6b9-4a3c-bb7a-2d2b91a9ceba | public Matrix arrayTimes (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] * B.A[i][j];
}
}
return X;
} |
38c7feab-f52d-4570-b6b3-c2cfcd5b695e | public Matrix arrayTimesEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] * B.A[i][j];
}
}
return this;
} |
6c3e4e39-c3aa-4964-9f71-2aa2f1333ff4 | public Matrix arrayRightDivide (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] / B.A[i][j];
}
}
return X;
} |
5c230c4c-a34d-4607-bd14-cbc30c9038e5 | public Matrix arrayRightDivideEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] / B.A[i][j];
}
}
return this;
} |
d9361c83-736c-4bc9-a322-d44bda05c7f3 | public Matrix arrayLeftDivide (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = B.A[i][j] / A[i][j];
}
}
return X;
} |
d870ec2e-49d2-4405-89cf-3f26c046dd6a | public Matrix arrayLeftDivideEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = B.A[i][j] / A[i][j];
}
}
return this;
} |
2954c77a-ce79-4ee2-a6ef-c4cdd70011e0 | public Matrix times (double s) {
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = s*A[i][j];
}
}
return X;
} |
9f1606ea-a006-424d-9c58-3b0a0055cd22 | public Matrix timesEquals (double s) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = s*A[i][j];
}
}
return this;
} |
a01cd6a2-4a5f-4835-ad62-c0d0d915da3e | public Matrix times (Matrix B) {
if (B.m != n) {
throw new IllegalArgumentException("Matrix inner dimensions must agree.");
}
Matrix X = new Matrix(m,B.n);
double[][] C = X.getArray();
double[] Bcolj = new double[n];
for (int j = 0; j < B.n; j++) {
for (int k = 0; k < n; k++) {
Bcolj[k] = B.A[k][j];
}
for (int i = 0; i < m; i++) {
double[] Arowi = A[i];
double s = 0;
for (int k = 0; k < n; k++) {
s += Arowi[k]*Bcolj[k];
}
C[i][j] = s;
}
}
return X;
} |
ce3f4e20-807c-44da-9ef7-d95eeba577c2 | public LUDecomposition lu () {
return new LUDecomposition(this);
} |
a00ed10a-02d3-42ae-9f00-e605c8602bb8 | public QRDecomposition qr () {
return new QRDecomposition(this);
} |
12c210ce-ca6c-49b6-9cba-abe10fea7a2b | public CholeskyDecomposition chol () {
return new CholeskyDecomposition(this);
} |
21dbedf9-2981-4d4c-865c-ead5e0229e5c | public SingularValueDecomposition svd () {
return new SingularValueDecomposition(this);
} |
22fab20d-f211-47ae-87bf-1bd70259fcf2 | public EigenvalueDecomposition eig () {
return new EigenvalueDecomposition(this);
} |
cb00eaff-91d5-46f3-9daf-e471f9137cca | public Matrix solve (Matrix B) {
return (m == n ? (new LUDecomposition(this)).solve(B) :
(new QRDecomposition(this)).solve(B));
} |
b720120a-c43d-4522-acf8-05ed9cd8a5e1 | public Matrix solveTranspose (Matrix B) {
return transpose().solve(B.transpose());
} |
fa50fcce-aaf7-4033-94ba-410c574b4fd8 | public Matrix inverse () {
return solve(identity(m,m));
} |
e9448480-a00a-45a2-8c94-5defeae9ba37 | public double det () {
return new LUDecomposition(this).det();
} |
62e26de1-bd0c-4351-ba8a-a5a04971fa41 | public int rank () {
return new SingularValueDecomposition(this).rank();
} |
f560056f-2915-4a31-af7f-e662dbe54388 | public double cond () {
return new SingularValueDecomposition(this).cond();
} |
8da5417a-52bb-4f5d-974c-66bfb81d834b | public double trace () {
double t = 0;
for (int i = 0; i < Math.min(m,n); i++) {
t += A[i][i];
}
return t;
} |
fbeca4c2-864a-4fbe-aecd-42e334bbd254 | public static Matrix random (int m, int n) {
Matrix A = new Matrix(m,n);
double[][] X = A.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
X[i][j] = Math.random();
}
}
return A;
} |
319a3f65-d569-4de2-9c0c-143c798cb2a7 | public static Matrix identity (int m, int n) {
Matrix A = new Matrix(m,n);
double[][] X = A.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
X[i][j] = (i == j ? 1.0 : 0.0);
}
}
return A;
} |
658b54a2-b102-4a2e-a04e-343ebf73098f | public void print (int w, int d) {
print(new PrintWriter(System.out,true),w,d); } |
c479c844-164e-447f-9bfc-e2041ea4c498 | public void print (PrintWriter output, int w, int d) {
DecimalFormat format = new DecimalFormat();
format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US));
format.setMinimumIntegerDigits(1);
format.setMaximumFractionDigits(d);
format.setMinimumFractionDigits(d);
format.setGroupingUsed(false);
print(output,format,w+2);
} |
ca8bce47-5ca8-4912-916f-21ad039fb543 | public void print (NumberFormat format, int width) {
print(new PrintWriter(System.out,true),format,width); } |
9b9c9818-1ff7-4ae9-b6f9-385c3b1a6095 | public void print (PrintWriter output, NumberFormat format, int width) {
output.println(); // start on new line.
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
String s = format.format(A[i][j]); // format the number
int padding = Math.max(1,width-s.length()); // At _least_ 1 space
for (int k = 0; k < padding; k++)
output.print(' ');
output.print(s);
}
output.println();
}
output.println(); // end with blank line.
} |
6993f7de-81cb-4dc0-b479-60654737a358 | public static Matrix read (BufferedReader input) throws java.io.IOException {
StreamTokenizer tokenizer= new StreamTokenizer(input);
// Although StreamTokenizer will parse numbers, it doesn't recognize
// scientific notation (E or D); however, Double.valueOf does.
// The strategy here is to disable StreamTokenizer's number parsing.
// We'll only get whitespace delimited words, EOL's and EOF's.
// These words should all be numbers, for Double.valueOf to parse.
tokenizer.resetSyntax();
tokenizer.wordChars(0,255);
tokenizer.whitespaceChars(0, ' ');
tokenizer.eolIsSignificant(true);
java.util.Vector v = new java.util.Vector();
// Ignore initial empty lines
while (tokenizer.nextToken() == StreamTokenizer.TT_EOL);
if (tokenizer.ttype == StreamTokenizer.TT_EOF)
throw new java.io.IOException("Unexpected EOF on matrix read.");
do {
v.addElement(Double.valueOf(tokenizer.sval)); // Read & store 1st row.
} while (tokenizer.nextToken() == StreamTokenizer.TT_WORD);
int n = v.size(); // Now we've got the number of columns!
double row[] = new double[n];
for (int j=0; j<n; j++) // extract the elements of the 1st row.
row[j]=((Double)v.elementAt(j)).doubleValue();
v.removeAllElements();
v.addElement(row); // Start storing rows instead of columns.
while (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
// While non-empty lines
v.addElement(row = new double[n]);
int j = 0;
do {
if (j >= n) throw new java.io.IOException
("Row " + v.size() + " is too long.");
row[j++] = Double.valueOf(tokenizer.sval).doubleValue();
} while (tokenizer.nextToken() == StreamTokenizer.TT_WORD);
if (j < n) throw new java.io.IOException
("Row " + v.size() + " is too short.");
}
int m = v.size(); // Now we've got the number of rows.
double[][] A = new double[m][];
v.copyInto(A); // copy the rows out of the vector
return new Matrix(A);
} |
79b355d7-c072-4977-8056-20bfee9f911d | private void checkMatrixDimensions (Matrix B) {
if (B.m != m || B.n != n) {
throw new IllegalArgumentException("Matrix dimensions must agree.");
}
} |
efd73149-0c29-42e6-a09d-1bfbd9dc2e36 | public void reduce(IntWritable key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
ArrayList<String> list = new ArrayList<String>();
for (Text val : values) {
list.add(val.toString());
context.write(val, null);
}
int coloumnSize = list.size();
int rowSize = list.get(0).split("\t").length-1;
if (coloumnSize == 0) {
System.out.println("Cannot perform LSI because '0' elements in this Cluster");
}
/* Need to look into this condition*/
else if (rowSize <= coloumnSize) {
System.out.println("rowSize =" + rowSize);
System.out.println("coloumnSize =" + coloumnSize);
System.out.println("Cannot perform LSI because rowsize < coloumnsize in this Cluster");
}
else {
double [][] arr = new double[rowSize][coloumnSize];
String[] Ids = new String[coloumnSize];
Iterator<String> ListIter = list.iterator();
int countRow = 0;
int countCol = 0;
while (ListIter.hasNext()) {
String tmp = ListIter.next();
StringTokenizer itr = new StringTokenizer(tmp);
Ids[countCol] = itr.nextToken("\t");
while (itr.hasMoreTokens()) {
arr[countRow][countCol] = Double.parseDouble(itr.nextToken("\t"));
//context.write(word, one);
countRow++;
} countRow=0;
countCol++;
}
Matrix A = new Matrix(arr);
SingularValueDecomposition SVD = new SingularValueDecomposition(A);
Matrix S = SVD.getS();
Matrix U = SVD.getU();
Matrix V = SVD.getV();
Configuration conf = context.getConfiguration();
int reducedRank = Integer.parseInt(conf.get("ReducedRank"));
int rank= S.rank();
int toreduce = 40; //default no specific reason why I gave 40
if((6*rank)/10 < reducedRank ) {
toreduce = (6*rank)/10;
}
else {
toreduce = reducedRank;
}
//Reducing rank
S = S.getMatrix(0, toreduce-1, 0, toreduce-1);
U = U.getMatrix(0, U.getRowDimension()-1, 0, toreduce-1);
V = V.getMatrix(0, V.getRowDimension()-1, 0, toreduce-1);
A = U.times(S);
A = A.times(V.transpose());
//Convert back to array to print
arr = A.getArray();
String val = "";
for (int i=0;i<coloumnSize;i++) {
val = Ids[i]+" ";
for (int j=0;j<rowSize;j++) {
val += Double.toString(arr[j][i]) + " ";
}
context.write(new Text(val), null);
}
}
} |
d8977af3-7f08-450a-84cb-cd9b27ec4df6 | public void setup(Context context) {
Configuration conf = context.getConfiguration();
try {
FileSystem fs = FileSystem.get(conf);
Path infile = new Path(conf.get("CentroidFile")); //give it as an argument
if (!fs.exists(infile))
System.out.println("Centroids.txt does not exist");
if (!fs.isFile(infile))
System.out.println("Centroids.txt is not a file");
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(infile)));
String str = conf.get("NoOfClusters");
int K = Integer.parseInt(str);
for (int i=0; i<K;i++)
{
String centroidData = br.readLine();
Centroids.put(i, centroidData);
System.out.println(centroidData);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Exception in Creating the FileSystem object in Mapper");
e.printStackTrace();
}
} |
72e534ce-1e73-46aa-8a5c-e8a0d02421a6 | private double EucledianDistance(String s1, String s2) {
StringTokenizer itr1 = new StringTokenizer(s1);
StringTokenizer itr2 = new StringTokenizer(s2);
String id1 = itr1.nextToken("\t");//Do not remove (needed to remove the first id element)
String id2 = itr2.nextToken("\t");//Do not remove (needed to remove the first id element)
Double sum = 0.0;
Double Diff = 0.0;
while (itr1.hasMoreTokens() && itr2.hasMoreTokens()) {
Diff = (Double.parseDouble(itr1.nextToken("\t")) - Double.parseDouble(itr2.nextToken("\t")));
sum = sum + Math.pow(Diff, 2);
}
return Math.sqrt(sum);
} |
42b20e83-2597-4a6c-8cf2-46803e8fdafb | public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
int minCentroidID=0;
double dist, mindist = -1.0;
Iterator<Integer> keyIter = Centroids.keySet().iterator();
while (keyIter.hasNext()) {
Integer CentroidID = keyIter.next();
dist = EucledianDistance(Centroids.get(CentroidID), value.toString());
System.out.println("Distance between"+Centroids.get(CentroidID)+"AND"+value.toString()+"is"+ dist);
if (dist < mindist || mindist == -1.0) {
mindist = dist;
minCentroidID = CentroidID;
}
}
IntWritable k = new IntWritable(minCentroidID);
context.write(k, value);
} |
755e2853-88b2-44ed-812a-ac99fb9a13c9 | public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
IntWritable one = new IntWritable(1);
context.write(one, value);
} |
a153d1c0-cce8-4402-a03a-32d564a4ce84 | public void reduce(IntWritable key, Iterable<Text> values,
Context context
) throws IOException, InterruptedException {
for (Text val : values) {
context.write(val, null);
}
} |
b4a3337a-0598-4b99-8937-048611eac2a1 | public Map()
{
System.out.println("============CALLING THE CONSTRUCTOR==============");
} |
1433cc45-6a5b-427c-ad77-7bae85562d2d | public void setup(Context context) {
Configuration conf = context.getConfiguration();
docCount = termCount = 0;
int maxTermCount = Integer.parseInt(conf.get("TermCount"));
curDocId.set(-1);
tdMat = conf.get("MatType").trim();
/* Fetch information about documents from docIndex */
try {
System.out.println("----Making DocIndex-----");
FileSystem fs = FileSystem.get(conf);
String docIdx = conf.get("DocIndex");
Path infile = new Path(docIdx); /* DocIndex File*/
if (!fs.exists(infile))
System.out.println(docIdx+" does not exist");
if (!fs.isFile(infile))
System.out.println(docIdx+" is not a file");
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(infile)));
String curLine = "", curDoc = "";
while( (curLine = br.readLine()) != null)
{
curDoc = curLine.trim();
if(curDoc.length()>0)
{
String tokens[] = curDoc.split("/");
String curFile = tokens[tokens.length-1];
docIndex.put(curFile, docCount);
System.out.println(docCount+" "+curFile);
docCount++;
}
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Exception in Creating the FileSystem object in Mapper");
e.printStackTrace();
}
System.out.println("Initiaing TermDocIndexer with "+docCount+" documents using "+termCount+" terms");
/* Fetch information about terms from termIndex */
System.out.println("Trying to make termIndex");
try {
FileSystem fs = FileSystem.get(conf);
String termIdx = conf.get("TermIndex");
Path infile = new Path(termIdx); /* TermIndex File*/
if (!fs.exists(infile))
System.out.println(termIdx+" does not exist");
if (!fs.isFile(infile))
System.out.println(termIdx+" is not a file");
System.out.println("----Making termIndex-----");
BufferedReader br=new BufferedReader(new InputStreamReader(fs.open(infile)));
String curLine = "", curTerm = "";
int curFrq = 0;
while( (curLine = br.readLine()) != null && termCount < maxTermCount )
{
String tokens[] = curLine.split("\t");
//System.out.println(curLine+" "+tokens.length);
if(tokens.length>=2)
{
curTerm = tokens[0];
curFrq = Integer.parseInt(tokens[1]);
float dc = Float.parseFloat(tokens[2]);
float termRatio = dc/docCount;
double curIdf = Math.log(termRatio);
if(dc == 0) curIdf = -200;
System.out.println(" IDF for "+curTerm+" is "+curIdf+" made from "+termRatio+" on "+dc+"/"+docCount);
idf.put(curTerm, curIdf);
termFrq.put(curTerm, curFrq);
termIndex.put(curTerm , termCount);
System.out.println(termCount+" "+curTerm+" "+curFrq);
termCount++;
}
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Exception in Creating the FileSystem object in Mapper");
e.printStackTrace();
}
FileSplit fileSplit = (FileSplit)context.getInputSplit();
String inputFile = fileSplit.getPath().getName();
System.out.println("===========INPUT-FILE : "+inputFile+" =================");
if(docIndex.containsKey(inputFile)==true)
{
curDocId.set(docIndex.get(inputFile));
}
System.out.print("Imp!!! - Running on inputFile "+inputFile+" with docIndex "+curDocId.get());
//conf.set("TermCount", Integer.toString(termCount) );
conf.set("DocCount", Integer.toString(docCount) );
return;
} |
45d66fad-027f-4226-b851-1dabc0ea35e4 | public void map(LongWritable key, Text value, Context context) throws IOException {
String line = value.toString();
line = line.replaceAll("[^a-zA-Z0-9]+"," ");
line = line.toLowerCase();
IntWritable dummyDocId = new IntWritable();
dummyDocId.set(1);
Configuration conf = context.getConfiguration();
System.out.println("Working on TermDocIndexer with "+docCount+" documents using "+termCount+" terms");
System.out.println("Conf : Working on TermDocIndexer with "+conf.get("DocCount")+" documents using "+conf.get("TermCount")+" terms");
System.out.println("TermIndex has "+termIndex.size()+" elements and docIndex has "+docIndex.size()+" elements");
System.out.println("Line is : "+line);
StringTokenizer tokenizer = new StringTokenizer(line);
while (tokenizer.hasMoreTokens()) {
String curTerm = tokenizer.nextToken();
int termIdx = -1;
double termWt = 1.0;
if(termIndex.containsKey(curTerm) == true)
termIdx = termIndex.get(curTerm);
System.out.println("TD - Matrix type is "+tdMat);
if(tdMat.equals("TFIDF"))
{
if(idf.containsKey(curTerm) == true)
termWt = idf.get(curTerm);
System.out.println("Using TFIDF termWt "+termWt);
}
else if(tdMat.equals("TF"))
{
termWt = 1;
}
else {
termWt = 2;
}
String ans = termIdx+"\t"+termWt;
System.out.println("Emit :"+ curDocId.get()+" => "+ans);
Text ans1 = new Text(ans);
try {
context.write( curDocId , ans1 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Failed emitting from mapper");
e.printStackTrace();
}
}
} |
52502c12-d333-4aae-84d8-436228c25001 | public void reduce(IntWritable docId, Iterable<Text> termFrequencies,
Context context)
throws IOException {
/* Dont collect to reduce rather write straight-away to $OUTPUT_FOLDER/Index/TermIndex/ */
int[] finalTermFrq = new int[2];
boolean mtTypeIncdence = false;
Configuration conf = context.getConfiguration();
String termCountString = conf.get("TermCount");
int termCount = /*10000;
conf.setIfUnset("TermCount", "10000");
if(termCountString != null)
termCount =*/ Integer.parseInt(termCountString);
System.out.println("Reducer Conf : Working on TermDocIndexer with "+conf.get("DocCount")+" documents using "+conf.get("TermCount")+" terms");
float[] docVector = new float[termCount];
int termFreq = 0;
for (Text termInformation : termFrequencies)
{
String termInfo[] = termInformation.toString().split("\t");
System.out.println("Input is : "+termInformation);
if(termInfo.length>=2)
{
int curTermIdx = Integer.parseInt(termInfo[0]);
float curTermWt = Float.parseFloat(termInfo[1]);
if(curTermWt == 2)
mtTypeIncdence = true;
if(curTermIdx<termCount && curTermIdx>=0)
docVector[curTermIdx] += curTermWt;
else {
System.out.println("ERROR !! - TermIdx "+curTermIdx+" with frequency "+curTermWt+" out of range");
}
}
}
String val = "";
for(int i=0;i<termCount;i++)
{
if(mtTypeIncdence==true)
{
if(docVector[i]!=0)
val+="1\t";
else val+="0\t";
}
else {
val+=docVector[i]+"\t";
}
}
try {
context.write(docId, new Text(val));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Failed emitting from reducer");
e.printStackTrace();
}
} |
9c414e29-233e-47ce-ba26-e59dfeb64563 | public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("TermIndex",args[0]+"/../Index/TermIndex.dat");
conf.set("DocIndex",args[0]+"/../Index/DocIndex.dat");
conf.set("TermCount", args[1]);
conf.set("MatType", args[2]);
//JobConf conf = new JobConf(TermDocIndexer.class);
Job job = new Job(conf, "TermDocIndexing Job");
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(Text.class);
job.setJarByClass(TermDocIndexer.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[0]+"/../TDMatrix/"));
job.waitForCompletion(true);
} |
57fb22db-8e8e-4da2-9a60-d9111c549da0 | public Parser() {
analyzer = new LexicalAnalyzer();
} |
8515b59c-05c7-4d14-be2a-98bea8e158bd | public Node parse(Reader src) throws ScriptException {
analyzer.load(src);
return parseExpression();
} |
9c79756d-11fa-42d7-8eec-6aab1a8cc6b4 | private Node parseExpression() throws ScriptException {
return parseAdditive();
} |
d3e81b06-bdd5-4e2c-9864-c721b4d46417 | private Node parseAdditive() throws ScriptException {
Node left = parseMultiplicative();
if (left == null)
return null;
Token token;
while ((token = analyzer.getNextToken()) != null) {
if (token.getType() != Operators.ADD
&& token.getType() != Operators.SUB) {
analyzer.ungetToken(token);
return left;
}
Node right = parseMultiplicative();
left = new Node(token.getType(), left, right);
}
return left;
} |
d76ccd68-0586-453c-94a4-181cdb71fc27 | private Node parseMultiplicative() throws ScriptException {
Node left = parsePower();
if (left == null)
return null;
Token token;
while ((token = analyzer.getNextToken()) != null) {
if (token.getType() != Operators.MUL
&& token.getType() != Operators.DIV
&& token.getType() != Operators.MOD) {
analyzer.ungetToken(token);
return left;
}
Node right = parsePower();
left = new Node(token.getType(), left, right);
}
return left;
} |
7d6224e4-9573-47d7-b6ee-50ce6d24d77b | private Node parsePower() throws ScriptException {
Node left = parseUnary();
if (left == null)
return null;
Token token;
while ((token = analyzer.getNextToken()) != null) {
if (token.getType() != Operators.POW) {
analyzer.ungetToken(token);
return left;
}
Node right = parsePower();
left = new Node(token.getType(), left, right);
}
return left;
} |
7dc99c50-46da-458f-a15c-4bc6ea424622 | private Node parseUnary() throws ScriptException {
Token token = analyzer.getNextToken();
if (token == null)
return null;
if (token.getType() == Operators.ADD) {
return parsePrimary();
} else if (token.getType() == Operators.SUB) {
Node node = parsePrimary();
return new Node(Operators.SUB, node, null);
} else {
analyzer.ungetToken(token);
return parsePrimary();
}
} |
95adb2ca-c0b6-43a4-b659-178bab8a68ac | private Node parsePrimary() throws ScriptException {
Token token = analyzer.getNextToken();
if (token == null)
return null;
if (token.getType() == Tokens.NUMBER) {
return new Node(token.toNumber());
} else if (token.getType() == Operators.OPEN_PARENS) {
Node node = parseExpression();
analyzer.checkNextToken(Operators.CLOSE_PARENS);
return node;
}
return null;
} |
e0073482-a399-4f46-93c5-abb458a7284e | public void load(Reader reader) throws ScriptException {
this.reader = new LineNumberReader(reader);
readNextLine();
} |
6dfa1b26-9750-496b-a1d1-e7b4ffbb15ce | private String readNextLine() throws ScriptException {
try {
while ((line = reader.readLine()) != null) {
if (line.length() != 0)
break;
}
index = 0;
return line;
} catch (IOException ex) {
throw new ScriptException(ex);
}
} |
fccff210-331a-4e93-953f-f83c811dce87 | private char charAt(int column) throws ScriptException {
if (column < line.length())
return line.charAt(column);
if (readNextLine() != null)
return line.charAt(0);
else
return '\0'; // ファイルの末尾に達した場合
} |
cc7d14d0-de72-49e8-b6ae-f3939e0088b9 | private boolean isOperator(Token token, char ch) {
String target = token + Character.toString(ch);
for (Operators oper : opers) {
if (oper.toString().indexOf(target) >= 0)
return true;
}
return false;
} |
5be961c3-fa67-41e9-98ba-cefe32ecce11 | private Enum<?> getOperator(Token token) {
for (Enum<?> oper : opers) {
if (token.equals(oper.toString()))
return oper;
}
return null;
} |
75eb0356-db04-4474-b40b-d5f302beae9a | private ScriptException error(char ch) {
index++;
return error("Character \'" + ch + "\' is illegal here.");
} |
e39731a2-dee4-4997-a863-60d55e02ee36 | private ScriptException error(Object msg) {
int line = reader.getLineNumber();
return new ScriptException(
msg + " at line : " + line +
"\n => " + getLine(), null, line, index + 1);
} |
1ad65b07-f9e4-4df2-95fb-75f01b337e14 | public Token getNextToken() throws ScriptException {
Token token = next;
if (next != null) {
next = null;
return token;
}
return read(new Token());
} |
715da5d5-f3be-4890-a00d-1c33d8a29e7c | public void ungetToken(Token token) {
this.next = token; // 最大で大きさ1のスタック
} |
4d3370d9-26c0-4c04-b323-fa016a844b7d | private Token read(Token token) throws ScriptException {
while (line != null) {
char ch = charAt(index);
index++;
if (Character.isDigit(ch))
return readInteger(token.append(ch));
if (ch == '\0')
return token;
if (isOperator(token, ch))
return readOperator(token.append(ch));
if (!Character.isWhitespace(ch))
throw error(ch);
if (index >= line.length())
readNextLine();
}
return null;
} |
3dc312b2-c6a1-4c18-805c-80acdc5eb6cf | private Token readInteger(Token token) throws ScriptException {
while (line != null) {
char ch = charAt(index);
index++;
if (Character.isDigit(ch))
token.append(ch);
else if (ch == '.' && Character.isDigit(charAt(index)))
return readNumber(token.append(ch));
else {
index--;
return token.setType(Tokens.NUMBER);
}
}
return null;
} |
0a912b01-683e-43bd-b921-9831e064ac06 | private Token readNumber(Token token) throws ScriptException {
while (line != null) {
char ch = charAt(index);
index++;
if (Character.isDigit(ch))
token.append(ch);
else {
index--;
return token.setType(Tokens.NUMBER);
}
}
return null;
} |
45dca1a2-4dcd-4176-92ee-cc0a977cca55 | private Token readOperator(Token token)
throws ScriptException {
while (line != null) {
char ch = charAt(index);
index++;
if (isOperator(token, ch)) {
token.append(ch);
} else {
index--;
return token.setType(getOperator(token));
}
}
return null;
} |
3d7bf10e-5571-4284-a6be-2fa0512f3480 | public Token checkNextToken(Enum<?>... expected)
throws ScriptException {
Token next = getNextToken();
if (next != null) {
for (Enum<?> exp : expected) {
if (next.getType() == exp)
return next;
}
}
throw error("\"" + next + "\" is not expected here.");
} |
f34c8905-d9b8-46d4-9551-290c7fbf15a8 | public String getLine() {
return (line != null) ? line.substring(0, index) : "";
} |
faf5af9c-6e8b-44ce-8dbf-d34219ddae74 | public int getLineNumber() {
return reader.getLineNumber();
} |
e9d5e497-e4fb-4336-8e35-9837770f1528 | public int getColumnNumber() {
return index + 1;
} |
57bf439f-b158-41c9-9c27-ac44e1d67b7e | public Node(Enum<?> op, Node left, Node right) {
this.op = (Operators) op;
this.left = left;
this.right = right;
} |
1215996a-7e6e-4dff-b29a-698bf72962a7 | public Node(BigDecimal value) {
this.op = null;
this.value = value;
} |
99cc0ecc-87e1-4582-8789-4f26e79bb98a | public BigDecimal value() {
if (op == null)
return value;
BigDecimal l = left.value();
BigDecimal r = (right != null) ? right.value() : null;
switch (op) {
case ADD:
return l.add(r);
case SUB:
return r != null ? l.subtract(r) : l.negate();
case MUL:
return l.multiply(r);
case DIV:
return l.divide
(r, 32, BigDecimal.ROUND_HALF_EVEN);
case MOD:
return l.remainder(r);
case POW:
return l.pow(r.intValue());
default:
// 呼び出しなし
}
return BigDecimal.ZERO;
} |
7355479e-efcd-4d29-9826-089917357b7a | public String toString() {
if (op == null)
return String.valueOf(value);
StringBuilder sb = new StringBuilder();
sb.append(left);
sb.append(" ");
sb.append(right);
sb.append(" ");
sb.append(op);
return new String(sb);
} |
35f691a7-4a4a-422e-a80f-af3ceded1381 | public Token() {
sb = new StringBuilder();
} |
397009df-75cf-481b-8b20-14d2928a290d | public Token append(char ch) {
sb.append(ch);
return this;
} |
facd4b11-4dfd-4562-8a5b-b9a321582c0e | public String toString() {
return sb.toString();
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.