Skip to content

Instantly share code, notes, and snippets.

View catalinghita8's full-sized avatar

Catalin Ghita catalinghita8

View GitHub Profile
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
val props = Properties()
props.setProperty("annotators", "tokenize, ssplit, pos, lemma")
instance = NLPClient(StanfordCoreNLP(props))
private val scope = CoroutineScope(Dispatchers.IO)
override fun onCreate(savedInstanceState: Bundle?) {
...
scope.launch { viewModel.requestInitializeNLP() }
}
private fun requestExtractQuery(bundle: Bundle?) {
val data = bundle?.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)
data?.let { results ->
val resultedText = results[0] // First we set the text result to a TextView
txtSpeechInput.text = resultedText
// Secondly, we send a processing command downstream to the viewModel
// In return, we get a colorized text result with the noun highlighted and we set it again
val result: Spannable = viewModel.requestQuery(resultedText)
txtSpeechInput.text = result
private var speechRecognizer: SpeechRecognizer? = null
...
private fun listenToSpeechInput() {
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context)
speechRecognizer?.setRecognitionListener(object: RecognitionListener {
...
override fun onResults(p0: Bundle?) {
speechRecognizer?.stopListening()
private fun promptSpeechInput() {
val intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
intent.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
)
intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, activity?.application?.packageName?: "")
speechRecognizer?.startListening(intent)
}
@AppScoped
public class MessageRepository {
@Inject
public MessageRepository() {
}
@NonNull
public String getData(){
return "Hello Medium!";
}
public class MessageUiModel {
private final String mMessageText;
public MessageUiModel(String messageText) {
this.mMessageText = messageText;
}
public String getMessageText() {
return mMessageText;
@Module
public abstract class ViewModelModule {
@Binds
@IntoMap
@ViewModelKey(MessageViewModel.class)
abstract ViewModel bindMessageViewModel(MessageViewModel messageViewModel);
@Binds
@AppScoped
abstract ViewModelProvider.Factory bindViewModelFactory(MessageViewModelFactory factory);
@AppScoped
public class MessageViewModelFactory implements ViewModelProvider.Factory {
private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;
@Inject
public MessageViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
this.creators = creators;
}
@SuppressWarnings("unchecked")