|
| 1 | +package gumga.framework.application.nlp; |
| 2 | + |
| 3 | +import gumga.framework.core.GumgaValues; |
| 4 | +import gumga.framework.domain.nlp.GumgaNLPThing; |
| 5 | +import java.lang.reflect.Field; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Locale; |
| 11 | +import java.util.Map; |
| 12 | +import java.util.Set; |
| 13 | +import org.cogroo.analyzer.Analyzer; |
| 14 | +import org.cogroo.analyzer.ComponentFactory; |
| 15 | +import org.cogroo.text.Chunk; |
| 16 | +import org.cogroo.text.Document; |
| 17 | +import org.cogroo.text.Sentence; |
| 18 | +import org.cogroo.text.SyntacticChunk; |
| 19 | +import org.cogroo.text.Token; |
| 20 | +import org.cogroo.text.impl.DocumentImpl; |
| 21 | +import org.reflections.Reflections; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.stereotype.Component; |
| 24 | + |
| 25 | +@Component |
| 26 | +public class GumgaNLP { |
| 27 | + |
| 28 | + public static final String NO_NAME = "_NO_NAME"; |
| 29 | + private final Analyzer cogroo; |
| 30 | + private final Reflections reflections; |
| 31 | + private final Set<Class<?>> classOfInterest; |
| 32 | + |
| 33 | + @Autowired |
| 34 | + public GumgaNLP(GumgaValues gumgaValues) { |
| 35 | + String basePackage = gumgaValues.getGumgaNLPBasePackage(); |
| 36 | + ComponentFactory factory = ComponentFactory.create(new Locale("pt", "BR")); |
| 37 | + cogroo = factory.createPipe(); |
| 38 | + reflections = new Reflections(basePackage); |
| 39 | + System.out.println("ReflectionsConfiguration------->" + reflections.getConfiguration().getUrls()); |
| 40 | + classOfInterest = reflections.getTypesAnnotatedWith(GumgaNLPThing.class); |
| 41 | + System.out.println("GumgaNLP ------" + basePackage + "----->" + classOfInterest); |
| 42 | + } |
| 43 | + |
| 44 | + private enum Estados { |
| 45 | + VERBO, SUBSTANTIVO, FIM, ATRIBUTOS, VALOR_ATRIBUTO |
| 46 | + }; |
| 47 | + |
| 48 | + public List<Object> createObjectsFromDocument(String text, String instanceVerbs) throws Exception { |
| 49 | + List<String> verbs = Arrays.asList(instanceVerbs.split(",")); |
| 50 | + Document document = new DocumentImpl(); |
| 51 | + document.setText(text); |
| 52 | + cogroo.analyze(document); |
| 53 | + print(document); |
| 54 | + ArrayList<Object> toReturn = new ArrayList<>(); |
| 55 | + Object currentObject = null; |
| 56 | + Field currentField = null; |
| 57 | + Estados estado = Estados.VERBO; |
| 58 | + |
| 59 | + for (Sentence sentence : document.getSentences()) { |
| 60 | + int i = 0; |
| 61 | + List<Token> tokens = sentence.getTokens(); |
| 62 | + while (i < tokens.size()) { |
| 63 | + System.out.print(estado + " "); |
| 64 | + Token token = sentence.getTokens().get(i); |
| 65 | + if (token.getPOSTag().startsWith("v-")) { |
| 66 | + for (String v : verbs) { |
| 67 | + if (Arrays.asList(token.getLemmas()).contains(v)) { |
| 68 | + estado = Estados.SUBSTANTIVO; |
| 69 | + } |
| 70 | + } |
| 71 | + } else if (estado == Estados.SUBSTANTIVO && token.getPOSTag().startsWith("n")) { |
| 72 | + Class classe = null; |
| 73 | + for (Class c : classOfInterest) { |
| 74 | + GumgaNLPThing gumgaNPLThing = (GumgaNLPThing) c.getAnnotation(GumgaNLPThing.class); |
| 75 | + String nome = (NO_NAME.equals(gumgaNPLThing.value()) ? c.getSimpleName() : gumgaNPLThing.value()).toLowerCase(); |
| 76 | + if (nome.equals(token.getLexeme())) { |
| 77 | + currentObject = c.newInstance(); |
| 78 | + toReturn.add(currentObject); |
| 79 | + estado = Estados.ATRIBUTOS; |
| 80 | + } |
| 81 | + } |
| 82 | + } else if (estado == Estados.ATRIBUTOS && token.getPOSTag().startsWith("n")) { |
| 83 | + try { |
| 84 | + currentField = mapAllFields(currentObject.getClass()).get(token.getLexeme()); //currentObject.getClass().getDeclaredField(token.getLexeme());tod |
| 85 | + estado = Estados.VALOR_ATRIBUTO; |
| 86 | + } catch (NullPointerException nfe) { |
| 87 | + |
| 88 | + } |
| 89 | + } else if (estado == Estados.VALOR_ATRIBUTO && (!token.getPOSTag().startsWith("prp"))) { |
| 90 | + try { |
| 91 | + currentField.setAccessible(true); |
| 92 | + currentField.set(currentObject, currentField.getType().getConstructor(String.class).newInstance(token.getLexeme().toLowerCase())); |
| 93 | + estado = Estados.ATRIBUTOS; |
| 94 | + } catch (Exception ex) { |
| 95 | + ex.printStackTrace(); |
| 96 | + } |
| 97 | + } |
| 98 | + i++; |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | + System.out.println("---------->" + toReturn); |
| 103 | + return toReturn; |
| 104 | + |
| 105 | + } |
| 106 | + |
| 107 | + private Map<String, Field> mapAllFields(Class clazz) { |
| 108 | + Map<String, Field> toReturn; |
| 109 | + if (clazz.getSuperclass().equals(Object.class)) { |
| 110 | + toReturn = new HashMap<>(); |
| 111 | + } else { |
| 112 | + toReturn = mapAllFields(clazz.getSuperclass()); |
| 113 | + } |
| 114 | + Field[] declaredFields = clazz.getDeclaredFields(); |
| 115 | + for (Field f : declaredFields) { |
| 116 | + String label = f.getName(); |
| 117 | + if (f.isAnnotationPresent(GumgaNLPThing.class)) { |
| 118 | + GumgaNLPThing annotation = f.getAnnotation(GumgaNLPThing.class); |
| 119 | + label = annotation.value(); |
| 120 | + } |
| 121 | + toReturn.put(label, f); |
| 122 | + } |
| 123 | + return toReturn; |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + private void print(Document document) { |
| 128 | + StringBuilder output = new StringBuilder(); |
| 129 | + for (Sentence sentence : document.getSentences()) { |
| 130 | + output.append("Sentence: ").append(sentence.getText()).append("\n"); |
| 131 | + output.append(" Tokens: \n"); |
| 132 | + for (Token token : sentence.getTokens()) { |
| 133 | + String lexeme = token.getLexeme(); |
| 134 | + String lemmas = Arrays.toString(token.getLemmas()); |
| 135 | + String pos = token.getPOSTag(); |
| 136 | + String feat = token.getFeatures(); |
| 137 | + output.append(String.format(" %-10s %-12s %-6s %-10s\n", lexeme, lemmas, pos, feat)); |
| 138 | + } |
| 139 | + output.append(" Chunks: "); |
| 140 | + for (Chunk chunk : sentence.getChunks()) { |
| 141 | + output.append("[").append(chunk.getTag()).append(": "); |
| 142 | + for (Token innerToken : chunk.getTokens()) { |
| 143 | + output.append(innerToken.getLexeme()).append(" "); |
| 144 | + } |
| 145 | + output.append("] "); |
| 146 | + } |
| 147 | + output.append("\n"); |
| 148 | + output.append(" Shallow Structure: "); |
| 149 | + for (SyntacticChunk structure : sentence.getSyntacticChunks()) { |
| 150 | + output.append("[").append(structure.getTag()).append(": "); |
| 151 | + for (Token innerToken : structure.getTokens()) { |
| 152 | + output.append(innerToken.getLexeme()).append(" "); |
| 153 | + } |
| 154 | + output.append("] "); |
| 155 | + } |
| 156 | + output.append("\n"); |
| 157 | + } |
| 158 | + System.out.println(output.toString()); |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments