/** {@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 CoreLabel proto from a CoreLabel instance. * This is not static, as it optionally throws an exception if the serialization is lossy. * @param coreLabel The CoreLabel to convert * @return A protocol buffer message corresponding to this CoreLabel */ public CoreNLPProtos.Token toProto(CoreLabel coreLabel) { Set<Class<?>> keysToSerialize = new HashSet<>(coreLabel.keySetNotNull()); CoreNLPProtos.Token.Builder builder = toProtoBuilder(coreLabel, 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 CoreLabel proto from a CoreLabel instance. * This is not static, as it optionally throws an exception if the serialization is lossy. * @param coreLabel The CoreLabel to convert * @return A protocol buffer message corresponding to this CoreLabel */ public CoreNLPProtos.Token toProto(CoreLabel coreLabel) { Set<Class<?>> keysToSerialize = new HashSet<>(coreLabel.keySetNotNull()); CoreNLPProtos.Token.Builder builder = toProtoBuilder(coreLabel, 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(); } }
/** * 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(); } }