RosettaCodeData/Task/Abbreviations-automatic/PureBasic/abbreviations-automatic.basic

47 lines
1.5 KiB
Plaintext

EnableExplicit
#TZ="|"
#FZ="@"
#WORD_SEPARATOR=Chr(32)
Declare.i abbreviations(txt.s)
Define dataset.s, line_number.i, line_cache.s, result.i, word_index.i, word.s
If OpenConsole("abbreviations-automatic") And ReadFile(0,"./Data/days_of_the_week.txt")
dataset=ReadString(0,#PB_UTF8|#PB_File_IgnoreEOL) : CloseFile(0)
line_number=1
Repeat
line_cache=StringField(dataset,line_number,#LF$)
If Len(line_cache)=0
PrintN("")
Else
result=abbreviations(line_cache) : PrintN(RSet(Str(result),3)+": "+line_cache)
word_index=1
word=StringField(line_cache,word_index,#WORD_SEPARATOR)
If word : Print(Space(5)) : EndIf
While word
Print(LSet(Left(word,result),Len(word)+1))
word_index+1 : word=StringField(line_cache,word_index,#WORD_SEPARATOR)
Wend
PrintN("")
EndIf
line_number+1
Until line_number>CountString(dataset,#LF$)
Input()
EndIf
Procedure.b CompareLetters(sfield.s,letters.i,separator.s=#WORD_SEPARATOR)
Define word_index.i, buf.s
For word_index=1 To CountString(sfield,separator)+1
buf+LSet(Left(StringField(sfield,word_index,separator),letters),letters,#FZ)+#TZ
Next
For word_index=1 To CountString(buf,#TZ)
If CountString(buf,StringField(buf,word_index,#TZ))>1 : ProcedureReturn #True : EndIf
Next
ProcedureReturn #False
EndProcedure
Procedure.i abbreviations(txt.s)
Define letters.i=1
While CompareLetters(txt,letters) : letters+1 : Wend
ProcedureReturn letters
EndProcedure