/** {@inheritDoc} */ @Override public int hashCode() { if (this.impl.hasText()) { return this.impl.getText().hashCode() * 31 + this.tokensBuilders.size(); } else { return impl.build().hashCode(); } }
/** {@inheritDoc} */ @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Sentence)) return false; Sentence sentence = (Sentence) o; // Short circuit for fast equals check if (impl.hasText() && !impl.getText().equals(sentence.impl.getText())) { return false; } if (this.tokensBuilders.size() != sentence.tokensBuilders.size()) { return false; } // Check the implementation of the sentence if (!impl.build().equals(sentence.impl.build())) { return false; } // Check each token for (int i = 0, sz = tokensBuilders.size(); i < sz; ++i) { if (!tokensBuilders.get(i).build().equals(sentence.tokensBuilders.get(i).build())) { return false; } } return true; }
/** * Create a Sentence proto from a CoreMap instance. * This is not static, as it optionally throws an exception if the serialization is lossy. * @param sentence The CoreMap to convert. Note that it should not be a CoreLabel or an Annotation, * and should represent a sentence. * @return A protocol buffer message corresponding to this sentence * @throws IllegalArgumentException If the sentence is not a valid sentence (e.g., is a document or a word). */ public CoreNLPProtos.Sentence toProto(CoreMap sentence) { Set<Class<?>> keysToSerialize = new HashSet<>(sentence.keySet()); CoreNLPProtos.Sentence.Builder builder = toProtoBuilder(sentence, keysToSerialize); // Completeness check if (enforceLosslessSerialization && !keysToSerialize.isEmpty()) { throw new LossySerializationException("Keys are not being serialized: " + StringUtils.join(keysToSerialize)); } return builder.build(); }
/** {@inheritDoc} */ @Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Sentence)) return false; Sentence sentence = (Sentence) o; // Short circuit for fast equals check if (impl.hasText() && !impl.getText().equals(sentence.impl.getText())) { return false; } if (this.tokensBuilders.size() != sentence.tokensBuilders.size()) { return false; } // Check the implementation of the sentence if (!impl.build().equals(sentence.impl.build())) { return false; } // Check each token for (int i = 0, sz = tokensBuilders.size(); i < sz; ++i) { if (!tokensBuilders.get(i).build().equals(sentence.tokensBuilders.get(i).build())) { return false; } } return true; }
/** * Create a Sentence proto from a CoreMap instance. * This is not static, as it optionally throws an exception if the serialization is lossy. * @param sentence The CoreMap to convert. Note that it should not be a CoreLabel or an Annotation, * and should represent a sentence. * @return A protocol buffer message corresponding to this sentence * @throws IllegalArgumentException If the sentence is not a valid sentence (e.g., is a document or a word). */ public CoreNLPProtos.Sentence toProto(CoreMap sentence) { Set<Class<?>> keysToSerialize = new HashSet<>(sentence.keySet()); CoreNLPProtos.Sentence.Builder builder = toProtoBuilder(sentence, keysToSerialize); // Completeness check if (enforceLosslessSerialization && !keysToSerialize.isEmpty()) { throw new LossySerializationException("Keys are not being serialized: " + StringUtils.join(keysToSerialize)); } return builder.build(); }
/** * Serialize the given sentence (but not the associated document!) into a Protocol Buffer. * * @return The Protocol Buffer representing this sentence. */ public CoreNLPProtos.Sentence serialize() { synchronized (impl) { this.impl.clearToken(); for (CoreNLPProtos.Token.Builder token : this.tokensBuilders) { this.impl.addToken(token.build()); } return impl.build(); } }
/** {@inheritDoc} */ @Override public int hashCode() { if (this.impl.hasText()) { return this.impl.getText().hashCode() * 31 + this.tokensBuilders.size(); } else { return impl.build().hashCode(); } }
/** * Make this sentence caseless. That is, from now on, run the caseless models * on the sentence by default rather than the standard CoreNLP models. * * @return A new sentence with the default properties swapped out. */ public Sentence caseless() { return new Sentence(this.docFn, impl.build(), Document.CASELESS_PROPS); }
/** * Make this sentence case sensitive. * A sentence is case sensitive by default; this only has an effect if you have previously * called {@link Sentence#caseless()}. * * @return A new sentence with the default properties swapped out. */ public Sentence cased() { return new Sentence(this.docFn, impl.build(), Document.EMPTY_PROPS); }
/** * Serialize the given sentence (but not the associated document!) into a Protocol Buffer. * * @return The Protocol Buffer representing this sentence. */ public CoreNLPProtos.Sentence serialize() { synchronized (impl) { this.impl.clearToken(); for (CoreNLPProtos.Token.Builder token : this.tokensBuilders) { this.impl.addToken(token.build()); } return impl.build(); } }
/** * Make this sentence case sensitive. * A sentence is case sensitive by default; this only has an effect if you have previously * called {@link Sentence#caseless()}. * * @return A new sentence with the default properties swapped out. */ public Sentence cased() { return new Sentence(this.docFn, impl.build(), Document.EMPTY_PROPS); }
/** * Make this sentence caseless. That is, from now on, run the caseless models * on the sentence by default rather than the standard CoreNLP models. * * @return A new sentence with the default properties swapped out. */ public Sentence caseless() { return new Sentence(this.docFn, impl.build(), Document.CASELESS_PROPS); }