0

テキストファイルを前処理しようとしています。各行は、ドキュメント内の頻度を含むドキュメントのバイグラムワードです。各行の例を次に示します。

i_like 1 you_know 2 .... not_good 1

コーパス全体から辞書を作成することができました。ここで、コーパスを1行ずつ読み、辞書を作成して、ドキュメント用語マトリックスを作成します。これにより、マトリックス内の各要素(i、j)がドキュメント「i」内の用語「j」の頻度になります。

4

1 に答える 1

2

辞書を使用して各単語の整数インデックスを生成する関数を作成します。

Dictionary<string, int> m_WordIndexes = new Dictionary<string, int>();

int GetWordIndex(string word)
{
  int result;
  if (!m_WordIndexes.TryGet(word, out result)) {
    result = m_WordIndexes.Count;
    m_WordIndexes.Add(word, result);
  }
  return result;
}

結果のマトリックスは次のとおりです。

List<List<int>> m_Matrix = new List<List<int>>();

テキスト ファイルの各行を処理すると、行列の 1 行が生成されます。

List<int> ProcessLine(string line)
{
  List<int> result = new List<int>();
  . . . split the line in a sequence of word / number of occurences . . . 
  . . . for each word / number of occurences . . .{
    int index = GetWordIndex(word);      
    while (index > result.Count) {
      result.Add(0);
    }  
    result.Insert(index, numberOfOccurences);
  }
  return result;
}

一度に 1 行ずつテキスト ファイルを読み取り、各行で呼び出しProcessLine()、結果のリストを m_Matrix に追加します。

于 2012-06-05T14:11:20.443 に答える