如何在我的单词频率程序中使用LineNo(出现单词的行)和CurLine(当前行)扩展这些数据结构?

时间:2019-04-01 23:04:33

标签: permutation ada

我有一个程序可以将文件中的每个单词及其频率打印出来,但是我正在尝试对其进行调整,以便它还输出单词出现在哪几行。

我知道我可以使用创建一个Word_List类型 字:Word_Array; -独特的词 Num_Words:自然:= 0; -到目前为止看到了多少个独特的单词 curr_line:自然:= 1;

但是我想看看是否可以不执行此操作并通过调整现有代码来执行此操作。

procedure v3 is

   type Word is record 
      wlen: Natural := 45;       -- Length of the word
      count: Natural := 0;  -- Total number of occurrences of this word
      s: Unbounded_String;
      value: Unbounded_String;
            LineNo: Natural := 0;
   end record;

   type IntArray is array (1 .. 10) of Natural;
   type Word_Array is array (Natural range <>) of Word;


   --Sorting--
function "+" (S : String) return Unbounded_String renames To_Unbounded_String;
function "+" (S : Natural) return Unbounded_String renames To_Unbounded_String;

function "<" (L, R : Word) return Boolean is
 begin
 return L.s < R.s;
   end "<";

function "=" (L, R : Word) return Boolean is
 begin
 return L.s = R.s;
   end "=";

procedure Sort is new Ada.Containers.Generic_Array_Sort (Natural, Word, Word_Array);  
   ----


    num_words : Integer;

    procedure get_words(wl: out Word_Array) is
    Input : File_Type;
   begin


      num_words := 0;
      Open (File => Input,
         Mode => In_File,
         Name => "input.txt");

      loop
         declare
            s : String := Get_Line(Input);
            found: Boolean := false;
            word : Unbounded_String;
            index : Integer := 0;
            word_len:Integer := 0;
            empty : Unbounded_String;
            space : Unbounded_String;
         begin
            -- process the contents of Line here.
            index := 1;

            for I in s'Range loop 
               word_len := word_len + 1;
               if (index = s'Length or s(I) = ' ') then 
                  if (s(I) /= ' ') then
                     word := word & s(I);
                  end if;
                  space := space & s(I);

                  found := false;
                  for i in 1 .. num_words loop
                     if word = wl(i).s then 
                        wl(i).count := wl(i).count + 1;
                        found := true;
                     end if;
                     exit when found;
                  end loop;

                  if not found and word /= empty then -- Add word to list
                     num_words := num_words + 1;
                     wl(num_words).s := word;
                     wl(num_words).wlen := word_len;
                     wl(num_words).count := 1;
                  end if;
                  word := empty;
                  word_len := 0;
               else 
                  word := word & s(I);
               end if;  
               index := index + 1;
            end loop;
         end;
      end loop;

      exception
      when End_Error =>
         if Is_Open(Input) then
            Close (Input);
         end if;

   END Get_Words;


   Data : Word_Array(1 .. 1000);
   Output : File_Type;
begin
   get_words(Data);
   Sort (Data);
   --Create(F, Ada.Text_IO.Out_File, "output.txt");
   Create (File => Output,
           Mode => Out_File,
           Name => "output.txt");
   for I in Data'Range loop
      if Data(I).wlen /= 45 then
         Put_Line(Output, To_String(Data(I).s & Integer'Image(Data(I).count)));
      end if;
   end loop;
   close(Output);
end v3;

因此,如果文件具有:

Hello Hello
Hello World
World World 
World

我当前的输出是

Hello 3
World 4

我希望我的新输出是

Hello 1-2 wc: 3
World 2-3-4 wc: 4

0 个答案:

没有答案