pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / smartcn / src / java / org / apache / lucene / analysis / cn / smart / hhmm / SegGraph.java
1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis.cn.smart.hhmm;
19
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24
25 /**
26  * Graph representing possible tokens at each start offset in the sentence.
27  * <p>
28  * For each start offset, a list of possible tokens is stored.
29  * </p>
30  * @lucene.experimental
31  */
32 class SegGraph {
33
34   /**
35    * Map of start offsets to ArrayList of tokens at that position
36    */
37   private Map<Integer,ArrayList<SegToken>> tokenListTable = new HashMap<Integer,ArrayList<SegToken>>();
38
39   private int maxStart = -1;
40
41   /**
42    * Returns true if a mapping for the specified start offset exists
43    * 
44    * @param s startOffset
45    * @return true if there are tokens for the startOffset
46    */
47   public boolean isStartExist(int s) {
48     return tokenListTable.get(s) != null;
49   }
50
51   /**
52    * Get the list of tokens at the specified start offset
53    * 
54    * @param s startOffset
55    * @return List of tokens at the specified start offset.
56    */
57   public List<SegToken> getStartList(int s) {
58     return tokenListTable.get(s);
59   }
60
61   /**
62    * Get the highest start offset in the map
63    * 
64    * @return maximum start offset, or -1 if the map is empty.
65    */
66   public int getMaxStart() {
67     return maxStart;
68   }
69
70   /**
71    * Set the {@link SegToken#index} for each token, based upon its order by startOffset. 
72    * @return a {@link List} of these ordered tokens.
73    */
74   public List<SegToken> makeIndex() {
75     List<SegToken> result = new ArrayList<SegToken>();
76     int s = -1, count = 0, size = tokenListTable.size();
77     List<SegToken> tokenList;
78     int index = 0;
79     while (count < size) {
80       if (isStartExist(s)) {
81         tokenList = tokenListTable.get(s);
82         for (SegToken st : tokenList) {
83           st.index = index;
84           result.add(st);
85           index++;
86         }
87         count++;
88       }
89       s++;
90     }
91     return result;
92   }
93
94   /**
95    * Add a {@link SegToken} to the mapping, creating a new mapping at the token's startOffset if one does not exist. 
96    * @param token {@link SegToken}
97    */
98   public void addToken(SegToken token) {
99     int s = token.startOffset;
100     if (!isStartExist(s)) {
101       ArrayList<SegToken> newlist = new ArrayList<SegToken>();
102       newlist.add(token);
103       tokenListTable.put(s, newlist);
104     } else {
105       List<SegToken> tokenList = tokenListTable.get(s);
106       tokenList.add(token);
107     }
108     if (s > maxStart)
109       maxStart = s;
110   }
111
112   /**
113    * Return a {@link List} of all tokens in the map, ordered by startOffset.
114    * 
115    * @return {@link List} of all tokens in the map.
116    */
117   public List<SegToken> toTokenList() {
118     List<SegToken> result = new ArrayList<SegToken>();
119     int s = -1, count = 0, size = tokenListTable.size();
120     List<SegToken> tokenList;
121
122     while (count < size) {
123       if (isStartExist(s)) {
124         tokenList = tokenListTable.get(s);
125         for (SegToken st : tokenList) {
126           result.add(st);
127         }
128         count++;
129       }
130       s++;
131     }
132     return result;
133   }
134
135   @Override
136   public String toString() {
137     List<SegToken> tokenList = this.toTokenList();
138     StringBuilder sb = new StringBuilder();
139     for (SegToken t : tokenList) {
140       sb.append(t + "\n");
141     }
142     return sb.toString();
143   }
144 }