Guest User

Untitled

a guest
Dec 7th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.73 KB | None | 0 0
  1. package kodms;
  2.  
  3. import java.io.IOException;
  4. import java.io.StringReader;
  5. import java.util.ArrayList;
  6. import java.util.HashSet;
  7.  
  8. import no.uib.cipr.matrix.DenseMatrix;
  9. import no.uib.cipr.matrix.QRP;
  10.  
  11. import org.apache.commons.math.linear.Array2DRowRealMatrix;
  12. import org.apache.commons.math.linear.RealMatrix;
  13. import org.apache.lucene.analysis.Analyzer;
  14. import org.apache.lucene.analysis.TokenStream;
  15. import org.apache.lucene.analysis.en.EnglishAnalyzer;
  16. import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
  17. import org.apache.lucene.util.AttributeSource;
  18. import org.apache.lucene.util.Version;
  19.  
  20. import cern.colt.matrix.tdouble.DoubleMatrix2D;
  21. import cern.colt.matrix.tdouble.algo.decomposition.DenseDoubleSingularValueDecomposition;
  22. import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
  23.  
  24. import magisterarbeit.SplitInSents;
  25.  
  26. public class AlgorithmFactory implements AlgorithmFactoryInterface {
  27.  
  28.     @Override
  29.     public AlgorithmInterface parser() {
  30.         return new Parser();
  31.     }
  32.  
  33.     public static class Parser implements AlgorithmInterface {
  34.         protected ArrayList<String> dic;
  35.         protected ArrayList<String> sentences;
  36.         protected String text;
  37.  
  38.         @Override
  39.         public void process() throws IOException {
  40.             this.dic = new ArrayList<String>();
  41.             this.sentences = new SplitInSents().split(text);
  42.  
  43.             Analyzer analyzer = new EnglishAnalyzer(Version.LUCENE_31);
  44.             TokenStream stream = analyzer.tokenStream("contents",
  45.                     new StringReader(text));
  46.  
  47.             while (stream.incrementToken()) {
  48.                 AttributeSource token = stream.cloneAttributes();
  49.                 CharTermAttribute term = (CharTermAttribute) token
  50.                         .addAttribute(CharTermAttribute.class);
  51.                 dic.add(term.toString());
  52.             }
  53.  
  54.             HashSet<String> hashSet = new HashSet<String>(dic);
  55.             dic.clear();
  56.             dic.addAll(hashSet);
  57.         }
  58.  
  59.         public Parser init(String text) throws IOException {
  60.             setText(text);
  61.             process();
  62.             return this;
  63.         }
  64.  
  65.         public void setText(String text) {
  66.             this.text = text;
  67.         }
  68.  
  69.         public ArrayList<String> getDic() {
  70.             return this.dic;
  71.         }
  72.  
  73.         public ArrayList<String> getSentences() {
  74.             return this.sentences;
  75.         }
  76.     }
  77.  
  78.     @Override
  79.     public AlgorithmInterface st() {
  80.         return new ST();
  81.     }
  82.  
  83.     public static class ST implements AlgorithmInterface {
  84.         protected ArrayList<String> dic;
  85.         protected ArrayList<String> sentences;
  86.         protected Matrix stmatrix;
  87.  
  88.         @Override
  89.         public void process() throws IOException {
  90.             this.stmatrix = new Matrix(new double[dic.size()][sentences.size()]);
  91.             Analyzer analyzer = new EnglishAnalyzer(Version.LUCENE_31);
  92.  
  93.             for (int i = 0; i < dic.size(); i++) {
  94.                 for (int j = 0; j < sentences.size(); j++) {
  95.                     TokenStream streaming = analyzer.tokenStream("contents",
  96.                             new StringReader(sentences.get(j)));
  97.                     int count = 0;
  98.                     while (streaming.incrementToken()) {
  99.                         AttributeSource token = streaming.cloneAttributes();
  100.                         CharTermAttribute term = (CharTermAttribute) token
  101.                                 .addAttribute(CharTermAttribute.class);
  102.                         if (term.toString().equalsIgnoreCase(dic.get(i)))
  103.                             count++;
  104.                         stmatrix.set(i, j, count);
  105.                     }
  106.  
  107.                 }
  108.  
  109.             }
  110.         }
  111.  
  112.         public ST init(ArrayList<String> dic, ArrayList<String> sentences)
  113.                 throws IOException {
  114.             setDic(dic);
  115.             setSentences(sentences);
  116.             process();
  117.             return this;
  118.         }
  119.  
  120.         public void setDic(ArrayList<String> dic) {
  121.             this.dic = dic;
  122.         }
  123.  
  124.         public void setSentences(ArrayList<String> sentences) {
  125.             this.sentences = sentences;
  126.         }
  127.  
  128.         public Matrix getSTMatrix() {
  129.             return stmatrix;
  130.         }
  131.     }
  132.  
  133.     @Override
  134.     public AlgorithmInterface a() {
  135.         return new A();
  136.     }
  137.  
  138.     public static class A implements AlgorithmInterface {
  139.         protected ArrayList<String> dic;
  140.         protected ArrayList<String> sentences;
  141.         protected MatrixInterface stmatrix;
  142.         protected Matrix a;
  143.  
  144.         @Override
  145.         public void process() {
  146.             double[] g = new double[dic.size()];
  147.             for (int i = 0; i < dic.size(); i++) {
  148.                 double chisum = 0;
  149.                 for (int j = 0; j < sentences.size(); j++) {
  150.                     chisum += Math.signum(stmatrix.get(i, j));
  151.  
  152.                 }
  153.                 g[i] = Math.log(sentences.size() / chisum);
  154.             }
  155.  
  156.             double[] d = new double[sentences.size()];
  157.             for (int j = 0; j < sentences.size(); j++) {
  158.                 double lgsum = 0;
  159.                 for (int i = 0; i < dic.size(); i++) {
  160.                     lgsum += g[i] * Math.log(1 + stmatrix.get(i, j)) * g[i]
  161.                             * Math.log(1 + stmatrix.get(i, j));
  162.                 }
  163.                 d[j] = 1 / Math.sqrt(lgsum);
  164.             }
  165.  
  166.             this.a = new Matrix(new double[dic.size()][sentences.size()]);
  167.  
  168.             for (int i = 0; i < dic.size(); i++) {
  169.                 for (int j = 0; j < sentences.size(); j++) {
  170.                     this.a.set(i, j, Math.log(1 + stmatrix.get(i, j)) * g[i]
  171.                             * d[j]);
  172.                 }
  173.             }
  174.         }
  175.  
  176.         public A init(ArrayList<String> dic, ArrayList<String> sentences,
  177.                 Matrix stmatrix) throws IOException {
  178.             setDic(dic);
  179.             setSentences(sentences);
  180.             process();
  181.             return this;
  182.         }
  183.  
  184.         public void setDic(ArrayList<String> dic) {
  185.             this.dic = dic;
  186.         }
  187.  
  188.         public void setSentences(ArrayList<String> sentences) {
  189.             this.sentences = sentences;
  190.         }
  191.  
  192.         public void setSTMatrix(MatrixInterface stmatrix) {
  193.             this.stmatrix = stmatrix;
  194.         }
  195.  
  196.         public Matrix getA() {
  197.             return a;
  198.         }
  199.     }
  200.  
  201.     @Override
  202.     public AlgorithmInterface svd() {
  203.         return new SVD();
  204.     }
  205.  
  206.     public static class SVD implements AlgorithmInterface {
  207.         protected MatrixInterface a;
  208.         protected ColtMatrix v, u, s;
  209.        
  210.         @Override
  211.         public void process() {
  212.             DoubleMatrix2D A1 = new DenseDoubleMatrix2D(a.toArray());
  213.             DenseDoubleSingularValueDecomposition svdcolt = new DenseDoubleSingularValueDecomposition(
  214.                     A1, true, true);
  215.             v = new ColtMatrix(svdcolt.getV());
  216.             u = new ColtMatrix(svdcolt.getU());
  217.             s = new ColtMatrix(svdcolt.getS());
  218.         }
  219.  
  220.         public SVD init(MatrixInterface a) {
  221.             setA(a);
  222.             process();
  223.             return this;
  224.         }
  225.  
  226.         public void setA(MatrixInterface a) {
  227.             this.a = a;
  228.         }
  229.  
  230.         public ColtMatrix getV() {
  231.             return v;
  232.         }
  233.  
  234.         public ColtMatrix getU() {
  235.             return u;
  236.         }
  237.  
  238.         public ColtMatrix getS() {
  239.             return s;
  240.         }
  241.     }
  242.  
  243.     @Override
  244.     public AlgorithmInterface qr() {
  245.         return new QR();
  246.     }
  247.  
  248.     public static class QR implements AlgorithmInterface {
  249.         protected ArrayList<String> sentences;
  250.         protected MatrixInterface v, u, s;
  251.         protected MtjMatrix tp;
  252.         protected int k;
  253.        
  254.         @Override
  255.         public void process() {
  256.             RealMatrix v1 = new Array2DRowRealMatrix(v.toArray());
  257.             RealMatrix s1 = new Array2DRowRealMatrix(s.toArray());
  258.            
  259.             RealMatrix vt = v1.transpose();
  260.  
  261.             RealMatrix sthin = s1.getSubMatrix(0, k-1, 0, k-1);
  262.             RealMatrix vtthin = vt.getSubMatrix(0, k-1, 0, vt.getColumnDimension()-1);
  263.            
  264.             RealMatrix dk = sthin.multiply(vtthin);
  265.            
  266.             double [][] fRow = new double [1][sentences.size()];
  267.            
  268.             for (int j = 0; j < sentences.size(); j++) {
  269.                  fRow[0][j] = j;   
  270.             }
  271.                    
  272.             DenseMatrix tFirstRowInd = new DenseMatrix(fRow);
  273.            
  274.             DenseMatrix t = new DenseMatrix(dk.getData());
  275.            
  276.             QRP qrp = QRP.factorize(t);
  277.             DenseMatrix qrP = (DenseMatrix) qrp.getP();
  278.             DenseMatrix tp1 = new DenseMatrix(1,dk.getColumnDimension());
  279.             tFirstRowInd.mult(qrP, tp1);
  280.            
  281.             this.tp = new MtjMatrix(tp1);
  282.         }
  283.        
  284.         public QR init(ArrayList<String> sentences, MatrixInterface v, MatrixInterface u, MatrixInterface s, int k) {
  285.             setSentences(sentences);
  286.             setV(v);
  287.             setU(u);
  288.             setS(s);
  289.             setK(k);
  290.             process();
  291.             return this;
  292.         }
  293.        
  294.         public void setSentences(ArrayList<String> sentences) {
  295.             this.sentences = sentences;
  296.         }
  297.  
  298.         public void setV(MatrixInterface v) {
  299.             this.v = v;
  300.         }
  301.  
  302.         public void setU(MatrixInterface u) {
  303.             this.u = u;
  304.         }
  305.  
  306.         public void setS(MatrixInterface s) {
  307.             this.s = s;
  308.         }
  309.  
  310.         public void setK(int k) {
  311.             this.k = k;
  312.         }
  313.        
  314.         public MtjMatrix getTPMatrix() {
  315.             return tp;
  316.         }
  317.     }
  318.  
  319. }
Add Comment
Please, Sign In to add comment