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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.lucene.analysis.cn.smart.hhmm;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
26 * Graph representing possible tokens at each start offset in the sentence.
28 * For each start offset, a list of possible tokens is stored.
30 * @lucene.experimental
35 * Map of start offsets to ArrayList of tokens at that position
37 private Map<Integer,ArrayList<SegToken>> tokenListTable = new HashMap<Integer,ArrayList<SegToken>>();
39 private int maxStart = -1;
42 * Returns true if a mapping for the specified start offset exists
44 * @param s startOffset
45 * @return true if there are tokens for the startOffset
47 public boolean isStartExist(int s) {
48 return tokenListTable.get(s) != null;
52 * Get the list of tokens at the specified start offset
54 * @param s startOffset
55 * @return List of tokens at the specified start offset.
57 public List<SegToken> getStartList(int s) {
58 return tokenListTable.get(s);
62 * Get the highest start offset in the map
64 * @return maximum start offset, or -1 if the map is empty.
66 public int getMaxStart() {
71 * Set the {@link SegToken#index} for each token, based upon its order by startOffset.
72 * @return a {@link List} of these ordered tokens.
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;
79 while (count < size) {
80 if (isStartExist(s)) {
81 tokenList = tokenListTable.get(s);
82 for (SegToken st : tokenList) {
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}
98 public void addToken(SegToken token) {
99 int s = token.startOffset;
100 if (!isStartExist(s)) {
101 ArrayList<SegToken> newlist = new ArrayList<SegToken>();
103 tokenListTable.put(s, newlist);
105 List<SegToken> tokenList = tokenListTable.get(s);
106 tokenList.add(token);
113 * Return a {@link List} of all tokens in the map, ordered by startOffset.
115 * @return {@link List} of all tokens in the map.
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;
122 while (count < size) {
123 if (isStartExist(s)) {
124 tokenList = tokenListTable.get(s);
125 for (SegToken st : tokenList) {
136 public String toString() {
137 List<SegToken> tokenList = this.toTokenList();
138 StringBuilder sb = new StringBuilder();
139 for (SegToken t : tokenList) {
142 return sb.toString();