1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "annotator/types.h"
18
19 namespace libtextclassifier3 {
20
operator <<(logging::LoggingStringStream & stream,const Token & token)21 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
22 const Token& token) {
23 if (!token.is_padding) {
24 return stream << "Token(\"" << token.value << "\", " << token.start << ", "
25 << token.end << ")";
26 } else {
27 return stream << "Token()";
28 }
29 }
30
31 namespace {
FormatMillis(int64 time_ms_utc)32 std::string FormatMillis(int64 time_ms_utc) {
33 long time_seconds = time_ms_utc / 1000; // NOLINT
34 char buffer[512];
35 strftime(buffer, sizeof(buffer), "%a %Y-%m-%d %H:%M:%S %Z",
36 localtime(&time_seconds));
37 return std::string(buffer);
38 }
39 } // namespace
40
operator <<(logging::LoggingStringStream & stream,const DatetimeParseResultSpan & value)41 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
42 const DatetimeParseResultSpan& value) {
43 stream << "DatetimeParseResultSpan({" << value.span.first << ", "
44 << value.span.second << "}, {";
45 for (const DatetimeParseResult& data : value.data) {
46 stream << "{/*time_ms_utc=*/ " << data.time_ms_utc << " /* "
47 << FormatMillis(data.time_ms_utc) << " */, /*granularity=*/ "
48 << data.granularity << "}, ";
49 }
50 stream << "})";
51 return stream;
52 }
53
operator <<(logging::LoggingStringStream & stream,const ClassificationResult & result)54 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
55 const ClassificationResult& result) {
56 return stream << "ClassificationResult(" << result.collection
57 << ", /*score=*/ " << result.score << ", /*priority_score=*/ "
58 << result.priority_score << ")";
59 }
60
operator <<(logging::LoggingStringStream & stream,const std::vector<ClassificationResult> & results)61 logging::LoggingStringStream& operator<<(
62 logging::LoggingStringStream& stream,
63 const std::vector<ClassificationResult>& results) {
64 stream = stream << "{\n";
65 for (const ClassificationResult& result : results) {
66 stream = stream << " " << result << "\n";
67 }
68 stream = stream << "}";
69 return stream;
70 }
71
operator <<(logging::LoggingStringStream & stream,const AnnotatedSpan & span)72 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
73 const AnnotatedSpan& span) {
74 std::string best_class;
75 float best_score = -1;
76 if (!span.classification.empty()) {
77 best_class = span.classification[0].collection;
78 best_score = span.classification[0].score;
79 }
80 return stream << "Span(" << span.span.first << ", " << span.span.second
81 << ", " << best_class << ", " << best_score << ")";
82 }
83
operator <<(logging::LoggingStringStream & stream,const DateParseData & data)84 logging::LoggingStringStream& operator<<(logging::LoggingStringStream& stream,
85 const DateParseData& data) {
86 // TODO(zilka): Add human-readable form of field_set_mask and the enum fields.
87 stream = stream << "DateParseData {\n";
88 stream = stream << " field_set_mask: " << data.field_set_mask << "\n";
89 stream = stream << " year: " << data.year << "\n";
90 stream = stream << " month: " << data.month << "\n";
91 stream = stream << " day_of_month: " << data.day_of_month << "\n";
92 stream = stream << " hour: " << data.hour << "\n";
93 stream = stream << " minute: " << data.minute << "\n";
94 stream = stream << " second: " << data.second << "\n";
95 stream = stream << " ampm: " << static_cast<int>(data.ampm) << "\n";
96 stream = stream << " zone_offset: " << data.zone_offset << "\n";
97 stream = stream << " dst_offset: " << data.dst_offset << "\n";
98 stream = stream << " relation: " << static_cast<int>(data.relation) << "\n";
99 stream = stream << " relation_type: " << static_cast<int>(data.relation_type)
100 << "\n";
101 stream = stream << " relation_distance: " << data.relation_distance << "\n";
102 stream = stream << "}";
103 return stream;
104 }
105
106 } // namespace libtextclassifier3
107