001/* 002 * Copyright (c) 2010-2024 Mark Allen, Norbert Bartels. 003 * 004 * Permission is hereby granted, free of charge, to any person obtaining a copy 005 * of this software and associated documentation files (the "Software"), to deal 006 * in the Software without restriction, including without limitation the rights 007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 008 * copies of the Software, and to permit persons to whom the Software is 009 * furnished to do so, subject to the following conditions: 010 * 011 * The above copyright notice and this permission notice shall be included in 012 * all copies or substantial portions of the Software. 013 * 014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 020 * THE SOFTWARE. 021 */ 022package com.restfb.types.webhook.messaging; 023 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027 028import com.restfb.Facebook; 029import com.restfb.JsonMapper; 030import com.restfb.json.JsonObject; 031import com.restfb.types.webhook.messaging.nlp.*; 032 033public class NlpResult { 034 035 private List<BaseNlpEntity> convertedEntities = new ArrayList<>(); 036 037 @Facebook("entities") 038 private JsonObject rawEntities; 039 040 @Facebook 041 private List<NlpError> errors = new ArrayList<>(); 042 043 @JsonMapper.JsonMappingCompleted 044 public void convertRawEntites(JsonMapper mapper) { 045 List<String> names = (rawEntities != null) ? rawEntities.names() : Collections.<String> emptyList(); 046 for (String key : names) { 047 if ("datetime".equals(key)) { 048 List<NlpDatetime> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpDatetime.class); 049 convertedEntities.addAll(list); 050 } else if ("bye".equals(key)) { 051 List<NlpBye> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpBye.class); 052 convertedEntities.addAll(list); 053 } else if ("reminder".equals(key)) { 054 List<NlpReminder> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpReminder.class); 055 convertedEntities.addAll(list); 056 } else if ("greetings".equals(key)) { 057 List<NlpGreetings> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpGreetings.class); 058 convertedEntities.addAll(list); 059 } else if ("amount_of_money".equals(key)) { 060 List<NlpAmountOfMoney> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpAmountOfMoney.class); 061 convertedEntities.addAll(list); 062 } else if ("phone_number".equals(key)) { 063 List<NlpPhoneNumber> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpPhoneNumber.class); 064 convertedEntities.addAll(list); 065 } else if ("email".equals(key)) { 066 List<NlpEmail> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpEmail.class); 067 convertedEntities.addAll(list); 068 } else if ("distance".equals(key)) { 069 List<NlpDistance> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpDistance.class); 070 convertedEntities.addAll(list); 071 } else if ("volume".equals(key)) { 072 List<NlpVolume> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpVolume.class); 073 convertedEntities.addAll(list); 074 } else if ("temperature".equals(key)) { 075 List<NlpTemperature> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpTemperature.class); 076 convertedEntities.addAll(list); 077 } else if ("quantity".equals(key)) { 078 List<NlpQuantity> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpQuantity.class); 079 convertedEntities.addAll(list); 080 } else if ("duration".equals(key)) { 081 List<NlpDuration> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpDuration.class); 082 convertedEntities.addAll(list); 083 } else if ("sentiment".equals(key)) { 084 List<NlpSentiment> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpSentiment.class); 085 convertedEntities.addAll(list); 086 } else if ("url".equals(key)) { 087 List<NlpUrl> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpUrl.class); 088 convertedEntities.addAll(list); 089 } else if ("location".equals(key)) { 090 List<NlpLocation> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpLocation.class); 091 convertedEntities.addAll(list); 092 } else { 093 List<NlpCustomWitAi> list = mapper.toJavaList(rawEntities.get(key).toString(), NlpCustomWitAi.class); 094 for (NlpCustomWitAi customNlp : list) { 095 customNlp.setWitAiKey(key); 096 } 097 convertedEntities.addAll(list); 098 } 099 } 100 } 101 102 /** 103 * returns the complete list of all found entities. 104 * 105 * @return the complete list of all found entities. 106 */ 107 public List<BaseNlpEntity> getEntities() { 108 return convertedEntities; 109 } 110 111 /** 112 * returns the complete list of all found errors 113 * 114 * @return the complete list of all found errors 115 */ 116 public List<NlpError> getErrors() { 117 return errors; 118 } 119 120 /** 121 * checks if the returned JSON contains the error field 122 * 123 * @return {@code true} if the NLP result found errors, {@code false} otherwise 124 */ 125 public boolean hasErrors() { 126 return !errors.isEmpty(); 127 } 128 129 /** 130 * returns a subset of the found entities. 131 * 132 * Only entities that are of type <code>T</code> are returned. T needs to extend the {@link BaseNlpEntity}. 133 * 134 * @param clazz 135 * the filter class 136 * @return List of entites, only the filtered elements are returned. 137 */ 138 public <T extends BaseNlpEntity> List<T> getEntities(Class<T> clazz) { 139 List<BaseNlpEntity> resultList = new ArrayList<>(); 140 for (BaseNlpEntity item : getEntities()) { 141 if (item.getClass().equals(clazz)) { 142 resultList.add(item); 143 } 144 } 145 return (List<T>) resultList; 146 } 147}