0

おかげさまで解決しました。

私のwords.txtファイルは次のようになります。

await   -1

awaited -1

award   3

awards  3

値はタブ区切りです。まず、たとえば await = -1 ポイントの結果を取得し、comment.txt ファイルに従ってすべての文のスコアを提供したいと考えていwords.txtます。プログラムの出力は次のようになります(たとえば)

-1.0

2.0

0.0

5.0

立ち往生していて、次に何をすべきか正確にわかりません。words.txtこれまでのところ、ファイルを読み取ることしかできませんでした。

    const char DELIM = '\t'; 
    const string FILENAME = @"words.txt"; 

    string record;  
    string[] fields; 

    FileStream inFile; 
    StreamReader reader; 


    inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);

    reader = new StreamReader(inFile);

    record = reader.ReadLine();

    //Spliting up a string using delimiter and
    //storing the spilt strings into a string array
    fields = record.Split(DELIM);

    double values = double.Parse(fields[1]);
    string words = fields[0];
4

4 に答える 4

1

辞書を見て、スコアを付けたい各単語を辞書の値と一致させることができます。このようにして、取得したすべての単語をループして値を出力することができます

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary<string, int> dictionary = new Dictionary<string, int>();
        dictionary.Add("await", -1);
        dictionary.Add("awaited", -1);
        dictionary.Add("award", 3);
        dictionary.Add("awards", 3);

        //read your file
        //split content on the splitter (tab) in an array

        for(int i=0; i<array.Length; i++)
        {
            //output the value
        }
    }
}
于 2013-06-13T16:45:46.697 に答える
0

辞書なしの実用的なソリューション:

using System.IO;
using System.Text.RegularExpressions; 

class Program
{
    static void Main(string[] args)
    {
        foreach (var comment in File.ReadAllLines(@"..\..\comments.txt"))
            Console.WriteLine(GetRating(comment));

        Console.ReadLine();
    }

    static double GetRating(string comment)
    {
        double rating = double.NaN;

        var wordsLines = from line in File.ReadAllLines(@"..\..\words.txt")
                         where !String.IsNullOrEmpty(line)
                         select Regex.Replace(line, @"\s+", " ");

        var wordRatings = from wLine in wordsLines
                          select new { Word = wLine.Split()[0],  Rating = Double.Parse(wLine.Split()[1]) };


        foreach (var wr in wordRatings)
        {
            if (comment.ToLower().Split(new Char[] {' ', ',', '.', ':', ';'}).Contains(wr.Word))
                rating = wr.Rating;
        }

        return rating;
    }
}
于 2013-06-15T19:49:48.607 に答える