2017-10-27 6 views
0

ファイルの各行は、単語のベクトル記述(埋め込み)を表す50の浮動小数点数で始まる単語で始まります。私はファイルを読み込み、各単語とその埋め込みをハッシュテーブルに格納しようとしています。私が直面している問題は、数値書式例外または時には範囲外の配列例外が発生することです。各単語とその埋め込みをハッシュマップに読み込んで保存するにはどうすればよいですか?文字列と浮動小数点数のデータを含むテキストファイルをハッシュマップに読み込んで保存する

Sノードクラス:

public class sNode{ // Node class for hash map 
public String word; 
public float[] embedding; 
public sNode next; 

public sNode(String S, float[] E, sNode N){ // Constructor 
    word = S; 
    embedding = new float[50]; 
    for (int i=0;i<50;i++) 
     embedding[i] = E[i]; next = N; 
} 

hashTableStringsクラス:

public class hashTableStrings{ 
private static sNode [] H; 
private int TABLE_SIZE; 
private int size; 

public hashTableStrings(int n){ // Initialize all lists to null H = new sNode[n]; for(int i=0;i<n;i++) H[i] = null; } 
    size = 0; 
    TABLE_SIZE = n; 
    H = new sNode[TABLE_SIZE]; 
    for(int i=0;i<TABLE_SIZE;i++) 
     H[i] = null; 
} 

public int getSize(){ // Function to get number of key-value pairs 
    return size; 
} 


public static void main (String [] args) throws IOException{ 
    Scanner scanner = new Scanner(new FileReader("glove.6B.50d.txt")); 

    HashMap<String, Float> table = new HashMap<String, Float>(); 

    while (scanner.hasNextLine()) { 
     String[] words = scanner.nextLine().split("\t\t"); // split space between word and float number embedding 
     for (int i=0; i<50;i++){ 
      table.put(words[0], Float.parseFloat(words[i])); 
     } 
    } 

    System.out.println(table); 

} 

TXTファイルのサンプル: ファイルには、次のリンクで見つけることができenter image description here

https://nlp.stanford.edu/projects/glove/

ファイル

glove.6B.zip

をダウンロードして、

glove.6B.50d.txt

テキストファイルを開きます。

+1

を使用し、keyためValueとしてすべての浮動値を格納することになります例外を除いて、デバッガでプログラムの実行をステップ実行することをお勧めします。これにより、コード内のバグを特定するのに役立ちます。 – dave

答えて

0

文字列を「\ t \ t」二重タブスペースで分割しているため、「範囲外の配列」の例外が発生しています。一方、単一のスペースしかありません。このため、各行は複数の単語に分割されるのではなく、1つの文字列として分割され、1つの長さの配列しか得られません。 split(" ")split("\t\t")の交換

String[] words = scanner.nextLine().split("\t\t"); 
// words.length will return 1, since it contains only single String(Whole line). 

は、(あなたが各行で始まる単語が含まれている場合)、51ワードの合計は、各行にある、道problem.Byを修正する必要があります。だからi < 51 not i <50する必要があります。 @Satish Thulvaが指摘しているよう

for(i = 1; i < 51; i++){ 
    // Do your work... 
    } 

    // i is starting from 1st index because at 0th index, the starting word will be placed and the floating points starts from 1st index. 

しかし、あなたは、HashMapのでやっているあなたのcode.Theの方法といくつかの問題がまだある、キー(ワード)が唯一持っている値(全体ではない浮動浮動続きます行の値)を値として返します。 EXは、お使いの場合には

truecar.com -0.23163 0.39098 -0.7428 1.5123 -1.2368 -0.89173 -0.051826 -1.1305 0.96384 -0.12672 -0.8412 -0.76053 0.10582 -0.23173 0.11274 0.26327 0.053071 0.66657 0.9423 -0.78162 1.6225 0.097435 -0.67124 0.46235 0.3226 1.3423 0.87102 0.2217 -0.068228 0.73468 -1.0692 -0.85722 -0.49683 -1.4468 -1.1979 -0.49506 -0.36319 0.53553 -0.046529 1.5829 -0.1326 -0.55717 -0.17242 0.99214 0.73551 -0.51421 0.29743 0.19933 0.87613 0.63135 

、結果は

Key: truecar.com value: 0.63135 

コードが投げされている場合はHashMap<String, Float[]>

String[] words = scanner.nextLine().split(" "); // split space between word and float number embedding 

     //An Array of Float which will keep values for words. 
     Float values[] = new Float[ words.length-1 ]; // because we are not going to store word as its value. 
     for(int i=1; i< words.length; i++){ 
      values[i-1] = Float.parseFloat(words[i]) ; } 

     // Now all the values are stored in array. 
     // Now store it in the Map. 
     table.put(words[0], values); 
+0

もう一つ。 'table'は各' key'に対してただ一つの 'float'値を含みます。オリジナルのポスターもそれを変更する必要があります。あなたもあなたの答えを編集することができますそれを含める:) –

+0

私は、その部分を忘れて、指摘のためにありがとう。この場合、文字列を分割する必要のない部分は渡すことができません。すなわち分割( ""、2)。 –

+0

明らかに、テキストファイルの各行に51を超えるデータ型があります。それはHashMapにキーと値を読み込んで保存するときに問題になるでしょうか?テキストファイルの正確な列と行を知らなくても、これらの値をHashMapに渡すことは可能ですか? – Alan

関連する問題