简体   繁体   English

在Android应用中设计一种算法以“倒退到上一句话”

[英]Designing an algorithm to “rewind to previous sentence” in android app

I'm making an app that flashes one word at a time on the screen. 我正在制作一个在屏幕上一次闪烁一个字的应用程序。 These words are currently created by making an array of strings called words[] and splitting up the main string (toRead) into words using split(). 当前,通过创建称为words []的字符串数组并使用split()将主字符串(toRead)拆分为单词来创建这些单词。

I've also made a Sentences class that is created using Breakiterator to split toRead into sentences. 我还制作了一个使用Breakiterator将toRead拆分为句子的Sentences类。 I store start and end for each sentence. 我存储每个句子的开始和结束。 However these start and end integers are the index of the characters in the toRead string. 但是,这些开始和结束整数是toRead字符串中字符的索引。 But my program sets the text by using the words[] array which indexes words. 但是我的程序通过使用word []数组设置文本来索引单词。

Ex: One sentence could start at index 0 and end at index 20. (20 character long sentence with variable amount of words). 例如:一个句子可以从索引0开始,到索引20结束。(20个字符长的句子,单词数量可变)。 And it may contain 5 words. 它可能包含5个字。 So words[0] to word[4] contains this sentence. 因此,单词[0]至单词[4]包含此句子。

What I'd like to do is to get the index of the word showing while my app is running, and figure out what sentence that word is in. Then when the "rewind sentence" button is pressed the index changes to the index of the first word in that sentence (or perhaps the sentence before). 我想做的是获取在我的应用程序运行时显示的单词的索引,并弄清楚该单词在哪个句子中。然后,当按下“倒带句子”按钮时,索引将变为该单词的索引。该句子(或前面的句子)中的第一个单词。

I need some help coming up with the algorithm for doing this. 我需要一些帮助来解决此问题。 I can think of some ways that would be very high in time complexity but I need something more efficient. 我可以想到一些时间复杂度很高的方法,但我需要更有效的方法。 Maybe using a hash function if necessary. 如有必要,也许使用哈希函数。

Edit: I'll try to be more clear. 编辑:我会尝试更清楚。

The user will enter some sort of a string (the use case for my app is an article or a long essay). 用户将输入某种字符串(我的应用程序的用例是一篇文章或一篇长论文)。 The string is called toRead . 该字符串称为toRead Then I have a function that takes an input of toRead and creates a String[] called words that has all of the words in the toRead string. 然后,我有一个函数,该函数接受toRead的输入并创建一个名为word的String [],该字符串具有toRead字符串中的所有单词。 So the first word is words[0], etc. In my app when you hit the "play" button it cycles through all the words in words[] one at a time, in order of index at a chosen words-per-minute. 因此,第一个单词是words [0]等。在我的应用中,当您按下“播放”按钮时,它会一次循环遍历words []中的所有单词,并按每分钟选定单词的索引顺序。 While this is going on, I'd like my rewind button to be able to know what sentence the user is on, and return to the beginning of that sentence (while continuing to cycle through the words) and if pressed again I'd like it to return to a sentence before that, and if again to a sentence before that, etc. 在进行此操作的同时,我希望我的快退按钮能够知道用户正在使用的句子,然后返回到该句子的开头(同时继续循环显示各个单词),如果再次按下,我想它返回到之前的句子,如果再次返回到之前的句子,依此类推。

So far I've been able to split the toRead string into sentences using BreakIterator and a Sentence class that just has a start and end. 到目前为止,我已经能够使用BreakIterator和仅具有开始和结束的Sentence类将toRead字符串拆分为句子。 So basically I have a bunch of Sentence objects with index values for where they begin and end, but these index values are the character numbers and not the word numbers (which is what I'm using to display each word). 因此,基本上,我有一堆带有索引值的Sentence对象,它们的开始和结束位置都在其中,但是这些索引值是字符编号而不是单词编号(这是我用来显示每个单词的内容)。

HashMaps might be a way to go. HashMaps可能是一种方法。

Map<String,String> myStrings = new HashMap<String,String>

String[] sentences = new String[256]; 
sentences[0] = "Hi how are you"; // etc etc blah blah
String[] words = new String[4];
words[0] = "Hi";  // etc etc

myStrings.add(sentence[0], words[0]);
myStrings.add(sentence[0], words[1]); // etc 

you can determine if a word exists in any given sentence like this 您可以确定某个给定句子中是否存在这样的单词

for (String value: myStrings.keySet()) {
    if (myStrings.get(value).has(words[index_you_want]) {   
         // do something
    }
}

Check out HashMaps in more detail and I'll bet that you can solve the rest of your problems 仔细检查HashMaps,我敢打赌,您可以解决其余的问题

What you're doing is basically translating a 2 dimensional coordinates (sentences, word) to a linear coordinate in the original string. 您要做的基本上是将二维坐标(句子,单词)转换为原始字符串中的线性坐标。

You can create a list of indices to the start of words, it's essentially a running sum of the length of the words: 您可以创建到单词开头的索引列表,它实质上是单词长度的总和:

String s = "Andy builds car. Data entry fan.";
String[] words = s.split(" ");
int[] words_indexes = new int[words.length];
for (int i = 1; i < words.length; i++) {
    words_indexes[i] = words_indexes[i - 1] + words[i - 1].length + 1;
}

Then finding the position of the third word of the second sentence in the original string would be just: 然后找到第二个句子的第三个单词在原始字符串中的位置将是:

words_indexes[index_of_sentence_in_words_indexes + index_of_word_in_sentence]

You should also be able to build the words_indexes array lazily as you go displaying the words to the user, that way you don't need to parse the entire thing first (although you really have to have a really, really long string for it to matter, even the longest essays can probably be indexed in a blink of an eye). 您还应该能够在向用户显示单词时懒惰地构建words_indexes数组,这样就不必首先解析整个内容(尽管您确实必须有一个非常长的字符串才能重要的是,即使最长的论文也可以在眨眼间被索引。

I'm still a bit confused about what you are trying to do, so this suggestion may be way off, but how about using a Zipper pattern? 我对您要执行的操作仍感到困惑,因此此建议可能会过时,但是如何使用Zipper模式?

You can get check the Functional Java API http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/data/Zipper.html 您可以查看功能Java API http://functionaljava.googlecode.com/svn/artifacts/3.0/javadoc/fj/data/Zipper.html

And for an explanation of what it is: What is the Zipper data structure and should I be using it? 并解释一下它是什么:Zipper数据结构什么,我应该使用它吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM