/** * The main method for realising noun phrases. * * @param parent the <code>SyntaxProcessor</code> that called this method. * @param phrase the <code>PhraseElement</code> to be realised. * @return the realised <code>NLGElement</code>. */ static NLGElement realise(SyntaxProcessor parent, PhraseElement phrase) { ListElement realisedElement = null; if (phrase != null && !phrase.getFeatureAsBoolean(Feature.ELIDED).booleanValue()) { realisedElement = new ListElement(); if (phrase.getFeatureAsBoolean(Feature.PRONOMINAL).booleanValue()) { realisedElement.addComponent(createPronoun(parent, phrase)); } else { realiseSpecifier(phrase, parent, realisedElement); realisePreModifiers(phrase, parent, realisedElement); realiseHeadNoun(phrase, parent, realisedElement); PhraseHelper.realiseList( parent, realisedElement, phrase.getFeatureAsElementList(InternalFeature.COMPLEMENTS), DiscourseFunction.COMPLEMENT); PhraseHelper.realiseList( parent, realisedElement, phrase.getPostModifiers(), DiscourseFunction.POST_MODIFIER); } } return realisedElement; }
/** * Realises a phrase element. * * @param phrase the element to be realised * @return the realised element. */ private NLGElement realisePhraseElement(PhraseElement phrase) { NLGElement realisedElement = null; if (phrase != null) { ElementCategory category = phrase.getCategory(); if (category instanceof PhraseCategory) { switch ((PhraseCategory) category) { case CLAUSE: realisedElement = ClauseHelper.realise(this, phrase); break; case NOUN_PHRASE: realisedElement = NounPhraseHelper.realise(this, phrase); break; case VERB_PHRASE: realisedElement = VerbPhraseHelper.realise(this, phrase); break; case PREPOSITIONAL_PHRASE: case ADJECTIVE_PHRASE: case ADVERB_PHRASE: realisedElement = PhraseHelper.realise(this, phrase); break; default: realisedElement = phrase; break; } } } return realisedElement; }
/** * Realises the pre-modifiers of the noun phrase. Before being realised, pre-modifiers undergo * some basic sorting based on adjective ordering. * * @param phrase the <code>PhraseElement</code> representing this noun phrase. * @param parent the parent <code>SyntaxProcessor</code> that will do the realisation of the * complementiser. * @param realisedElement the current realisation of the noun phrase. */ private static void realisePreModifiers( PhraseElement phrase, SyntaxProcessor parent, ListElement realisedElement) { List<NLGElement> preModifiers = phrase.getPreModifiers(); if (phrase.getFeatureAsBoolean(Feature.ADJECTIVE_ORDERING).booleanValue()) { preModifiers = sortNPPreModifiers(preModifiers); } PhraseHelper.realiseList(parent, realisedElement, preModifiers, DiscourseFunction.PRE_MODIFIER); }
protected List<OffsetsEnum> createOffsetsEnumsFromReader(LeafReader atomicReader, int doc) throws IOException { // For strict positions, get a Map of term to Spans: // note: ScriptPhraseHelper.NONE does the right thing for these method calls final Map<BytesRef, Spans> strictPhrasesTermToSpans = strictPhrases.getTermToSpans(atomicReader, doc); // Usually simply wraps terms in a List; but if willRewrite() then can be expanded final List<BytesRef> sourceTerms = strictPhrases.expandTermsIfRewrite(terms, strictPhrasesTermToSpans); final List<OffsetsEnum> offsetsEnums = new ArrayList<>(sourceTerms.size() + 1); Terms termsIndex = atomicReader == null || sourceTerms.isEmpty() ? null : atomicReader.terms(field); if (termsIndex != null) { TermsEnum termsEnum = termsIndex.iterator(); // does not return null for (BytesRef term : sourceTerms) { if (!termsEnum.seekExact(term)) { continue; // term not found } PostingsEnum postingsEnum = termsEnum.postings(null, PostingsEnum.OFFSETS); if (postingsEnum == null) { // no offsets or positions available throw new IllegalArgumentException( "field '" + field + "' was indexed without offsets, cannot highlight"); } if (doc != postingsEnum.advance(doc)) { // now it's positioned, although may be exhausted continue; } postingsEnum = strictPhrases.filterPostings(term, postingsEnum, strictPhrasesTermToSpans.get(term)); if (postingsEnum == null) { continue; // completely filtered out } offsetsEnums.add(new OffsetsEnum(term, postingsEnum)); } } return offsetsEnums; }