本文整理汇总了Java中org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute.getPositionIncrement方法的典型用法代码示例。如果您正苦于以下问题:Java PositionIncrementAttribute.getPositionIncrement方法的具体用法?Java PositionIncrementAttribute.getPositionIncrement怎么用?Java PositionIncrementAttribute.getPositionIncrement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute
的用法示例。
在下文中一共展示了PositionIncrementAttribute.getPositionIncrement方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
final Tokenizer tok = new WhitespaceTokenizer();
tok.setReader(new StringReader("dark sea green sea green"));
final SynonymMap.Builder builder = new SynonymMap.Builder(true);
addSynonym("dark sea green", "color", builder);
addSynonym("green", "color", builder);
addSynonym("dark sea", "color", builder);
addSynonym("sea green", "color", builder);
final SynonymMap synMap = builder.build();
final TokenStream ts = new SynonymFilter(tok, synMap, true);
final CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
final PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class);
final PositionLengthAttribute posLengthAtt = ts.addAttribute(PositionLengthAttribute.class);
ts.reset();
int pos = -1;
while (ts.incrementToken()) {
pos += posIncrAtt.getPositionIncrement();
System.out.println("term=" + termAtt + ", pos=" + pos + ", posLen=" + posLengthAtt.getPositionLength());
}
ts.end();
ts.close();
}
示例2: analyze
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private Set<String> analyze(String text) throws IOException {
Set<String> result = new HashSet<String>();
Analyzer analyzer = configuration.getAnalyzer();
try (TokenStream ts = analyzer.tokenStream("", text)) {
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncAtt = ts.addAttribute(PositionIncrementAttribute.class);
ts.reset();
while (ts.incrementToken()) {
int length = termAtt.length();
if (length == 0) {
throw new IllegalArgumentException("term: " + text + " analyzed to a zero-length token");
}
if (posIncAtt.getPositionIncrement() != 1) {
throw new IllegalArgumentException("term: " + text + " analyzed to a token with posinc != 1");
}
result.add(new String(termAtt.buffer(), 0, termAtt.length()));
}
ts.end();
return result;
}
}
示例3: testShingleAnalyzerWrapperPhraseQuery
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
public void testShingleAnalyzerWrapperPhraseQuery() throws Exception {
PhraseQuery q = new PhraseQuery();
TokenStream ts = analyzer.tokenStream("content", "this sentence");
try {
int j = -1;
PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class);
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
ts.reset();
while (ts.incrementToken()) {
j += posIncrAtt.getPositionIncrement();
String termText = termAtt.toString();
q.add(new Term("content", termText), j);
}
ts.end();
} finally {
IOUtils.closeWhileHandlingException(ts);
}
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
int[] ranks = new int[] { 0 };
compareRanks(hits, ranks);
}
示例4: handleTokenStream
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private void handleTokenStream(Map<Integer, List<Token>> tokenPosMap, TokenStream tokenStream) throws IOException {
tokenStream.reset();
int pos = 0;
CharTermAttribute charTermAttribute = getCharTermAttribute(tokenStream);
OffsetAttribute offsetAttribute = getOffsetAttribute(tokenStream);
TypeAttribute typeAttribute = getTypeAttribute(tokenStream);
PositionIncrementAttribute positionIncrementAttribute = getPositionIncrementAttribute(tokenStream);
while (tokenStream.incrementToken()) {
if (null == charTermAttribute || null == offsetAttribute) {
return;
}
Token token = new Token(charTermAttribute.buffer(), 0, charTermAttribute.length(),
offsetAttribute.startOffset(), offsetAttribute.endOffset());
if (null != typeAttribute) {
token.setType(typeAttribute.type());
}
pos += null != positionIncrementAttribute ? positionIncrementAttribute.getPositionIncrement() : 1;
if (!tokenPosMap.containsKey(pos)) {
tokenPosMap.put(pos, new LinkedList<Token>());
}
tokenPosMap.get(pos).add(token);
}
tokenStream.close();
}
示例5: analyzeMultiBoolean
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
/**
* Creates complex boolean query from the cached tokenstream contents
*/
protected Query analyzeMultiBoolean(String field, TokenStream stream, BooleanClause.Occur operator)
throws IOException {
BooleanQuery.Builder q = newBooleanQuery();
List<Term> currentQuery = new ArrayList<>();
TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class);
stream.reset();
while (stream.incrementToken()) {
if (posIncrAtt.getPositionIncrement() != 0) {
add(q, currentQuery, operator);
currentQuery.clear();
}
currentQuery.add(new Term(field, termAtt.getBytesRef()));
}
add(q, currentQuery, operator);
return q.build();
}
示例6: analyzePhrase
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
/**
* Creates simple phrase query from the cached tokenstream contents
*/
protected Query analyzePhrase(String field, TokenStream stream, int slop) throws IOException {
PhraseQuery.Builder builder = new PhraseQuery.Builder();
builder.setSlop(slop);
TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
PositionIncrementAttribute posIncrAtt = stream.getAttribute(PositionIncrementAttribute.class);
int position = -1;
stream.reset();
while (stream.incrementToken()) {
if (enablePositionIncrements) {
position += posIncrAtt.getPositionIncrement();
} else {
position += 1;
}
builder.add(new Term(field, termAtt.getBytesRef()), position);
}
return builder.build();
}
示例7: assertTokenInfos
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private static void assertTokenInfos(TokenStream ts, TokenInfo... infos) throws IOException {
ts.reset();
final CharTermAttribute term = ts.addAttribute(CharTermAttribute.class);
final PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class);
final PayloadAttribute payloadAtt = ts.addAttribute(PayloadAttribute.class);
final ByteArrayDataInput in = new ByteArrayDataInput();
int pos = -1;
for (final TokenInfo info : infos) {
assertThat(ts.incrementToken()).isTrue();
pos += posIncrAtt.getPositionIncrement();
int len = -1;
final BytesRef payload = payloadAtt.getPayload();
if (info.len != -1) {
assertThat(payload).isNotNull();
in.reset(payload.bytes);
len = in.readVInt();
} else {
assertThat(payload).isNull();
}
assertThat(new TokenInfo(term.toString(), pos, len)).isEqualTo(info);
}
assertThat(ts.incrementToken()).isFalse();
}
示例8: testShingleAnalyzerWrapperPhraseQuery
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
public void testShingleAnalyzerWrapperPhraseQuery() throws Exception {
PhraseQuery q = new PhraseQuery();
TokenStream ts = analyzer.tokenStream("content", new StringReader("this sentence"));
int j = -1;
PositionIncrementAttribute posIncrAtt = ts.addAttribute(PositionIncrementAttribute.class);
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
ts.reset();
while (ts.incrementToken()) {
j += posIncrAtt.getPositionIncrement();
String termText = termAtt.toString();
q.add(new Term("content", termText), j);
}
ScoreDoc[] hits = searcher.search(q, null, 1000).scoreDocs;
int[] ranks = new int[] { 0 };
compareRanks(hits, ranks);
}
示例9: countPositions
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
/**
* Count position increments in a token stream. Package private for testing.
* @param analyzer analyzer to create token stream
* @param fieldName field name to pass to analyzer
* @param fieldValue field value to pass to analyzer
* @return number of position increments in a token stream
* @throws IOException if tokenStream throws it
*/
static int countPositions(Analyzer analyzer, String fieldName, String fieldValue) throws IOException {
try (TokenStream tokenStream = analyzer.tokenStream(fieldName, fieldValue)) {
int count = 0;
PositionIncrementAttribute position = tokenStream.addAttribute(PositionIncrementAttribute.class);
tokenStream.reset();
while (tokenStream.incrementToken()) {
count += position.getPositionIncrement();
}
tokenStream.end();
count += position.getPositionIncrement();
return count;
}
}
示例10: simpleAnalyze
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private static List<AnalyzeResponse.AnalyzeToken> simpleAnalyze(AnalyzeRequest request, Analyzer analyzer, String field) {
List<AnalyzeResponse.AnalyzeToken> tokens = new ArrayList<>();
int lastPosition = -1;
int lastOffset = 0;
for (String text : request.text()) {
try (TokenStream stream = analyzer.tokenStream(field, text)) {
stream.reset();
CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);
TypeAttribute type = stream.addAttribute(TypeAttribute.class);
PositionLengthAttribute posLen = stream.addAttribute(PositionLengthAttribute.class);
while (stream.incrementToken()) {
int increment = posIncr.getPositionIncrement();
if (increment > 0) {
lastPosition = lastPosition + increment;
}
tokens.add(new AnalyzeResponse.AnalyzeToken(term.toString(), lastPosition, lastOffset + offset.startOffset(),
lastOffset + offset.endOffset(), posLen.getPositionLength(), type.type(), null));
}
stream.end();
lastOffset += offset.endOffset();
lastPosition += posIncr.getPositionIncrement();
lastPosition += analyzer.getPositionIncrementGap(field);
lastOffset += analyzer.getOffsetGap(field);
} catch (IOException e) {
throw new ElasticsearchException("failed to analyze", e);
}
}
return tokens;
}
示例11: analyze
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private void analyze(TokenStream stream, Analyzer analyzer, String field, Set<String> includeAttributes) {
try {
stream.reset();
CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);
TypeAttribute type = stream.addAttribute(TypeAttribute.class);
PositionLengthAttribute posLen = stream.addAttribute(PositionLengthAttribute.class);
while (stream.incrementToken()) {
int increment = posIncr.getPositionIncrement();
if (increment > 0) {
lastPosition = lastPosition + increment;
}
tokens.add(new AnalyzeResponse.AnalyzeToken(term.toString(), lastPosition, lastOffset + offset.startOffset(),
lastOffset + offset.endOffset(), posLen.getPositionLength(), type.type(), extractExtendedAttributes(stream, includeAttributes)));
}
stream.end();
lastOffset += offset.endOffset();
lastPosition += posIncr.getPositionIncrement();
lastPosition += analyzer.getPositionIncrementGap(field);
lastOffset += analyzer.getOffsetGap(field);
} catch (IOException e) {
throw new ElasticsearchException("failed to analyze", e);
} finally {
IOUtils.closeWhileHandlingException(stream);
}
}
示例12: analyze
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
/** Sugar: analyzes the text with the analyzer and
* separates by {@link SynonymMap#WORD_SEPARATOR}.
* reuse and its chars must not be null. */
public CharsRef analyze(String text, CharsRefBuilder reuse) throws IOException {
try (TokenStream ts = analyzer.tokenStream("", text)) {
CharTermAttribute termAtt = ts.addAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncAtt = ts.addAttribute(PositionIncrementAttribute.class);
ts.reset();
reuse.clear();
while (ts.incrementToken()) {
int length = termAtt.length();
if (length == 0) {
throw new IllegalArgumentException("term: " + text + " analyzed to a zero-length token");
}
if (posIncAtt.getPositionIncrement() != 1) {
throw new IllegalArgumentException("term: " + text + " analyzed to a token with posinc != 1");
}
reuse.grow(reuse.length() + length + 1); /* current + word + separator */
int end = reuse.length();
if (reuse.length() > 0) {
reuse.setCharAt(end++, SynonymMap.WORD_SEPARATOR);
reuse.setLength(reuse.length() + 1);
}
System.arraycopy(termAtt.buffer(), 0, reuse.chars(), end, length);
reuse.setLength(reuse.length() + length);
}
ts.end();
}
if (reuse.length() == 0) {
throw new IllegalArgumentException("term: " + text + " was completely eliminated by analyzer");
}
return reuse.get();
}
示例13: simpleAnalyze
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private static List<AnalyzeResponse.AnalyzeToken> simpleAnalyze(AnalyzeRequest request, Analyzer analyzer, String field) {
List<AnalyzeResponse.AnalyzeToken> tokens = new ArrayList<>();
int lastPosition = -1;
int lastOffset = 0;
for (String text : request.text()) {
try (TokenStream stream = analyzer.tokenStream(field, text)) {
stream.reset();
CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);
TypeAttribute type = stream.addAttribute(TypeAttribute.class);
while (stream.incrementToken()) {
int increment = posIncr.getPositionIncrement();
if (increment > 0) {
lastPosition = lastPosition + increment;
}
tokens.add(new AnalyzeResponse.AnalyzeToken(term.toString(), lastPosition, lastOffset + offset.startOffset(), lastOffset + offset.endOffset(), type.type(), null));
}
stream.end();
lastOffset += offset.endOffset();
lastPosition += posIncr.getPositionIncrement();
lastPosition += analyzer.getPositionIncrementGap(field);
lastOffset += analyzer.getOffsetGap(field);
} catch (IOException e) {
throw new ElasticsearchException("failed to analyze", e);
}
}
return tokens;
}
示例14: analyze
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
private void analyze(TokenStream stream, Analyzer analyzer, String field, Set<String> includeAttributes) {
try {
stream.reset();
CharTermAttribute term = stream.addAttribute(CharTermAttribute.class);
PositionIncrementAttribute posIncr = stream.addAttribute(PositionIncrementAttribute.class);
OffsetAttribute offset = stream.addAttribute(OffsetAttribute.class);
TypeAttribute type = stream.addAttribute(TypeAttribute.class);
while (stream.incrementToken()) {
int increment = posIncr.getPositionIncrement();
if (increment > 0) {
lastPosition = lastPosition + increment;
}
tokens.add(new AnalyzeResponse.AnalyzeToken(term.toString(), lastPosition, lastOffset + offset.startOffset(),
lastOffset +offset.endOffset(), type.type(), extractExtendedAttributes(stream, includeAttributes)));
}
stream.end();
lastOffset += offset.endOffset();
lastPosition += posIncr.getPositionIncrement();
lastPosition += analyzer.getPositionIncrementGap(field);
lastOffset += analyzer.getOffsetGap(field);
} catch (IOException e) {
throw new ElasticsearchException("failed to analyze", e);
} finally {
IOUtils.closeWhileHandlingException(stream);
}
}
示例15: printResultOfTokenStream
import org.apache.lucene.analysis.tokenattributes.PositionIncrementAttribute; //导入方法依赖的package包/类
public static void printResultOfTokenStream(PrintStream out, TokenStream ts) throws IOException {
CharTermAttribute termAttr = ts.getAttribute(CharTermAttribute.class);
TypeAttribute typeAttr = ts.getAttribute(TypeAttribute.class);
OffsetAttribute offAttr = ts.getAttribute(OffsetAttribute.class);
PositionIncrementAttribute posIncAttr = ts.getAttribute(PositionIncrementAttribute.class);
PositionLengthAttribute posLenAttr = ts.getAttribute(PositionLengthAttribute.class);
ts.reset();
Table<String, String, String> contentTable = Tables.newCustomTable(new LinkedHashMap<String, Map<String, String>>(),
new Supplier<Map<String, String>>() {
@Override
public Map<String, String> get() {
return Maps.newLinkedHashMap();
}
});
int lineNo = 1;
int pos = 0;
while (ts.incrementToken()) {
String lineId = lineNo + ".";
contentTable.put(lineId, "term", termAttr.toString());
contentTable.put(lineId, "type", typeAttr.type());
contentTable.put(lineId, "startOffset", offAttr.startOffset() + "");
contentTable.put(lineId, "endOffset", offAttr.endOffset() + "");
contentTable.put(lineId, "posInc", posIncAttr.getPositionIncrement() + "");
contentTable.put(lineId, "posLen", posLenAttr.getPositionLength() + "");
pos += posIncAttr.getPositionIncrement();
contentTable.put(lineId, "pos", pos + "");
lineNo++;
}
printTable(out, contentTable);
}