1

これは簡単なはずですが、何らかの理由で、ファイルを SD カードにダウンロードした後にファイル内の単語を数えようとすると、数がずれているように見えます。また、発生回数が多いほど、結果がずれているように見えます。Microsoft Word を使用して出現回数を確認します (大文字と小文字を区別せず、単語全体のみを使用)。発生回数をテストするには、以下の「the_counter」変数を使用します。また、ダウンロードに問題がなく、完全なファイルが SD カードにダウンロードされていることも確認しました。これは私を狂わせています-私はここでWordが間違っているとは思わないので、以下の私のコードで何が間違っている可能性がありますか?

問題の原因となっているのは、ファイル内の空白または特殊文字である可能性があります。これを確認するためにファイルをクリーンアップする方法はありますか?

//Find the directory for the SD Card using the API
        File sdcard = Environment.getExternalStorageDirectory();

        //Get the text file
        File file = new File(sdcard,TEMP_FILE);

        //Read text from file
        //StringBuilder text = new StringBuilder();
        m_tree = new Tree();
        int i=0;
        BufferedReader br = null;
        long the_counter=0;
        try {
            br = new BufferedReader(new FileReader(file));
            String line;
            String []arLine;
            while ((line = br.readLine()) != null) {
                //get each word in line
                if(line.length()==0)
                    continue;
                arLine = line.split("\\s+");

                //now add each word to search tree
                for(i=0;i< arLine.length;++i){
                    m_tree.insert(arLine[i]);
                    if(arLine[i].equalsIgnoreCase("a"))
                        ++the_counter;
                }
            }
           m_sTest = Long.toString(the_counter) ;
           br.close();

コードを編集して、1 行ごとに各文字を読み取り、単語を手動で作成しました。それでも同じ結果が得られます。

 br = new BufferedReader(new FileReader(file));
            String line;
            String []arLine;
            StringBuilder word = new StringBuilder();
            while ((line = br.readLine()) != null) {
                //check for word at end of last line
                if(word.length()>0){
                    m_tree.insert(word.toString());
                    word.setLength(0);
                }
                char[] lineChars = new char [line.length()];
                line.getChars(0,line.length(),lineChars,0);

                for(char c: lineChars){
                    if(c== ' '){
                        //if we have a word then store and clear then move on
                        if(word.length()>0){
                            m_tree.insert(word.toString());
                            word.setLength(0);
                        }
                    }
                    else{
                        word.append(c);
                    }
                }
4

1 に答える 1

0

これは、単語間の特殊文字を考慮していなかったという問題です。つまり、 this-is-four-words であり、 one ではありません。それが適切な文法や書き方であるかどうかさえわかりませんが、それはこのファイルにあり、確かに私のカウントを台無しにしました.

于 2014-07-26T05:05:50.267 に答える