如何在Prolog中找到大写字母,小写字母和单词的数量

时间:2016-12-31 17:15:31

标签: prolog

我需要阅读一个文本文件,找到大写字母,小写字母和单词数。

我该怎么做?

修改

main :- write('Acmak istediginiz dosyanin konumunu giriniz...'),nl,
  read(File),nl, 
  open(File,read,InputStream), 
  get_code(InputStream,CharacterCode), 
  LowerCount is 0,
  UpperCount is 0,
  NumberCount is 0,
  SpaceCount is 0,
  WordCount is 0, 
  processChar(CharacterCode,_,InputStream,LowerCount,UpperCoun‌t,NumberCount,SpaceC‌​ount,WordCount), 
  close(InputStream). 

isLower(CharacterCode,C1,C2) :- 
  CharacterCode >= 97,
  CharacterCode =< 122,
  C1 is C2 + 1
  ; 
  C1 is C2. 

isUpper(CharacterCode,C1,C2) :- 
  CharacterCode >= 65,
  CharacterCode =< 90,
  C1 is C2 + 1
  ; 
  C1 is C2. 

isNumber(CharacterCode,C1,C2) :- 
  CharacterCode >= 48,
  CharacterCode =< 57,
  C1 is C2 + 1
  ; 
  C1 is C2. 

isSpace(CharacterCode,C1,C2) :- 
  CharacterCode >= 1,
  CharacterCode =< 32,
  C1 is C2 + 1
  ; 
  C1 is C2.

isWord(CharacterCode,NextCharacterCode,C1,C2) :- 
  CharacterCode >= 0,
  CharacterCode =< 38,
  C1 is C2
  ; 
  CharacterCode >= 40,
  CharacterCode =< 47,
  C1 is C2
  ; 
  CharacterCode >= 58,
  CharacterCode =< 64,
  C1 is C2
  ; 
  CharacterCode >= 91,
  CharacterCode =< 96,
  C1 is C2
  ; 
  CharacterCode >= 123,
  C1 is C2
  ; 
  NextCharacterCode >= 48,
  NextCharacterCode =< 57,
  C1 is C2
  ; 
  NextCharacterCode =:= 39,
  C1 is C2
  ; 
  NextCharacterCode >= 65,
  NextCharacterCode =< 90,
  C1 is C2
  ; 
  NextCharacterCode >= 97,
  NextCharacterCode =< 122,
  C1 is C2
  ; 
  C1 is C2+1. 

processChar(-1,[],_,LowerCount,UpperCount,NumberCount,SpaceC‌​ount,WordCount) :- 
  write('Harf Sayisi = '), LetterCount is LowerCount + UpperCount, write(LetterCount),nl, 
  write('Kucuk Harf Sayisi = '), write(LowerCount), nl,
  write('Buyuk Harf Sayisi = '), write(UpperCount), nl, 
  write('Rakam Sayisi = '), write(NumberCount), nl, 
  write('Boşluk Sayisi = '), write(SpaceCount), nl, 
  write('Kelime Sayisi = '), write(WordCount), nl, !. 

processChar(CharacterCode,[CharacterCode|CharacterCodes],Inp‌​utStream,LowerCount,‌​UpperCount,NumberCou‌​nt,SpaceCount,WordCo‌​unt) :- 
  isUpper(CharacterCode,UpperCount1,UpperCount), 
  isLower(CharacterCode,LowerCount1,LowerCount), 
  isNumber(CharacterCode,NumberCount1,NumberCount), 
  isSpace(CharacterCode,SpaceCount1,SpaceCount), 
  get_code(InputStream,NextCharacterCode), 
  isWord(CharacterCode,NextCharacterCode,WordCount1,WordCount‌​), 
  processChar(NextCharacterCode,CharacterCodes,InputStream,Low‌erCount1,UpperCount1‌​,NumberCount1,SpaceC‌​ount1,WordCount1).  

1 个答案:

答案 0 :(得分:0)

我在Windows 10上使用SWI-Prolog测试了此代码。
并非所有的解决方案都在这里,因为这是我留给你的作业,但我确实验证了所有这些部分一起工作。

读取文件

我注意到您在评论中引用了Learn Prolog Now! - 12.3 Reading from Files。我也发现这个页面有点令人困惑。

processChar(end_of_file,[],_) :- !.  % EOF

processChar(CharacterCode,[CharacterCode|CharacterCodes],InputStream) :-
    get_code(InputStream,NextCharacterCode),
    processChar(NextCharacterCode,CharacterCodes,InputStream).

main :-
    open('C:/Users/Eric/Documents/Prolog/test.txt',read,InputStream),
    get_code(InputStream,CharacterCode),
    processChar(CharacterCode,CharacterCodes,InputStream),
    write(CharacterCodes), nl,
    string_codes(Characters,CharacterCodes),
    write(Characters), nl,
    close(InputStream).

运行时

main.
[71,114,121,70,102,105,110,100,111,114,46,10,72,117,102,102,76,101,112,117,102,70,46,10,114,97,118,101,110,99,108,97,87,46,10,115,108,121,116,104,101,114,105,78,46]
GryFfindor.
HuffLepufF.
ravenclaW.
slytheriN.
true.

检查特定字符类型

由于Prolog使用的是字符代码而不是字符,因此这里提示找小写字符,输入为ASCII字符代码。

isLower(CharacterCode) :-
    CharacterCode >= 97,
    CharacterCode =< 122.

计算单词

计算单词的一种方法是传递两个状态变量。一个在处理完字符时被修改,第二个用于传回最终答案。

当您遇到结束单词(如空格,行尾或文件结尾)的条件时,您会增加计数。

processChar(...,WordCount,WordCount2) :-
    ...
    WordCount1 is WordCount + 1,
    processChar(...,WordCount1,WordCount2).

否则,您只需为单词中的字符传递值。

processChar(...,WordCount,WordCount2) :-
    ...
    processChar(...,WordCount,WordCount2).

当您到达输入的末尾时,例如-1,您将计数移动到将用于输出的另一个变量。

processChar(-1, ... ,WordCount,WordCount1) :-
    WordCount1 is WordCount + 1.

在主要内容中添加初始化计数,例如0

main :-
    ...
    processChar(...,0,WordCount),
    write(WordCount), nl,
    ... .

还有许多其他方法可以使用Prolog和更多的idomatic,但这应该让你开始。