Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. /**
  2.  * Merge three word expressions of the form "... of ..." into single expressions.
  3.  * @return number of merges performed
  4.  */
  5. private int mergeThreeWordExpressions() {
  6.     /*
  7.     *   AD-HOC-COVERAGE
  8.         */
  9.     AdHocCoverage ahc = new AdHocCoverage("mergeThreeWordExpressions", 17);
  10.  
  11.     int changes = 0;
  12.  
  13.     // loop until no more simplifications can be made
  14.     boolean changed;
  15.     do {
  16.         final Iterator<Expression> it = expressions.iterator();
  17.  
  18.         changed = false;
  19.  
  20.         if (it.hasNext()) { // ID: 1
  21.             // COVERAGE
  22.             ahc.branchReached(1);
  23.             // COVERAGE
  24.             Expression third = it.next();
  25.  
  26.             if (it.hasNext()) { // ID: 2
  27.                 // COVERAGE
  28.                 ahc.branchReached(2);
  29.                 // COVERAGE
  30.                 Expression first = null;
  31.                 Expression second = third;
  32.                 third = it.next();
  33.  
  34.                 // loop over all words of the sentence starting from left
  35.                 while (it.hasNext()) { // ID: 3
  36.                     // COVERAGE
  37.                     ahc.branchReached(3);
  38.                     // COVERAGE
  39.                     // Now look at three consecutive words.
  40.                     first = second;
  41.                     second = third;
  42.                     third = it.next();
  43.  
  44.                     // don't merge if the break flag is set
  45.                     boolean id4 = first.getBreakFlag();
  46.                     boolean id5 = second.getBreakFlag();
  47.                     if (id4 || id5) { // ID: 4,5
  48.                         // COVERAGE
  49.                         if (id4) {
  50.                             ahc.branchReached(4);
  51.                         } else if (id5){
  52.                             ahc.branchReached(5);
  53.                         }
  54.                         // COVERAGE
  55.                         continue;
  56.                     }
  57.  
  58.                     // don't merge if there are joker expressions
  59.                     if (context.isForMatching()) { // ID: 6
  60.                         // COVERAGE
  61.                         ahc.branchReached(6);
  62.                         // COVERAGE
  63.  
  64.                         boolean id7 = first.getNormalized().contains(Expression.JOKER);
  65.                         boolean id8 = second.getNormalized().contains(Expression.JOKER);
  66.                         boolean id9 = third.getNormalized().contains(Expression.JOKER);
  67.                         if (id7 || id8 || id9) { // ID: 7,8,9
  68.                             if (id7) {
  69.                                 ahc.branchReached(7);
  70.                             } else if (id8){
  71.                                 ahc.branchReached(8);
  72.                             } else if (id9){
  73.                                 ahc.branchReached(9);
  74.                             }
  75.  
  76.                         continue;
  77.                         }
  78.                     }
  79.  
  80.                     // merge "... of ..." expressions into one expression, preserving
  81.                     // only the main word as merged normalized expression
  82.                     boolean id10 = first.isObject();
  83.                     boolean id11 = second.getNormalized().equals("of");
  84.                     boolean id12 = third.isObject();
  85.                     if ( id10 && id11 && id12 ) { // ID: 10,11,12
  86.                         // COVERAGE
  87.                         ahc.branchReached(10);
  88.                         ahc.branchReached(11);
  89.                         ahc.branchReached(12);
  90.                         // COVERAGE
  91.  
  92.                         final String expr = first.getNormalized() + " of " + third.getNormalized();
  93.  
  94.                         // see if the expression has been normalized
  95.                         if (!Grammar.isNormalized(expr)) { // ID: 13
  96.                             // COVERAGE
  97.                             ahc.branchReached(13);
  98.                             // COVERAGE
  99.  
  100.                             first.mergeRight(second, false);
  101.                             expressions.remove(second);
  102.                             third.mergeLeft(first, false);
  103.                             expressions.remove(first);
  104.                             changed = true;
  105.  
  106.                             // COVERAGE
  107.                             ahc.branchReached(14);
  108.                             // COVERAGE
  109.                             break; // ID: 14
  110.                         }
  111.                     }
  112.                 }
  113.             }
  114.         }
  115.  
  116.         if (changed) { // ID: 15
  117.             // COVERAGE
  118.             ahc.branchReached(15);
  119.             // COVERAGE
  120.             ++changes;
  121.         }
  122.     } while (changed);
  123.  
  124.     // COVERAGE
  125.     ahc.branchReached(16);
  126.     // COVERAGE
  127.     return changes; // ID: 16
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement