我有一个包含一些文本的文件的文件夹。我试图一个接一个地浏览所有文件,并计算我们在文本文件中看到每个单词的次数。 我知道如何打开文件,但是一旦我在文件中,我不知道如何逐一读取每个单词,然后转到下一个单词。从Ada中的文件中获取单词
如果有人有一些想法来指导我会很好。
我有一个包含一些文本的文件的文件夹。我试图一个接一个地浏览所有文件,并计算我们在文本文件中看到每个单词的次数。 我知道如何打开文件,但是一旦我在文件中,我不知道如何逐一读取每个单词,然后转到下一个单词。从Ada中的文件中获取单词
如果有人有一些想法来指导我会很好。
使用Get_Line将文件一次一行地读取到一个字符串中,然后将该行分成单独的单词。
而是通过个人的话去的,你可以读文件使用Get_Line每次向每行一个字符串,然后使用正则表达式:如果你使用艾达2012这里就是我会建议Regular Expressions in Ada?
一旦我读完了这一行,我该如何分离每个单词? – user2131763 2013-04-08 14:53:13
@ user2131763:我认为你应该破解这些书,而不是在StackOverflow上闲逛。 – 2013-04-08 14:58:58
建议查看字符串包中的索引函数。它们是灵活的工具,可以为您提供字符索引(例如“”或“任何不在字母表中的字符”) - 然后您可以使用这些索引将字符串切分为单词。 – 2013-04-08 15:03:20
这样做:
With Ada.Containers.Indefinite_Ordered_Maps
。String
为关键字实例化一个映射,Positive
为Key;stream
-处理。For Element of WORD_MAP Loop
即可打印字符串& count。有几种方法,你可以在#3处理字符串:
Not Null Access String
工作变量。这可能有点像用大锤扑着苍蝇,但如果你要做这里提到的所有工作,你应该考虑使用[OpenToken](http://www.stephe-leake.org/ada/opentoken .html),它将为你做所有这些工作。 – 2013-04-10 13:46:38
我试图让OpenToken正常工作,但它不会......虽然这可能一直试图使用Dotnet编译器。 – Shark8 2013-04-10 14:36:37
我对使用.net编译器没有经验,我不会惊讶地发现你现在是唯一的人。 :-)维护人员可能有兴趣听到你的尝试。 – 2013-04-10 14:40:22
这样做的一种方式,我需要一些容器的游戏时间。 考虑到多个文件,使用流仍然是解决您的问题的最佳解决方案。
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;
+1 - 有点儿短,但这是真的在这里唯一的答案,指定的工作的问题的平凡相称的水平。 – 2013-04-10 13:49:02
有时候,一行答案比300行代码更好! – 2013-04-15 16:17:02