| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.build.indexer; |
| 7 |
|
| 8 |
import java.util.ArrayList; |
| 9 |
import java.util.HashMap; |
| 10 |
import java.util.Map.Entry; |
| 11 |
|
| 12 |
/** |
| 13 |
* Represents a word of the full text index. |
| 14 |
*/ |
| 15 |
public class Word { |
| 16 |
|
| 17 |
/** |
| 18 |
* The word text. |
| 19 |
*/ |
| 20 |
String name; |
| 21 |
|
| 22 |
/** |
| 23 |
* The pages map. |
| 24 |
*/ |
| 25 |
final HashMap<Page, Weight> pages = new HashMap<>(); |
| 26 |
|
| 27 |
private ArrayList<Weight> weightList; |
| 28 |
|
| 29 |
Word(String name) { |
| 30 |
this.name = name; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Add a page to this word. |
| 35 |
* |
| 36 |
* @param page the page |
| 37 |
* @param weight the weight of this word in this page |
| 38 |
*/ |
| 39 |
void addPage(Page page, int weight) { |
| 40 |
Weight w = pages.get(page); |
| 41 |
if (w == null) { |
| 42 |
w = new Weight(); |
| 43 |
w.page = page; |
| 44 |
pages.put(page, w); |
| 45 |
} |
| 46 |
w.value += weight; |
| 47 |
page.relations++; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public String toString() { |
| 52 |
return name + ":" + pages; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Add all data of the other word to this word. |
| 57 |
* |
| 58 |
* @param other the other word |
| 59 |
*/ |
| 60 |
void addAll(Word other) { |
| 61 |
for (Entry<Page, Weight> entry : other.pages.entrySet()) { |
| 62 |
Page p = entry.getKey(); |
| 63 |
Weight w = entry.getValue(); |
| 64 |
addPage(p, w.value); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
ArrayList<Weight> getSortedWeights() { |
| 69 |
if (weightList == null) { |
| 70 |
weightList = new ArrayList<>(pages.values()); |
| 71 |
weightList.sort((w0, w1) -> Integer.compare(w1.value, w0.value)); |
| 72 |
} |
| 73 |
return weightList; |
| 74 |
} |
| 75 |
} |