Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 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 | package android.view.textclassifier; |
| 18 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 19 | import android.app.Person; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame^] | 20 | import android.content.Context; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 21 | import android.text.TextUtils; |
| 22 | import android.util.ArrayMap; |
| 23 | |
| 24 | import com.android.internal.annotations.VisibleForTesting; |
| 25 | |
| 26 | import com.google.android.textclassifier.ActionsSuggestionsModel; |
| 27 | |
| 28 | import java.util.ArrayDeque; |
| 29 | import java.util.ArrayList; |
| 30 | import java.util.Deque; |
| 31 | import java.util.List; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame^] | 32 | import java.util.Locale; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 33 | import java.util.Map; |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame^] | 34 | import java.util.Objects; |
| 35 | import java.util.StringJoiner; |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 36 | import java.util.function.Function; |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 37 | import java.util.stream.Collectors; |
| 38 | |
| 39 | /** |
| 40 | * Helper class for action suggestions. |
| 41 | * |
| 42 | * @hide |
| 43 | */ |
| 44 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 45 | public final class ActionsSuggestionsHelper { |
| 46 | private static final int USER_LOCAL = 0; |
| 47 | private static final int FIRST_NON_LOCAL_USER = 1; |
| 48 | |
| 49 | private ActionsSuggestionsHelper() {} |
| 50 | |
| 51 | /** |
| 52 | * Converts the messages to a list of native messages object that the model can understand. |
| 53 | * <p> |
| 54 | * User id encoding - local user is represented as 0, Other users are numbered according to |
| 55 | * how far before they spoke last time in the conversation. For example, considering this |
| 56 | * conversation: |
| 57 | * <ul> |
| 58 | * <li> User A: xxx |
| 59 | * <li> Local user: yyy |
| 60 | * <li> User B: zzz |
| 61 | * </ul> |
| 62 | * User A will be encoded as 2, user B will be encoded as 1 and local user will be encoded as 0. |
| 63 | */ |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 64 | public static ActionsSuggestionsModel.ConversationMessage[] toNativeMessages( |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 65 | List<ConversationActions.Message> messages, |
| 66 | Function<CharSequence, String> languageDetector) { |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 67 | List<ConversationActions.Message> messagesWithText = |
| 68 | messages.stream() |
| 69 | .filter(message -> !TextUtils.isEmpty(message.getText())) |
| 70 | .collect(Collectors.toCollection(ArrayList::new)); |
| 71 | if (messagesWithText.isEmpty()) { |
| 72 | return new ActionsSuggestionsModel.ConversationMessage[0]; |
| 73 | } |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 74 | Deque<ActionsSuggestionsModel.ConversationMessage> nativeMessages = new ArrayDeque<>(); |
| 75 | PersonEncoder personEncoder = new PersonEncoder(); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 76 | int size = messagesWithText.size(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 77 | for (int i = size - 1; i >= 0; i--) { |
| 78 | ConversationActions.Message message = messagesWithText.get(i); |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 79 | long referenceTime = message.getReferenceTime() == null |
| 80 | ? 0 |
| 81 | : message.getReferenceTime().toInstant().toEpochMilli(); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 82 | nativeMessages.push(new ActionsSuggestionsModel.ConversationMessage( |
| 83 | personEncoder.encode(message.getAuthor()), |
Tony Mak | 82fa8d9 | 2018-12-07 17:37:43 +0000 | [diff] [blame] | 84 | message.getText().toString(), referenceTime, |
| 85 | languageDetector.apply(message.getText()))); |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 86 | } |
| 87 | return nativeMessages.toArray( |
| 88 | new ActionsSuggestionsModel.ConversationMessage[nativeMessages.size()]); |
| 89 | } |
| 90 | |
Tony Mak | e94e078 | 2018-12-14 11:57:54 +0800 | [diff] [blame^] | 91 | /** |
| 92 | * Returns the result id for logging. |
| 93 | */ |
| 94 | public static String createResultId( |
| 95 | Context context, |
| 96 | List<ConversationActions.Message> messages, |
| 97 | int modelVersion, |
| 98 | List<Locale> modelLocales) { |
| 99 | final StringJoiner localesJoiner = new StringJoiner(","); |
| 100 | for (Locale locale : modelLocales) { |
| 101 | localesJoiner.add(locale.toLanguageTag()); |
| 102 | } |
| 103 | final String modelName = String.format( |
| 104 | Locale.US, "%s_v%d", localesJoiner.toString(), modelVersion); |
| 105 | final int hash = Objects.hash( |
| 106 | messages.stream() |
| 107 | .map(ConversationActions.Message::getText) |
| 108 | .collect(Collectors.toList()), |
| 109 | context.getPackageName()); |
| 110 | return SelectionSessionLogger.SignatureParser.createSignature( |
| 111 | SelectionSessionLogger.CLASSIFIER_ID, modelName, hash); |
| 112 | } |
| 113 | |
Tony Mak | f99ee17 | 2018-11-23 12:14:39 +0000 | [diff] [blame] | 114 | private static final class PersonEncoder { |
| 115 | private final Map<Person, Integer> mMapping = new ArrayMap<>(); |
| 116 | private int mNextUserId = FIRST_NON_LOCAL_USER; |
| 117 | |
| 118 | private int encode(Person person) { |
| 119 | if (ConversationActions.Message.PERSON_USER_LOCAL.equals(person)) { |
| 120 | return USER_LOCAL; |
| 121 | } |
| 122 | Integer result = mMapping.get(person); |
| 123 | if (result == null) { |
| 124 | mMapping.put(person, mNextUserId); |
| 125 | result = mNextUserId; |
| 126 | mNextUserId++; |
| 127 | } |
| 128 | return result; |
| 129 | } |
| 130 | } |
| 131 | } |