Skip to content

Instantly share code, notes, and snippets.

@catalinghita8
Last active July 14, 2020 14:39
Show Gist options
  • Select an option

  • Save catalinghita8/c5f1e0d9c42c888faa76ab31dc899d7d to your computer and use it in GitHub Desktop.

Select an option

Save catalinghita8/c5f1e0d9c42c888faa76ab31dc899d7d to your computer and use it in GitHub Desktop.
private val pipeline: StanfordCoreNLP
...
fun extractRootNoun(text: CharSequence): String? {
val input = text.toString()
val document = Annotation(input)
pipeline.annotate(document)
// Get split sentences
val sentences: List<CoreMap> = document.get(CoreAnnotations.SentencesAnnotation::class.java)
// Get the tokens for the sentences
sentences.flatMap { sentence ->
sentence.get(CoreAnnotations.TokensAnnotation::class.java)
}.forEach { token ->
// Get the text of each token
val word = token.get(CoreAnnotations.TextAnnotation::class.java)
// Get the corresponding POS tag of the token
val pos = token.get(CoreAnnotations.PartOfSpeechAnnotation::class.java)
// Check if the POS tag is matched to a noun
val partOfSpeechTag = pos.toString()
if(partOfSpeechTag == "NN" || partOfSpeechTag == "NNS") {
// We found it, but we want the root form of the token which is the lemma
val lemmatizedValue = token.lemma().toString()
return lemmatizedValue
}
}
return null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment