从Ada中的文件中获取单词

时间:2013-04-08 14:09:23

标签: ada

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

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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;

答案 2 :(得分:0)

您可以使用Get_Line将文件一次读入字符串,然后使用正则表达式Regular Expressions in Ada?

,而不是单个单词。

答案 3 :(得分:0)

如果您在这里使用Ada 2012,我建议您这样做:

  1. With Ada.Containers.Indefinite_Ordered_Maps
  2. String为密钥,Positive为密钥实例化地图;
  3. 抓住绳子;我会使用单个字符串或stream - 处理。
  4. 将输入文本分解为单词;如果你使用流,这可以在飞行中完成。
  5. 当你得到一个单词(来自#4)时,如果它不存在则将其添加到地图中,否则增加该元素。
  6. 完成后,只需运行For Element of WORD_MAP Loop打印字符串&amp;计数。
  7. 有几种方法可以处理#3中的字符串:

    1. 通过递归函数调用完成大小[终止于非单词字符或输入结束]。
    2. Unbounded_String
    3. Vector(Positive,Character) - 附加有效字符,转换为数组[string]并在遇到无效字符[或输入结束]时添加到地图 - 工作变量。
    4. Not Null Access String工作变量。