-1

次のコードがあります。ディレクトリから多くのファイルをハッシュマップに読み込みます。これは私の機能 vecteurです。ステミングを行わないという意味ではややナイーブですが、それは今のところ私の主な関心事ではありません。このデータ構造をパーセプトロン アルゴリズムへの入力として使用する方法を知りたいです。これを言葉の袋と呼んでいると思いますね。

public class BagOfWords 
{
        static Map<String, Integer> bag_of_words = new HashMap<>();

        public static void main(String[] args) throws IOException 
        {
            String path = "/home/flavius/atheism;
            File file = new File( path );
            new BagOfWords().iterateDirectory(file);

            for (Map.Entry<String, Integer> entry : bag_of_words.entrySet()) 
            {
                System.out.println(entry.getKey()+" : "+entry.getValue());
            }

        }

        private void iterateDirectory(File file) throws IOException 
        {
            for (File f : file.listFiles()) 
            {
                if (f.isDirectory()) 
                {    
                    iterateDirectory(file);
                } 
                else 
                {
                    String line; 
                    BufferedReader br = new BufferedReader(new FileReader( f ));

                    while ((line = br.readLine()) != null) 
                    {

                        String[] words = line.split(" ");//those are your words

                        String word;

                        for (int i = 0; i < words.length; i++) 
                        {
                            word = words[i];
                            if (!bag_of_words.containsKey(word))
                            {
                                bag_of_words.put(word, 0);
                            }
                            bag_of_words.put(word, bag_of_words.get(word) + 1);
                        }

                    }

                }
            }
        }
    }

パスが 'atheism' と呼ばれるディレクトリに移動していることがわかります。そこにはスポーツと呼ばれるディレクトリもあります。これら 2 つのクラスのドキュメントを直線的に分離してから、目に見えないテスト ドキュメントをいずれかのカテゴリに分離しようとします。

どうやってするか?それをいかに概念化するか。確かなリファレンス、包括的な説明、またはある種の疑似コードをいただければ幸いです。

ウェブ上で有益で明快な参考文献はあまり見つかりませんでした。

4

2 に答える 2

1

前もっていくつかの語彙を確立しましょう (20 個のニュースグループのデータセットを使用していると思います)。

  • 「クラスラベル」はあなたが予測しようとしているものです。バイナリの場合、これは「無神論」と残りの部分です
  • 分類器に入力する「特徴ベクトル」
  • データセットからの単一の電子メールである「ドキュメント」
  • 文書の一部を「トークン」、通常はユニグラム/バイグラム/トライグラム
  • 「辞書」ベクトルに「許可された」単語のセット

そのため、bag of words のベクトル化アルゴリズムは通常、次の手順に従います。

  1. (すべてのクラス ラベルにわたって) すべてのドキュメントを調べて、すべてのトークンを収集します。これが辞書であり、特徴ベクトルの次元です。
  2. すべてのドキュメントをもう一度確認し、それぞれについて次のことを行います。
    1. 辞書の次元を持つ新しい特徴ベクトルを作成します (たとえば、その辞書の 200 エントリに対して 200)。
    2. そのドキュメント内のすべてのトークンを調べて、特徴ベクトルのこの次元で (このドキュメント内の) 単語数を設定します
  3. これで、アルゴリズムにフィードできる特徴ベクトルのリストができました

例:

Document 1 = ["I", "am", "awesome"]
Document 2 = ["I", "am", "great", "great"]

辞書は次のとおりです。

["I", "am", "awesome", "great"]

したがって、ベクトルとしてのドキュメントは次のようになります。

Document 1 = [1, 1, 1, 0]
Document 2 = [1, 1, 0, 2]

これにより、あらゆる種類の高度な数学を実行して、これをパーセプトロンに入力できます。

于 2015-02-16T11:46:29.063 に答える
0

これは私の元の質問に対する完全かつ完全な回答であり、将来の閲覧者のためにここに投稿されています


次のファイルがあるとします。

  • 無神論/a_0.txt

    Gott ist tot.
    
  • 政治/p_0.txt

    L'Etat, c'est moi , et aussi moi .
    
  • 科学/s_0.txt

    If I have seen further it is by standing on the shoulders of giants.
    
  • スポーツ/s_1.txt

    You miss 100% of the shots you don't take.
    
  • 出力データ構造:

    /data/train/politics/p_0.txt, [0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0]
    /data/train/science/s_0.txt, [1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0]
    /data/train/atheism/a_0.txt, [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    /data/train/sports/s_1.txt, [0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1]
    

コードは次のようになります。または、私の GitHub ページで見つけることができます。

public class FileDictCreateur 
{
    static String PATH = "/home/matthias/Workbench/SUTD/ISTD_50.570/assignments/practice_data/data/train";

    //the global list of all words across all articles
    static Set<String> GLOBO_DICT = new HashSet<String>();

    //is the globo dict full?
    static boolean globo_dict_fixed = false;

    // hash map of all the words contained in individual files
    static Map<File, ArrayList<String> > fileDict = new HashMap<>();

    //input to perceptron. final struc.
    static Map<File, int[] > perceptron_input = new HashMap<>();


    @SuppressWarnings("rawtypes")
    public static void main(String[] args) throws IOException 
    {
        //each of the diferent categories
        String[] categories = { "/atheism", "/politics", "/science", "/sports"};

        //cycle through all categories once to populate the global dict
        for(int cycle = 0; cycle <= 3; cycle++)
        {
            String general_data_partition = PATH + categories[cycle];

            File directory = new File( general_data_partition );
            iterateDirectory( directory , globo_dict_fixed);

            if(cycle == 3)
                globo_dict_fixed = true;
        }


        //cycle through again to populate the file dicts
        for(int cycle = 0; cycle <= 3; cycle++)
        {
            String general_data_partition = PATH + categories[cycle];

            File directory = new File( general_data_partition );
            iterateDirectory( directory , globo_dict_fixed);

        }



        perceptron_data_struc_generateur( GLOBO_DICT, fileDict, perceptron_input );



        //print the output
        for (Map.Entry<File, int[]> entry : perceptron_input.entrySet()) 
        {
            System.out.println(entry.getKey() + ", " + Arrays.toString(entry.getValue()));
        }
    }



    private static void iterateDirectory(File directory, boolean globo_dict_fixed) throws IOException 
    {
        for (File file : directory.listFiles()) 
        {
            if (file.isDirectory()) 
            {
                iterateDirectory(directory, globo_dict_fixed);
            } 
            else 
            {   
                String line; 
                BufferedReader br = new BufferedReader(new FileReader( file ));

                while ((line = br.readLine()) != null) 
                {
                    String[] words = line.split(" ");//those are your words

                    if(globo_dict_fixed == false)
                    {
                        populate_globo_dict( words );
                    }
                    else
                    {
                        create_file_dict( file, words );
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static void create_file_dict( File file, String[] words ) throws IOException
    {   

        if (!fileDict.containsKey(file))
        {
            @SuppressWarnings("rawtypes")
            ArrayList document_words = new ArrayList<String>();

            String word;

            for (int i = 0; i < words.length; i++) 
            {
                word = words[i];

                document_words.add(word);
            }
            fileDict.put(file, document_words);
        }
    }

    public static void populate_globo_dict( String[] words ) throws IOException
    {
        String word;

        for (int i = 0; i < words.length; i++) 
        {
            word = words[i];
            if (!GLOBO_DICT.contains(word))
            {
                GLOBO_DICT.add(word);
            }
        }   
    }

    public static void perceptron_data_struc_generateur(Set<String> GLOBO_DICT, 
                                                    Map<File,     ArrayList<String> > fileDict,
                                                    Map<File, int[] > perceptron_input)
    {
        //create a new entry in the array list 'perceptron_input'
        //with the key as the file name from fileDict
            //create a new array which is the length of GLOBO_DICT
            //iterate through the indicies of GLOBO_DICT
                //for all words in globo dict, if that word appears in fileDict,
                //increment the perceptron_input index that corresponds to that
                //word in GLOBO_DICT by the number of times that word appears in fileDict

        //so i can get the index later
        List<String> GLOBO_DICT_list = new ArrayList<>(GLOBO_DICT);

        for (Map.Entry<File, ArrayList<String>> entry : fileDict.entrySet()) 
        {
            int[] cross_czech = new int[GLOBO_DICT_list.size()];
            //initialize to zero
            Arrays.fill(cross_czech, 0);

            for (String s : GLOBO_DICT_list)
            {

                for(String st : entry.getValue()) 
                {
                    if( st.equals(s) )
                    {
                        cross_czech[ GLOBO_DICT_list.indexOf( s ) ] = cross_czech[ GLOBO_DICT_list.indexOf( s ) ] +1;
                    }
                }
            }
            perceptron_input.put( entry.getKey() , cross_czech);    
        }
    }
}
于 2015-02-17T10:46:02.313 に答える