Home | History | Annotate | Download | only in openwnn
      1 /*
      2  * Copyright (C) 2008-2012  OMRON SOFTWARE Co., Ltd.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package jp.co.omronsoft.openwnn;
     18 
     19 import java.lang.StringBuffer;
     20 import java.util.ArrayList;
     21 import java.util.Iterator;
     22 
     23 /**
     24  * The container class of a sentence.
     25  *
     26  * @author Copyright (C) 2009, OMRON SOFTWARE CO., LTD.  All Rights Reserved.
     27  */
     28 public class WnnSentence extends WnnWord {
     29     /** The array of clauses */
     30     public ArrayList<WnnClause> elements;
     31 
     32     /**
     33      * Constructor
     34      *
     35      * @param input     The string of reading
     36      * @param clauses   The array of clauses of this sentence
     37      */
     38     public WnnSentence(String input, ArrayList<WnnClause> clauses) {
     39         if (clauses == null || clauses.isEmpty()) {
     40             this.id = 0;
     41             this.candidate = "";
     42             this.stroke = "";
     43             this.frequency = 0;
     44             this.partOfSpeech = new WnnPOS();
     45             this.attribute = 0;
     46         } else {
     47             this.elements = clauses;
     48             WnnClause headClause = (WnnClause)clauses.get(0);
     49 
     50             if (clauses.size() == 1) {
     51                 this.id = headClause.id;
     52                 this.candidate = headClause.candidate;
     53                 this.stroke = input;
     54                 this.frequency = headClause.frequency;
     55                 this.partOfSpeech = headClause.partOfSpeech;
     56                 this.attribute = headClause.attribute;
     57             } else {
     58                 StringBuffer candidate = new StringBuffer();
     59                 Iterator<WnnClause> ci = clauses.iterator();
     60                 while (ci.hasNext()) {
     61                     WnnClause clause = ci.next();
     62                     candidate.append(clause.candidate);
     63                 }
     64                 WnnClause lastClause = (WnnClause)clauses.get(clauses.size() - 1);
     65 
     66                 this.id = headClause.id;
     67                 this.candidate = candidate.toString();
     68                 this.stroke = input;
     69                 this.frequency = headClause.frequency;
     70                 this.partOfSpeech = new WnnPOS(headClause.partOfSpeech.left, lastClause.partOfSpeech.right);
     71                 this.attribute = 2;
     72             }
     73         }
     74     }
     75 
     76     /**
     77      * Constructor
     78      *
     79      * @param input     The string of reading
     80      * @param clause    The clauses of this sentence
     81      */
     82     public WnnSentence(String input, WnnClause clause) {
     83         this.id = clause.id;
     84         this.candidate = clause.candidate;
     85         this.stroke = input;
     86         this.frequency = clause.frequency;
     87         this.partOfSpeech = clause.partOfSpeech;
     88         this.attribute = clause.attribute;
     89 
     90         this.elements = new ArrayList<WnnClause>();
     91         this.elements.add(clause);
     92     }
     93 
     94     /**
     95      * Constructor
     96      *
     97      * @param prev      The previous clauses
     98      * @param clause    The clauses of this sentence
     99      */
    100     public WnnSentence(WnnSentence prev, WnnClause clause) {
    101         this.id = prev.id;
    102         this.candidate = prev.candidate + clause.candidate;
    103         this.stroke = prev.stroke + clause.stroke;
    104         this.frequency = prev.frequency + clause.frequency;
    105         this.partOfSpeech = new WnnPOS(prev.partOfSpeech.left, clause.partOfSpeech.right);
    106         this.attribute = prev.attribute;
    107 
    108         this.elements = new ArrayList<WnnClause>();
    109         this.elements.addAll(prev.elements);
    110         this.elements.add(clause);
    111     }
    112 
    113     /**
    114      * Constructor
    115      *
    116      * @param head      The top clause of this sentence
    117      * @param tail      The following sentence
    118      */
    119     public WnnSentence(WnnClause head, WnnSentence tail) {
    120         if (tail == null) {
    121             /* single clause */
    122             this.id = head.id;
    123             this.candidate = head.candidate;
    124             this.stroke = head.stroke;
    125             this.frequency = head.frequency;
    126             this.partOfSpeech = head.partOfSpeech;
    127             this.attribute = head.attribute;
    128             this.elements = new ArrayList<WnnClause>();
    129             this.elements.add(head);
    130         } else {
    131             /* consecutive clauses */
    132             this.id = head.id;
    133             this.candidate = head.candidate + tail.candidate;
    134             this.stroke = head.stroke + tail.stroke;
    135             this.frequency = head.frequency + tail.frequency;
    136             this.partOfSpeech = new WnnPOS(head.partOfSpeech.left, tail.partOfSpeech.right);
    137             this.attribute = 2;
    138 
    139             this.elements = new ArrayList<WnnClause>();
    140             this.elements.add(head);
    141             this.elements.addAll(tail.elements);
    142         }
    143     }
    144 }
    145