Last active
July 14, 2020 14:39
-
-
Save catalinghita8/c5f1e0d9c42c888faa76ab31dc899d7d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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