use of org.jbibtex.BibTeXEntry in project jabref by JabRef.
the class CitationStyleGenerator method bibEntryToCSLItemData.
/**
* Converts the {@link BibEntry} into {@link CSLItemData}.
*/
private static CSLItemData bibEntryToCSLItemData(BibEntry bibEntry) {
String citeKey = bibEntry.getCiteKeyOptional().orElse("");
BibTeXEntry bibTeXEntry = new BibTeXEntry(new Key(bibEntry.getType()), new Key(citeKey));
// Not every field is already generated into latex free fields
for (String key : bibEntry.getFieldMap().keySet()) {
Optional<String> latexFreeField = bibEntry.getLatexFreeField(key);
latexFreeField.ifPresent(value -> bibTeXEntry.addField(new Key(key), new DigitStringValue(value)));
}
return BIBTEX_CONVERTER.toItemData(bibTeXEntry);
}
use of org.jbibtex.BibTeXEntry in project Anserini by castorini.
the class BibtexGenerator method createDocument.
@Override
public Document createDocument(BibtexCollection.Document bibtexDoc) throws GeneratorException {
String id = bibtexDoc.id();
String content = bibtexDoc.contents();
String type = bibtexDoc.type();
BibTeXEntry bibtexEntry = bibtexDoc.bibtexEntry();
if (content == null || content.trim().isEmpty()) {
throw new EmptyDocumentException();
}
Document doc = new Document();
// Store the collection docid.
doc.add(new StringField(IndexArgs.ID, id, Field.Store.YES));
// This is needed to break score ties by docid.
doc.add(new SortedDocValuesField(IndexArgs.ID, new BytesRef(id)));
// Store the collection's bibtex type
doc.add(new StringField(TYPE, type, Field.Store.YES));
if (args.storeRaw) {
doc.add(new StoredField(IndexArgs.RAW, bibtexDoc.raw()));
}
FieldType fieldType = new FieldType();
fieldType.setStored(args.storeContents);
// Are we storing document vectors?
if (args.storeDocvectors) {
fieldType.setStoreTermVectors(true);
fieldType.setStoreTermVectorPositions(true);
}
// Are we building a "positional" or "count" index?
if (args.storePositions) {
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
} else {
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
}
doc.add(new Field(IndexArgs.CONTENTS, content, fieldType));
for (Map.Entry<Key, Value> fieldEntry : bibtexEntry.getFields().entrySet()) {
String fieldKey = fieldEntry.getKey().toString();
String fieldValue = fieldEntry.getValue().toUserString();
// not worth trying to parse/normalize all numbers at the moment
if (fieldKey.equals(BibtexField.NUMBER.name)) {
continue;
}
if (STRING_FIELD_NAMES.contains(fieldKey)) {
// index field as single token
doc.add(new StringField(fieldKey, fieldValue, Field.Store.YES));
} else if (FIELDS_WITHOUT_STEMMING.contains(fieldKey)) {
// index field without stemming but store original string value
FieldType nonStemmedType = new FieldType(fieldType);
nonStemmedType.setStored(true);
// token stream to be indexed
Analyzer nonStemmingAnalyzer = DefaultEnglishAnalyzer.newNonStemmingInstance(CharArraySet.EMPTY_SET);
StringReader reader = new StringReader(fieldValue);
TokenStream stream = nonStemmingAnalyzer.tokenStream(null, reader);
Field field = new Field(fieldKey, fieldValue, nonStemmedType);
field.setTokenStream(stream);
doc.add(field);
nonStemmingAnalyzer.close();
} else if (fieldKey.equals(BibtexField.YEAR.name)) {
if (fieldValue != "") {
// index as numeric value to allow range queries
doc.add(new IntPoint(fieldKey, Integer.parseInt(fieldValue)));
}
doc.add(new StoredField(fieldKey, fieldValue));
} else {
// default to normal Field with tokenization and stemming
doc.add(new Field(fieldKey, fieldValue, fieldType));
}
}
return doc;
}
Aggregations