1

我有一个包含一些文本的文件的文件夹。我试图一个接一个地浏览所有文件,并计算我们在文本文件中看到每个单词的次数。我知道如何打开文件,但是一旦我进入文件,我不知道如何一个接一个地阅读每个单词,然后转到下一个单词。

如果有人有一些想法来指导我,那就太好了。

4

4 回答 4

1

这是一种方法,我需要一些容器的游戏时间。考虑到多个文件,使用流仍然是解决问题的最佳方法。


Text_Search.ads

Pragma Ada_2012;
With
Ada.Containers.Indefinite_Ordered_Maps;

Package Text_Search  with Elaborate_Body is

Text : Constant String :=
  ASCII.HT &
  "We hold these truths to be self-evident, that all men are created " &
  "equal, that they are endowed by their Creator with certain unalienable "&
  "Rights, that among these are Life, Liberty and the pursuit of " &
  "Happiness.--That to secure these rights, Governments are instituted " &
  "among Men, deriving their just powers from the consent of the governed" &
  ", --That whenever any Form of Government becomes destructive of these " &
  "ends, it is the Right of the People to alter or to abolish it, and to " &
  "institute new Government, laying its foundation on such principles " &
  "and organizing its powers in such form, as to them shall seem most " &
  "likely to effect their Safety and Happiness. Prudence, indeed, will " &
  "dictate that Governments long established should not be changed for " &
  "light and transient causes; and accordingly all experience hath shewn, "&
  "that mankind are more disposed to suffer, while evils are sufferable, " &
  "than to right themselves by abolishing the forms to which they are " &
  "accustomed. But when a long train of abuses and usurpations, pursuing " &
  "invariably the same Object evinces a design to reduce them under " &
  "absolute Despotism, it is their right, it is their duty, to throw off " &
  "such Government, and to provide new Guards for their future security." &
  "now the necessity which constrains them to alter their former Systems " &
  "of Government. The history of the present King of Great Britain is a " &
  "history of repeated injuries and usurpations, all having in direct " &
  "object the establishment of an absolute Tyranny over these States. To " &
  "prove this, let Facts be submitted to a candid world.";


Package Word_List is New Ada.Containers.Indefinite_Ordered_Maps(
    Key_Type     => String,
    Element_Type => Positive
);


Function Create_Map( Words : String ) Return Word_List.Map;

Words : Word_List.map;

End Text_Search;

Text_Search.adb

Package Body Text_Search is

Function Create_Map( Words : String ) Return Word_List.Map is
    Delimiters : Array (Character) of Boolean:=
  ('.' | ' ' | '-' | ',' | ';' | ASCII.HT => True, Others => False);


    Index, Start, Stop : Positive := Words'First;

begin
    Return Result : Word_List.Map do
        Parse:
        loop
        Start:= Index;
        -- Ignore initial delimeters.
        while Delimiters(Words(Start)) loop
            Start:= 1+Start;
        end loop;

        Stop:= Start;
        while not Delimiters(Words(Stop)) loop
            Stop:= 1+Stop;
        end loop;

        declare
            -- Because we stop *on* a delimiter we mustn't include it.
            Subtype R is Positive Range Start..Stop-1;
            Substring : String renames Words(R);
        begin
            -- if it's there, increment; otherwise add it.
            if Result.Contains( Substring ) then
            Result(Substring):= 1 + Result(Substring);
            else
            Result.Include( Key => substring, New_Item => 1 );
            end if;
        end;

        Index:= Stop + 1;
        end loop parse;

    exception
        When Constraint_Error => null; -- we run until our index fails.
    end return;
    End Create_Map;


Begin
    Words:= Create_Map( Words => Text );
End Text_Search;

Test.adb

Pragma Ada_2012;
Pragma Assertion_Policy( Check );

With
Text_Search,
Ada.Text_IO;

Procedure Test is

    Procedure Print_Word( Item : Text_Search.Word_List.Cursor ) is
    use Text_Search.Word_List;
    Word : String renames Key(Item);
    Word_Column : String(1..20) := (others => ' ');
    begin
    Word_Column(1..Word'Length+1):= Word & ':';
    Ada.Text_IO.Put_Line( Word_Column & Positive'Image(Element(Item)) );
    End Print_Word;

Begin
    Text_Search.Words.Iterate( Print_Word'Access );
End Test;
于 2013-04-11T05:04:07.600 回答
1

使用 Get_Line 将文件一次一行读入一个字符串,然后将该行分解为单个单词。

于 2013-04-08T14:23:33.220 回答
0

如果您使用的是 Ada 2012,我建议您这样做:

  1. With Ada.Containers.Indefinite_Ordered_Maps.
  2. String用as key 和as Key实例化一个 Map Positive
  3. 抓住绳子;我会使用单个字符串或stream-处理。
  4. 将输入文本分解成单词;如果您使用流,这可以即时完成。
  5. 当你得到一个词(来自#4)时,如果它不存在,则将它添加到你的地图中,否则增加元素。
  6. 完成后,只需运行一个For Element of WORD_MAP Loop打印字符串和计数的命令。

有几种方法可以处理 #3 中的字符串:

  1. 通过递归函数调用 [终止于非单词字符或输入结尾] 完美调整大小。
  2. Unbounded_String
  3. Vector (Positive, Character) -- 附加有效字符,转换为数组 [string] 并在遇到无效字符 [或输入结尾] 时添加到映射 - 工作变量。
  4. Not Null Access String工作变量。
于 2013-04-09T16:56:58.273 回答
0

您可以使用 Get_Line 将文件一次一行读入字符串,然后使用正则表达式:Ada 中的正则表达式?

于 2013-04-08T14:30:36.407 回答