返回下一个N字的有效方式&a​​mp;以前的N个词来显示

时间:2016-07-04 19:15:57

标签: java android

我希望允许用户一次查看字符串,N个字。它们可以向前和向后移动,这样向后将显示前N个单词。另外,由于我们受到屏幕空间的限制,所以我想只选择适合的单词,最多n个(整个单词,不...)。

所以,我创建了一个不太漂亮的方法:

import java.util.ArrayList;

public class Test {

    static String text = "This is a dummny sentence. I am a dummy sentence two. This is dummy sentence 3. This is a dummy sentence";

    static int currentWordIndex = 0;

    public static void main(String[] args) {

        String nextWords = getNextWords(5, 30);
        System.out.println("Sentence: " + nextWords);
        System.out.println("Index: " + currentWordIndex);

        nextWords = getNextWords(3, 30);
        System.out.println("Sentence: " + nextWords);
        System.out.println("Index: " + currentWordIndex);

        nextWords = getNextWords(3, 30);
        System.out.println("Sentence: " + nextWords);
        System.out.println("Index: " + currentWordIndex);

        nextWords = getNextWords(3, 30);
        System.out.println("Sentence: " + nextWords);
        System.out.println("Index: " + currentWordIndex);


        String prevWords = getPreviousWords(3, 30);
        System.out.println("Previous Sentence: " + prevWords);
        System.out.println("Index: " + currentWordIndex);

        prevWords = getPreviousWords(3, 30);
        System.out.println("PreviousSentence: " + prevWords);
        System.out.println("Index: " + currentWordIndex);

        prevWords = getPreviousWords(3, 30);
        System.out.println("PreviousSentence: " + prevWords);
        System.out.println("Index: " + currentWordIndex);

        nextWords = getNextWords(3, 30);
        System.out.println("Sentence: " + nextWords);
        System.out.println("Index: " + currentWordIndex);

        nextWords = getNextWords(3, 30);
        System.out.println("Sentence: " + nextWords);
        System.out.println("Index: " + currentWordIndex);


    }

    public static String getPreviousWords(int n, int maxCharacterLength) {
        ArrayList<String> initialSelection = new ArrayList();
        String[] splitWords = text.split(" ");
        int charCount = 0;
        int maxN = currentWordIndex - n;
        if(maxN < 0) {
            maxN = 0;
        }
        for (int i = maxN; i < currentWordIndex; i++) {
            if(i > splitWords.length) {
                break;
            }
            initialSelection.add(splitWords[i]);
            charCount += splitWords[i].length();
        }

        if (charCount > maxCharacterLength) {
            charCount = 0;
            for (int x = 0; x < initialSelection.size(); x++) {
                if (initialSelection.size() == 0)
                    return null;
                initialSelection.remove(initialSelection.size() - 1);
                for (String s : initialSelection) {
                    charCount += s.length();
                }
                if (charCount <= maxCharacterLength) {
                    break;
                }
            }
        }
        StringBuilder sb = new StringBuilder();
        for (String s : initialSelection) {
            sb.append(s + " ");
        }
        if(currentWordIndex - initialSelection.size() < 0) {
            currentWordIndex = 0;
        } else {
            currentWordIndex -= initialSelection.size();
        }
        return sb.toString().substring(0, sb.length());
    }

    /**
     * Obtains the next n number of words given the maximum number of characters that can fit on the screen.
     * If not all words fit on the screen
     *
     * @param n                  number of words
     * @param maxCharacterLength of text that can fit on the screen.
     * @return next words
     */
    public static String getNextWords(int n, int maxCharacterLength) {
        ArrayList<String> initialSelection = new ArrayList();
        int charCount = 0;
        String[] splitWords = text.split(" ");
        int maxN = currentWordIndex + n;
        if (maxN > splitWords.length - 1) {
            maxN = splitWords.length - 1;
        }
        // add the words
        for (int i = currentWordIndex; i < maxN; i++) {
            if (i > splitWords.length - 1)
                break;
            initialSelection.add(splitWords[i]);
            charCount += splitWords[i].length();
        }

        // if there's too many, remove the last word until everything fits
        if (charCount > maxCharacterLength) {
            charCount = 0;
            for (int x = 0; x < initialSelection.size(); x++) {
                if (initialSelection.size() == 0)
                    return null;
                initialSelection.remove(initialSelection.size() - 1);
                for (String s : initialSelection) {
                    charCount += s.length();
                }
                if (charCount <= maxCharacterLength) {
                    break;
                }
            }
        }

        // add back into a string
        StringBuilder sb = new StringBuilder();
        for (String s : initialSelection) {
            sb.append(s + " ");
        }

        // update the index/pivot based on the words selected
        if (currentWordIndex + initialSelection.size() < splitWords.length - 1) {
            currentWordIndex += initialSelection.size();
        } else {
            currentWordIndex = splitWords.length;
        }
        return sb.toString().substring(0, sb.length());
    }

}

这给出了这个输出:

Next Sentence: One two three four five. 
Index: 5
Next Sentence: Six Seven Either, 
Index: 8
Next Sentence: Nine, Ten, Eleven. 
Index: 11
Next Sentence: Twelve thirdteen fourteen 
Index: 14
Previous Sentence: Twelve thirdteen fourteen 
Index: 11
PreviousSentence: Nine, Ten, Eleven. 
Index: 8
PreviousSentence: Six Seven Either, 
Next Index: 5
Next Sentence: Six Seven Either, 
Index: 8
Next Sentence: Nine, Ten, Eleven. 
Index: 11

哪个不对。它似乎是下一个和前一个工作 - 只是在它们之间进行更改时。它应该说:

Next Sentence: One two three four five. 
Index: 5
Next Sentence: Six Seven Either, 
Index: 8
Next Sentence: Nine, Ten, Eleven. 
Index: 11
Next Sentence: Twelve thirdteen fourteen 
Index: 14
Previous Sentence: Nine, Ten, Eleven.
Index: 11
PreviousSentence: Six, Seven, Either. 
Index: 8
PreviousSentence: three four five, 
Index: 5
Next Sentence: Six Seven Either, 
Index: 8
Next Sentence: Nine, Ten, Eleven. 
Index: 11

我明白问题是什么:在getNext“递增之后,它在下一个句子的开头设置标记。因此当调用'getPrevious'时,它调用,最后一个句子(当前已经显示的那个) 。

所以我认为解决方案是同时具有开始和结束标记 - 标记句子索引的开头和结尾。

但我想知道是否有人有更优雅的解决方案来解决这个问题?

2 个答案:

答案 0 :(得分:2)

试试这个

 public class Test {

    static String text = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen";

    static int currentWordIndex = 0;
    private static List<String> list = new LinkedList<String>();

    public void createTokens() {
        String[] splitWords = text.split(" ");
        for (int i = 0; i < splitWords.length; i++) {
            list.add(splitWords[i]);
        }
    }

    public String getPreviousWords(int n, int maxCharacterLength) {
        StringBuilder strBuilder = new StringBuilder();
        int characterCount = 0;
        int item_size = 0;
        // you need to check this negative or not
        currentWordIndex = currentWordIndex - n; 
        ListIterator<String> nextIter = list.listIterator(currentWordIndex);
        while (nextIter.hasPrevious() && item_size < n) {
            String elem = (String) nextIter.previous();
            characterCount += elem.length();
            if (characterCount > maxCharacterLength) {
                break;
            }
            strBuilder.append(elem);
            strBuilder.append(" ");
            item_size++;
        }

        return strBuilder.toString();
    }

    /**
     * Obtains the next n number of words given the maximum number of characters
     * that can fit on the screen. If not all words fit on the screen
     *
     * @param n
     *            number of words
     * @param maxCharacterLength
     *            of text that can fit on the screen.
     * @return next words
     */
    public String getNextWords(int n, int maxCharacterLength) {
        StringBuilder strBuilder = new StringBuilder();
        int characterCount = 0;
        int item_size = 0;
        ListIterator<String> nextIter = list.listIterator(currentWordIndex);
        while (nextIter.hasNext() && item_size < n) {
            String elem = (String) nextIter.next();
            characterCount += elem.length();
            if (characterCount > maxCharacterLength) {
                break;
            }
            strBuilder.append(elem);
            strBuilder.append(" ");
            item_size++;
        }
        return strBuilder.toString();

    }

}

App.java

    public class App {

    public static void main(String[] args) {
            Test t = new Test();
            t.createTokens();
            String nextWords = t.getNextWords(5, 30);
            System.out.println("Sentence: " + nextWords);
            String nextWords2 = t.getNextWords(5, 30);
            System.out.println("Sentence: " + nextWords2);
            String nextWords3 = t.getNextWords(5, 30);
            System.out.println("Sentence: " + nextWords3);

            String previousWords = t.getPreviousWords(5, 30);
            System.out.println("Sentence: " + previousWords);
    }
}

输出:

Sentence: one two three four five
Sentence: six seven eight nine ten 
Sentence: eleven twelve thirteen fourteen 
Sentence: ten nine eight seven six 

答案 1 :(得分:0)

因为我想让它与@mithat konuk解决方案合作。但我认为这可能会更好地与子列表。

public class Test2 {

    public static void main(String[] args) {
        Test2 t = new Test2();
        t.createTokens();
        String nextWords = t.getNextWords(5, 30);
        System.out.println("Sentence: " + nextWords);
        String nextWords2 = t.getNextWords(5, 30);
        System.out.println("Sentence: " + nextWords2);
        String nextWords3 = t.getNextWords(5, 30);
        System.out.println("Sentence: " + nextWords3);

        String previousWords = t.getPreviousWords(3, 10);
        System.out.println("Previous Sentence: " + previousWords);
        previousWords = t.getPreviousWords(3, 10);
        System.out.println("Previous Sentence: " + previousWords);
        previousWords = t.getPreviousWords(3, 10);
        System.out.println("Previous Sentence: " + previousWords);
        previousWords = t.getPreviousWords(3, 10);
        System.out.println("Previous Sentence: " + previousWords);
        previousWords = t.getPreviousWords(3, 10);
        System.out.println("Previous Sentence: " + previousWords);

        nextWords = t.getNextWords(5, 30);
        System.out.println("Sentence: " + nextWords);
    }


    static String text = "I am a cat who sat on a mat and yelled as someone who was fat";

    private static List<String> list = new LinkedList<String>();

    // index of the start of the next sentence (i.e also the end of the sentence that is currently being displayed)
    static int nextWordIndex = 0;
    // index of the first word of the current sentence being displayed.
    static int firstWordIndex = 0;

    public void createTokens() {
        String[] splitWords = text.split(" ");
        for (int i = 0; i < splitWords.length; i++) {
            list.add(splitWords[i]);
        }
    }

    public String getPreviousWords(int n, int maxCharacterLength) {
        StringBuilder strBuilder = new StringBuilder();
        int characterCount = 0;
        int itemSize = 0;
        // you need to check this negative or not
        if(firstWordIndex < 0)
            firstWordIndex = 0;
        ListIterator<String> nextIter = list.listIterator(firstWordIndex);
        while (nextIter.hasPrevious() && itemSize < n) {
            String elem = (String) nextIter.previous();
            characterCount += elem.length();
            if (characterCount > maxCharacterLength) {
                break;
            }
            strBuilder.insert(0, elem + " ");
            itemSize++;
        }

        if(firstWordIndex == 0) {
            firstWordIndex = 0;
            nextWordIndex = itemSize;
        }
        nextWordIndex = firstWordIndex;
        firstWordIndex-=itemSize;

        return strBuilder.toString();
    }

    /**
     * Obtains the next n number of words given the maximum number of characters
     * that can fit on the screen. If not all words fit on the screen
     *
     * @param n
     *            number of words
     * @param maxCharacterLength
     *            of text that can fit on the screen.
     * @return next words
     */
    public String getNextWords(int n, int maxCharacterLength) {
        StringBuilder strBuilder = new StringBuilder();
        int characterCount = 0;
        int itemSize = 0;
        ListIterator<String> nextIter = list.listIterator(nextWordIndex);
        while (nextIter.hasNext() && itemSize < n) {
            String elem = (String) nextIter.next();
            characterCount += elem.length();
            if (characterCount > maxCharacterLength) {
                break;
            }
            strBuilder.append(elem);
            strBuilder.append(" ");
            itemSize++;
        }
        firstWordIndex = nextWordIndex;
        nextWordIndex+=itemSize;
        return strBuilder.toString();

    }
}

输出:

  

句子:我是一个句子:坐在垫子上的猫和句子:大喊   作为先前句子的人:垫子和前句子:   谁坐在上一句话:我是一只猫以前的句子:我   上一句:句子:我是一只猫