【问题标题】:Finding the longest sentence by word from a text file从文本文件中逐字查找最长的句子
【发布时间】:2018-03-31 18:02:51
【问题描述】:

我能够在文本文件中找到最长的单词,现在我正在努力逐字查找最长的句子。该算法类似于搜索最长的单词吗? 这是我查找最长单词的代码:

    public static int getLongestWord() throws FileNotFoundException {
    String longestWord = "";
    String current;
    Scanner scan = new Scanner(new File("t1.txt"));

    while (scan.hasNext()) {
        current = scan.next();
        if (current.length() > longestWord.length()) {
            longestWord = current;
        }

    }
    scan.close();
    return longestWord.length();
}

更新! 谢谢您的帮助。我能够得到最长的单词,现在我正在计算最长句子中的单词数。不知何故,柜台有点不对劲。 这是我所拥有的:

    public static String getLongestSentence() throws FileNotFoundException {
    int numWords = 0;
    String longestSentence = "";
    String currentSentence = "";
    Scanner scan = new Scanner(new File("t1.txt"));

    while (scan.hasNext()) {

        currentSentence = getNextSentence(scan);

        if (currentSentence.length() > longestSentence.length()) {
            longestSentence = currentSentence;
        }
    }
    scan.close();
    String[] wordList = longestSentence.split("\\s+");
    numWords += wordList.length;
    System.out.println(longestSentence);
    System.out.println("Number of words in this sentence: " + numWords);
    return longestSentence;
}

private static String getNextSentence(Scanner scan) {
    String sentence = "";
    while (scan.hasNext()) {
        sentence += " " + scan.next();
        if (sentence.contains("."))
            break;
    }
    return sentence;
}

我的文本文件中最长的句子包含 30 个单词,但我的计数器减 1,表示它有 31 个单词。知道为什么吗?谢谢。

【问题讨论】:

  • 您应该将文件打开代码与最长单词查找代码分开。
  • 好的!感谢您的提示!
  • 算法类似吗?好吧,保存当前最长并用新的最长替换它的概念是相同的。显然,检测哪个最长的代码会有所不同。
  • 对。我正在考虑将整个句子存储到 arrayList 中,并创建一个 for 循环来查找最长的句子。这是一个好主意吗?我怎么能用单词而不是字符来计数?

标签: java algorithm search


【解决方案1】:

第 1 部分:将所有文件内容读入变量

File file = new File("sentences.txt");
FileReader in = new FileReader(file);
BufferedReader br = new BufferedReader(in);
String line = br.readLine();
String fullContent = "";
while(line != null && line.length() > 0) {
    fullContent += line;
    line = br.readLine();
}

第 2 部分:声明两种方法

这会将整个文件内容拆分为不同的句子,假设句子结尾是'.'

private static String[] getSentences(String file) {             
     return file.split("\\.");          
}

这将找到最大长度的单词

public static int getMaxWordLength(String sentence, int maxWordLength) {
    for(String word : sentence.split(" ")) {
        String wordLength = word.trim().length();
        if(wordLength > maxWordLength) {
            maxWordLength = wordLength; 
        }
    }
    return maxWordLength;
}

第 3 部分:整合两种方法找到最大单词的句子

int length = 0;
String largestSentence = null;
for(String sentence : getSentences(fullContent)) {
    int maxWordLength = 0;
    maxWordLength = getMaxWordLength(sentence, maxWordLength);
    if(maxWordLength > length) {
        largestSentence = sentence;
        length = maxWordLength;
    }
}
System.out.println(largestSentence);

【讨论】:

  • 我认为这是最好的方法,应该被接受。
【解决方案2】:

你必须考虑两件事:

你认为任何句子都以'.'结尾

那么你可以选择这样的东西

public static String getLongestSentence() throws FileNotFoundException {
    String longestSentence = "";
    String currentSentence = "";    
    Scanner scan = new Scanner(new File("t1.txt"));

    while (scan.hasNext()) {

        currentSentence = getNextSentence(scan);

        if (currentSentence.length() > longestSentence.length()) {
              longestSentence = currentSentence;
        }

    } 
    scan.close();
    return longestSentence;
}

private static String getNextSentence(Scanner scan) {
    String sentence = "";
    while(scan.hasNext()){
        sentence += scan.next();
        if(sentence.contains(".")) break;
    }
    return sentence;
}

如果您想考虑更多现实生活中的案例,例如省略号、ASCII 怪异代码、其他语言,那么...您将不得不看看像 Aylien 这样的文本分析器因为从头开始会给你带来无限的头痛。

【讨论】:

  • 非常感谢!我能够得到文本文件中最长的句子。但是,当我在控制台上打印它时,似乎所有的空格都被删除了,这意味着所有的单词都在一起了。输出如下:USforceshuntingelusivealQaedaandTalibanfightersineastern... 等等。您如何建议按单词而不是字符来计算最长的句子?
  • 哦是的..你可以用sentence += " " + scan.next();替换sentence += scan.next();
  • 哦,你想得到最长的句子逐字,我没看到。此代码确实会按字符获得最长的句子。您可以做的不是返回句子,而是返回单词的数量(只需在循环中增加一个计数器)。然后比较你拥有的所有计数器值
  • 我刚刚更新了我的帖子。这种方法行得通吗?谢谢!
  • 计数器减 1,因为整个句子以空格开头(因为我告诉你要更改的代码)。由于情况总是如此,您可以安全地将计数器减 1,以使其正确
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-08-01
  • 2015-01-02
  • 1970-01-01
  • 2014-09-26
相关资源
最近更新 更多