Hep der! Har lige flikket noget kode sammen som jeg tror fungere, men du kan jo få æren af at teste det ;)
Det drejer sig om en class som ligger i sin egen fil og en testside som viser hvordan man kan bruge den. Here goes:
analyze.cls.asp:
----------------
<%
class TextAnalyzer
private m_text
private m_maxConsecutiveConsonants
private m_idx
private m_countValidWords
private m_countWords
private m_strValidWordChars
private m_strInvalidWords
' =============================================================================
' =============================================================================
private sub Class_Initialize()
m_text = ""
m_maxConsecutiveConsonants = 3
m_idx = 1
m_countValidWords = 0
m_countWords = 0
m_strInvalidWords = ""
m_strValidWordChars = "abcdefghijklmnopqrstuvwxyzæøåâãäáàôöõóòêëéèÿý0123456789-"
end sub
' =============================================================================
' =============================================================================
public property get maxConsecutiveConsonants
maxConsecutiveConsonants = m_maxConsecutiveConsonants
end property
public property let maxConsecutiveConsonants(value)
m_maxConsecutiveConsonants = value
end property
public property get Text
Text = m_text
end property
public property let Text(value)
m_text = value
m_idx = 1
m_countValidWords = 0
m_countWords = 0
m_strInvalidWords = ""
end property
public property get countWords
countWords = m_countWords
end property
public property get countValidWords
countValidWords = m_countValidWords
end property
public property get countInvalidWords
countInvalidWords = m_countWords - m_countValidWords
end property
public property get invalidWords
invalidWords = m_strInvalidWords
end property
' =============================================================================
' =============================================================================
private function isWordValid(wd)
dim arrConsonants
dim idx, consonant, vovels
dim isValid
isValid = true
strConsonants = "bcdfghjklmnpqrstvwxz"
idx = 1
consonant = 0
vovels = 0
do while idx <= len(wd)
if instr(1, strConsonants, mid(wd, idx, 1), vbTextCompare) > 0 then
consonants = consonants + 1
if consonants > maxConsecutiveConsonants then
isValid = false
exit do
end if
else
consonants = 0
vovels = vovels + 1
end if
idx = idx + 1
loop
' hvis der ikke fandtes mere end det tilladte efterfølgende
' antal konsonanter og der er mindst en vokal i ordet
' så er det OK (undgår ord som khj, kls, jh osv.
isWordValid = isValid and (vovels > 0)
end function
' =============================================================================
' =============================================================================
private sub findNextWordStart()
'response.Write("findNextWordStart - Enter (" & m_idx & ")<br>")
' textLen er en optimering, så vi undgår at kalde len på text hvert gennemløb
dim textLen
textLen = len(m_text)
' Sålænge den aktuelle karakter ikke er i strengen med
' valide karakterer for ord, så forøges indekset
do while (instr(1, m_strValidWordChars, mid(m_text, m_idx, 1), vbTextCompare) = 0) and (m_idx < textLen)
m_idx = m_idx + 1
loop
'response.Write("findNextWordStart - Exit (" & m_idx & ")<br>")
end sub
' =============================================================================
' =============================================================================
private function getNextWord()
'response.Write("getNextWord - Enter (" & m_idx & ")<br>")
' textLen er en optimering, så vi undgår at kalde len på text hvert gennemløb.
' newIdx benyttes til at finde slutningen af ordet. Det er vigtigt at m_idx IKKE
' ændres inden vi har kopieret ordet fra hovedteksten!
dim textLen, newIdx
textLen = len(m_text)
' Sæt pegepinden i starten af det aktuelle ord
newIdx = m_idx
' Sålænge det aktuelle tegn er validt for ord, så flyttes pegepinden til
' slutnigen af ordet en position frem.
do while instr(1, m_strValidWordChars, mid(m_text, newIdx, 1), vbTextCompare) > 0 and newIdx < textLen
newIdx = newIdx + 1
loop
' Hvis der blev fundet et ord, så kopier det ellers returner en tom streng
if newIdx - m_idx > 0 then
getNextWord = mid(m_text, m_idx, newIdx - m_idx)
else
getNextWord = ""
end if
' Nu er det sikkert at ændre m_idx, så den er up to date med den aktuelle position.
m_idx = newIdx
'response.Write("getNextWord - Exit (" & m_idx & ")<br>")
end function
' =============================================================================
' =============================================================================
public function validateText()
dim foundWord
' Nulstil objektets variable så der ikke opstår akkumulerede værdier
' ved gentagne kald af dette objekt.
m_idx = 1
m_strInvalidWords = ""
m_countValidWords = 0
m_countWords = 0
' Flyt markøren frem til det første ord i listen
findNextWordStart
' Hent det første ord i teksten
foundWord = getNextWord()
' Flyt markøren frem til det næste ord i listen
findNextWordStart
do while len(foundWord) > 0
m_countWords = m_countWords + 1
if isWordValid(foundWord) then
' tæl antallet af gyldige ord op
m_countValidWords = m_countValidWords + 1
else
' indsæt det ugyldige ord i strengen med ugyldige ord
if len(m_strInvalidWords) > 0 then
m_strInvalidWords = m_strInvalidWords & ","
end if
m_strInvalidWords = m_strInvalidWords & foundWord
end if
' Hent det næste ord i teksten
foundWord = getNextWord()
findNextWordStart
loop
end function
end class
%>
testanalyse.asp:
----------------
<%@ language="vbscript" codepage="1252" %>
<!-- #include file="analyze.cls.asp" -->
<%
if request.Form("checkText") <> "" then
set objAnalyze = new TextAnalyzer
objAnalyze.Text = cstr(Request.Form("txtAnalyzeThis"))
objAnalyze.validateText()
end if
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd"><html>
<head>
<title>Textanalyzer</title>
<meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<form method="post">
<textarea name="txtAnalyzeThis" style="width:600px;height:400px;"><%=Request.Form("txtAnalyzeThis")%></textarea><br>
<input type="submit" name="checkText" value="Check it!">
</form>
<%
if not isEmpty(objAnalyze) then
response.Write("Antal ord: " & objAnalyze.countWords & "<br>")
response.Write("Antal gyldige ord: " & objAnalyze.countValidWords & "<br>")
response.Write("Antal ugyldige ord: " & objAnalyze.countInvalidWords & "<br>")
response.Write("Ugyldige ord: " & objAnalyze.invalidWords & "<br>")
end if
%>
</body>
</html>
Spørg til hvis der er noget du er i tvivl om :)