Quiero identificar lo siguiente como HABILIDAD usando TokensRegexNERAnnotator de stanfordNLP.
AREAS OF EXPERTISE Areas of Knowledge Computer Skills Technical Experience Technical Skills
Hay muchas más secuencias de texto como la anterior.
Código -
Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, ner"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); pipeline.addAnnotator(new TokensRegexNERAnnotator("./mapping/test_degree.rule", true)); String[] tests = {"Bachelor of Arts is a good degree.", "Technical Skill is a must have for Software Developer."}; List tokens = new ArrayList<>(); // traversing each sentence from array of sentence. for (String txt : tests) { System.out.println("String is : " + txt); // create an empty Annotation just with the given text Annotation document = new Annotation(txt); pipeline.annotate(document); List<CoreMap> sentences = document.get(SentencesAnnotation.class); /* Next we can go over the annotated sentences and extract the annotated words, Using the CoreLabel Object */ for (CoreMap sentence : sentences) { for (CoreLabel token : sentence.get(TokensAnnotation.class)) { System.out.println("annotated coreMap sentences : " + token); // Extracting NER tag for current token String ne = token.get(NamedEntityTagAnnotation.class); String word = token.get(CoreAnnotations.TextAnnotation.class); System.out.println("Current Word : " + word + " POS :" + token.get(PartOfSpeechAnnotation.class)); System.out.println("Lemma : " + token.get(LemmaAnnotation.class)); System.out.println("Named Entity : " + ne); } }Mi archivo de reglas de expresiones regulares es:
$SKILL_FIRST_KEYWORD = "/área de/|/áreas de/|/técnica/|/informática/|/profesional/" $SKILL_KEYWORD = "/conocimiento/|/habilidad/|/habilidades/|/pericia/|/experiencia/"
fichas = { tipo: "CLASE", valor: "edu.stanford.nlp.ling.CoreAnnotations$TokensAnnotation" }
{ ruleType: "tokens", patrón: ($SKILL_FIRST_KEYWORD + $SKILL_KEYWORD), resultado: "SKILL" }
Recibo el error ArrayIndexOutOfBoundsException . Supongo que hay algo mal con mi archivo de reglas. ¿Puede alguien señalarme dónde estoy cometiendo un error?
Salida deseada -
ÁREAS DE EXPERIENCIA - HABILIDAD
Áreas de Conocimiento - HABILIDAD
Habilidades Informáticas - HABILIDAD
y así.
Gracias por adelantado.
Debería usar TokensRegexAnnotator, no TokensRegexNERAnnotator.
Deberías revisar estos hilos para más información:
Reglas TokensRegex para obtener la salida correcta para las entidades con nombre
Respuesta arriba aceptada por @StanfordNLPHelp, me ayudó a resolver este problema. Todo el crédito va para él/ella.
Solo estoy concluyendo cómo se vería el código final para obtener la salida en el formato deseado con la esperanza de que ayude a alguien.
Primero cambié en el archivo de reglas
$SKILL_FIRST_KEYWORD = "/area of|areas of|Technical|computer|professional/" $SKILL_KEYWORD = "/knowledge|skill|skills|expertise|experience/"
Luego en código
props.put("annotators", "tokenize, ssplit, pos, lemma, ner"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); for (String txt : tests) { System.out.println("String is : " + txt); // create an empty Annotation just with the given text Annotation document = new Annotation(txt); pipeline.annotate(document); List<CoreMap> sentences = document.get(SentencesAnnotation.class); Env env = TokenSequencePattern.getNewEnv(); env.setDefaultStringMatchFlags(NodePattern.CASE_INSENSITIVE); env.setDefaultStringPatternFlags(Pattern.CASE_INSENSITIVE); CoreMapExpressionExtractor extractor = CoreMapExpressionExtractor.createExtractorFromFiles(env, "test_degree.rules"); for (CoreMap sentence : sentences) { List<MatchedExpression> matched = extractor.extractExpressions(sentence); for(MatchedExpression phrase : matched){ // Print out matched text and value System.out.println("MATCHED ENTITY: " + phrase.getText() + " VALUE: " + phrase.getValue().get()); } } }