2016-10-18 472 views
1

所以我正在做这个过去的样本期末考试,其中问题要求从文件读取输入,然后将它们处理成文字。 句子的结尾用任何以三个字符之一结尾的单词来标记。 ? !Java处理来自文件的输入

我能够为此写一个代码,但我只能用将它们拆分成句子使用扫描仪类并使用use.Delimiter。我想将它们处理成单词,看看在上面的句子分隔符中是否有单词结束,然后我将停止在句子类中添加单词。 任何帮助将不胜感激,因为我正在自己学习这一点,这就是我想出的。我的代码在这里。

File file = new File("finalq4.txt"); 
    Scanner scanner = new Scanner(file); 
    scanner.useDelimiter("[.?!]"); 
    while(scanner.hasNext()){ 
     sentCount++; 
     line = scanner.next(); 
     line = line.replaceAll("\\r?\\n", " "); 
     line = line.trim(); 
     StringTokenizer tokenizer = new StringTokenizer(line, " "); 
     wordsCount += tokenizer.countTokens(); 
     sentences.add(new Sentence(line,wordsCount)); 
     for(int i = 0; i < line.replaceAll(",|\\s+|'|-","").length(); i++){ 
      currentChar = line.charAt(i); 
      if (Character.isDigit(currentChar)) { 
      }else{ 
       lettersCount++; 
      } 
     } 
    } 

我在此代码正在做的是,我分裂投入使用分隔符方法的句子,然后计算的话,整个文件的信件,并存储在一个句子类的句子。

如果我想分解成单词,我怎么能做到这一点,而不使用扫描仪类。

从一些,我要处理的文件输入的是在这里

文字下面是基于密码的维基百科页面上!

密码学是隐藏信息的实践和研究。在现代,密码学被认为是数学和计算机科学的分支,并且与信息论,计算机安全和工程学紧密相关。加密技术用于技术领域的应用领域:先进的社会;例子包括ATM卡,计算机 密码和电子商务安全性,这都依赖于密码.....

我能在这个问题上进一步阐述,如果它需要解释。

我希望能够做的是不断向单词类添加单词,并在单词在上面的句子分隔符之一结束时停止。然后读另一个词,并继续添加这些词,直到我击中另一个分隔符。

+0

扫描仪很不错。 。你也可以按行读取文件行 –

+0

使用'String.split'怎么样? –

+0

是的扫描仪是好的,它也很容易,而不是很多的编码。 @ΦXocę웃Пepeúpaツ – Saad

回答

0

好了,所以我一直在通过多种技术解决这个问题,办法之一是上面。但是我能够用另一种方法解决这个问题,而不涉及使用Scanner类。这一个更准确,它给了我确切的输出,而在上面,我只有几个字和字母。

try { 
     input = new BufferedReader(new FileReader("file.txt")); 
     strLine = input.readLine(); 
     while(strLine!= null){ 

      String[] tokens = strLine.split("\\s+"); 
      for (int i = 0; i < tokens.length; i++) { 
       if(strLine.isEmpty()){ 
        continue; 
       } 
       String s = tokens[i]; 
       wordsJoin += tokens[i] + " "; 

       wordCount += i; 
       int len = s.length(); 
       String charString = s.replaceAll("[^a-zA-Z ]", ""); 
       for(int k =0; k<charString.length(); k++){ 
        currentChar = charString.charAt(k); 
        if(Character.isLetter(currentChar)){ 
         lettersCount++; 
        } 
       } 
       if (s.charAt(len - 1) == '.' || s.charAt(len - 1) == '?' || s.charAt(len - 1) == '!') { 
        sentences.add(new Sentence(wordsJoin, wordCount)); 
        sentCount++; 
        numOfWords += countWords(wordsJoin); 
        wordsJoin = ""; 
        wordCount = 0; 
       } 
      } 
      strLine = input.readLine(); 
     } 

这可能是任何人都做了同样的问题有用的或只是需要如何从一个文本文件数的字母,单词和句子的想法。

1

下面的代码片段应制定

public static void main(String[] args) throws FileNotFoundException { 
    File file = new File("final.txt"); 
    Scanner scanner = new Scanner(file); 
    scanner.useDelimiter("[.?!]"); 
    int sentCount; 
    List<Sentence> sentences = new ArrayList<Sentence>(); 
    while (scanner.hasNext()) { 
     String line = scanner.next(); 
     if (!line.equals("")) { /// for the ... in the end 
      int wordsCount = 0; 
      String[] wordsOfLine = line.split(" "); 
      for (int i = 0; i < wordsOfLine.length; i++) { 
       wordsCount++; 
      } 
      Sentence sentence = new Sentence(line, wordsCount); 
      sentences.add(sentence); 
     } 
    } 
} 



public class Sentence { 
    String line = ""; 
    int wordsCount = 0; 
    public Sentence(String line, int wordsCount) { 
     this.line = line; 
     this.wordsCount=wordsCount; 
} 
+0

如果我正在关注你的程序,那么这行就会得到一个句子,所以它和我的程序基本相同。我想将它分解成单词,然后将每个单词添加到句子课程中,并在结束时停止。 – Saad

+0

我不认为你的程序正在这样做。 – Saad

1

您可以使用一个缓冲的读者阅读文件的每一行。然后用split方法将每一行分割成一个句子,最后让这些单词用相同的方法分割句子。最后,它会是这个样子:

BufferedReader br; 
try{ 
    br = new BufferedReader(new File(fileName)); 
}catch (IOException e) {e.printStackTrace();} 
StringBuilder sb = new StringBuilder(); 
String line; 
while((line = br.readLine()) != null){ 
    sb.append(line); 
} 
String[] sentences = sb.toString().split("\\."); 
for(String sentence:sentences){ 
    String word = sentence.split(" "); 
    //Add word to sentence... 
} 
try{ 
    br.close(); 
}catch(IOException e){ 
    e.printStackTrace(); 
}