2013-04-08 41 views
1

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

如果有人有一些想法来指导我会很好。

回答

1

使用Get_Line将文件一次一行地读取到一个字符串中,然后将该行分成单独的单词。

+3

+1 - 有点儿短,但这是真的在这里唯一的答案,指定的工作的问题的平凡相称的水平。 – 2013-04-10 13:49:02

+0

有时候,一行答案比300行代码更好! – 2013-04-15 16:17:02

0

而是通过个人的话去的,你可以读文件使用Get_Line每次向每行一个字符串,然后使用正则表达式:如果你使用艾达2012这里就是我会建议Regular Expressions in Ada?

+0

一旦我读完了这一行,我该如何分离每个单词? – user2131763 2013-04-08 14:53:13

+1

@ user2131763:我认为你应该破解这些书,而不是在StackOverflow上闲逛。 – 2013-04-08 14:58:58

+1

建议查看字符串包中的索引函数。它们是灵活的工具,可以为您提供字符索引(例如“”或“任何不在字母表中的字符”) - 然后您可以使用这些索引将字符串切分为单词。 – 2013-04-08 15:03:20

0

这样做:

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

有几种方法,你可以在#3处理字符串:

  1. 通过递归函数调用[终止对非文字字符或输入结束]完美尺寸。
  2. Unbounded_String
  3. 向量(正数,字符) - 追加有效字符,转换为数组[字符串]并在遇到无效字符[或输入结束]时添加到映射 - 工作变量。
  4. Not Null Access String工作变量。
+0

这可能有点像用大锤扑着苍蝇,但如果你要做这里提到的所有工作,你应该考虑使用[OpenToken](http://www.stephe-leake.org/ada/opentoken .html),它将为你做所有这些工作。 – 2013-04-10 13:46:38

+0

我试图让OpenToken正常工作,但它不会......虽然这可能一直试图使用Dotnet编译器。 – Shark8 2013-04-10 14:36:37

+0

我对使用.net编译器没有经验,我不会惊讶地发现你现在是唯一的人。 :-)维护人员可能有兴趣听到你的尝试。 – 2013-04-10 14:40:22

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;