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/strip-unpaired-brackets.h"
18
19 #include <iterator>
20
21 #include "utils/base/logging.h"
22 #include "utils/utf8/unicodetext.h"
23
24 namespace libtextclassifier3 {
25
StripUnpairedBrackets(const UnicodeText::const_iterator & span_begin,const UnicodeText::const_iterator & span_end,CodepointSpan span,const UniLib & unilib)26 CodepointSpan StripUnpairedBrackets(
27 const UnicodeText::const_iterator& span_begin,
28 const UnicodeText::const_iterator& span_end, CodepointSpan span,
29 const UniLib& unilib) {
30 if (span_begin == span_end || !span.IsValid() || span.IsEmpty()) {
31 return span;
32 }
33
34 UnicodeText::const_iterator begin = span_begin;
35 const UnicodeText::const_iterator end = span_end;
36 const char32 begin_char = *begin;
37 const char32 paired_begin_char = unilib.GetPairedBracket(begin_char);
38 if (paired_begin_char != begin_char) {
39 if (!unilib.IsOpeningBracket(begin_char) ||
40 std::find(begin, end, paired_begin_char) == end) {
41 ++begin;
42 ++span.first;
43 }
44 }
45
46 if (span.first == span.second) {
47 return span;
48 }
49
50 const char32 end_char = *std::prev(end);
51 const char32 paired_end_char = unilib.GetPairedBracket(end_char);
52 if (paired_end_char != end_char) {
53 if (!unilib.IsClosingBracket(end_char) ||
54 std::find(begin, end, paired_end_char) == end) {
55 --span.second;
56 }
57 }
58
59 // Should not happen, but let's make sure.
60 if (span.first > span.second) {
61 TC3_LOG(WARNING) << "Inverse indices result: " << span.first << ", "
62 << span.second;
63 span.second = span.first;
64 }
65
66 return span;
67 }
68
StripUnpairedBrackets(const UnicodeText & context,CodepointSpan span,const UniLib & unilib)69 CodepointSpan StripUnpairedBrackets(const UnicodeText& context,
70 CodepointSpan span, const UniLib& unilib) {
71 if (!span.IsValid() || span.IsEmpty()) {
72 return span;
73 }
74 const UnicodeText span_text = UnicodeText::Substring(
75 context, span.first, span.second, /*do_copy=*/false);
76 return StripUnpairedBrackets(span_text.begin(), span_text.end(), span,
77 unilib);
78 }
79
StripUnpairedBrackets(const std::string & context,CodepointSpan span,const UniLib & unilib)80 CodepointSpan StripUnpairedBrackets(const std::string& context,
81 CodepointSpan span, const UniLib& unilib) {
82 return StripUnpairedBrackets(UTF8ToUnicodeText(context, /*do_copy=*/false),
83 span, unilib);
84 }
85
86 } // namespace libtextclassifier3
87