VB sentence count - vb.net

I am trying to write a program that will count the number of sentences in a string. I would like to better use the framework has much has possible but I really don't understand this msdn example. So could someone explain it if they understand or does someone know how to count the number of sentences in a sting accurately? I am open to all and any suggestions.
http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.sentences.count.ASPX?cs-save-lang=1&cs-lang=vb#code-snippet-1
EDIT:
Ok, for anyone that has had a hard time with counting sentences (like me) here is what I have been able to come up with and I think this is has good as you can get it. So if anyone else has a idea on how to solve sentences counting let me know. But what I have and it works.
My idea is that a sentences is defined has ". or ! or ?" and followed by at least one space. So this what seems to work. (Sorry its not a fully working module because I copied it out of class I'm using).
'Period cehck
For i As Integer = 0 To _runFor
If (str(i) = _Dot And True = Char.IsWhiteSpace(str(i + 1))) Then
_sentence_count = _sentence_count + 1
End If
Next
'Question check
For i As Integer = 0 To _runFor
If (str(i) = _Question And True = Char.IsWhiteSpace(str(i + 1))) Then
_sentence_count = _sentence_count + 1
End If
Next
'Exclamation check
For i As Integer = 0 To _runFor
If (str(i) = _Exclamation And True = Char.IsWhiteSpace(str(i + 1))) Then
_sentence_count = _sentence_count + 1
End If
Next

It's not really possible to accurately count sentences programmatically. For instance, if you wrote a piece of code that just counted the number of occurrences of a period followed by a space followed by a capital letter, then it would incorrectly interpret "I gave my report to Dr. Johnson." as two sentences.

Related

Peak over treshold method - VBA - Excel

I really hope you can help me with the following question.
I have a dataset and I need to filter for some values. This is a dataset for wave analysis, and I need to filter out for the higstest values. The so called peak over treshold method. I want to filter out the storms in de wave dataset.
I can do this with the following: A storm is defined when the waveheight is above a certain value, for example 2.5 meters. The waveheight is measured every 3 hours.
So in the dataset I can do an If(B3>$A$1; B3;0). A1 = 2,5 m. Now I have filtered for waveheights. So now I can see in my dataset rows straight after each other with values higher than 2,5 meters, because a storm last serveral hours. See picture 1 as example:
[Picture 1][1]
So if a certain value is above X, then return that value, otherwise return zero. Now comes the hard part:
Now I need the highest value of that strip for values above 2,5 meters. As this is a big(!) dataset, I have a lot of datapoints. So I have several 'storms', which don't always last as long. So the values that are higher than 2,5 meters aren't always 3 rows, sometimes more. See picture 2picture 2
I need to compute the highst value in Column i, for each storm. Because each storm doens't last as long, I probally need to write code in VBA. Could anybody help me with this? I've been stuck for 6 days, but my VBA code doens't work and I do not have much experience with VBA. If anybody could help me that would be very much appreciated!
Kind regards,
Jeroen
EDIT as a reply on -Excel Developers-
I think I'm almost there, thank u very much for replying!! I am probably misreading the code, but a I have still a slight difficulty in computing the highstest value for each storm I think it should look something like this, as seen in picture 3.Picture 3
The highest value of each storm should be printed in the next column, as seen in picture 3. It probably says it all in the code, but I am misreading it.
Thank u very much for you answer! I really hope you can help me with the last part :-)
Kind regards,
Jeroen
You will have to iterate over all the values in column i. For every storm, find the highest value. A storm starts when a 0 is followed by a positive value, and ends when a positive value is followed by a 0. So something like this:
Sub FindHighestValue()
Dim vData As Variant
Dim Storms As Collection
Dim ndx As Long
Dim ndxStorm As Long
Dim MaxValue As Double
Dim Previous As Double
Dim InStorm As Boolean
vData = Sheet1.Range("I8:I21").Value 'Change this to whatever range your wave heights are in
Set Storms = New Collection
Previous = 0
InStorm = False
For ndx = LBound(vData, 1) To UBound(vData, 1)
If vData(ndx, 1) > 0 And Previous = 0 Then
'A storm has started
MaxValue = vData(ndx, 1)
InStorm = True
End If
If InStorm And vData(ndx, 1) > MaxValue Then
MaxValue = vData(ndx, 1)
End If
If (vData(ndx, 1) = 0 And Previous > 0) Or (ndx = UBound(vData, 1)) Then
'A storm has ended
InStorm = False
Storms.Add MaxValue
End If
Previous = vData(ndx, 1)
Next
If Storms.Count > 0 Then
For ndx = 1 To Storms.Count
Debug.Print ndx & ": " & Storms(ndx)
Next
End If
End Sub

Checking an array of booleans in VB.NET

I need to check an array of booleans and for each value act accordingly.
Current code is something like this, but i want to make it read easier
If heater_check(0) = true Then
get_temp(0)
End If
If heater_check(1) = true Then
get_temp(1)
End If
...
And so on. Is there a better solution?
I guess this is what you are looking for
For i As Integer = 0 To heater_check.length - 1
If heater_check(i) then
get_temp(i)
End If
Next
"For a case like this, could a For loop still work? If node_num = "1" Then Temperature(0) = C_D(Convert.ToChar(raw_result(byte_num + 22))) + C_D(Convert.ToChar(raw_result(b........ "chia kang ren
And the answer :
"As long as there is a 'pattern' on your variable changes, you can always use loop to simplify your If-Else statement"TheQuickBrownFox
..or use some logic to tie together two variables of different types having some sort of relation between, whenever required.
' ...
Dim TempIndex As Int32 = Integer.Parse(node_num) - 1
' Converts node_num to an Integer and substract 1.
Temperature(TempIndex) = _
C_D(Convert.ToChar(raw_result(byte_num + 22))) + _
C_D(Convert.ToChar(raw_result(byte_num + 21))) * 16 + _
C_D(Convert.ToChar(raw_result(byte_num + 20))) * 256
' ...
By the way, if you're asking A, only answers to A are relevant, or answers that covers A and anything directly related to A like A', -A, A² or |A|.
Here I'm talking about converting a String to Integer. That has nothing to do with :
"I need to check an array of booleans and for each value act accordingly."
=> Please mark TheQuickBrownFox's answer as the valid answer. (And avoid asking other questions than the original one - :) )

Excel VBA For loop in range contains gaps of 0's in sequence

I have isolated the problem and simplified the code to the root cause. More frustrating is that this loop works elsewhere without this error.
If you see the comments in the sequence here, there are 6 cells in the middle of the loop that turn into 0, and then after this 6 cell gap of 0's the loop works. I did use a msgbox to confirm the values were there. For whatever reason rows 130-135 always read as 0 though.
For x = 1 To 140
Cells(3 + x, "AW").Value = 70
'MsgBox (Cells(3 + x, "AW").Value)
'MsgBox confirms the correct value
'rows 130-135 are always empty with 0
Next
Any help greatly appreciated - very stumped at such a simple thing!
it was a simple logical error that was nested inside:
For a = 1 To Number_of_CalcRows
Cells(137 + a, "AW").Value = TenkanCurrent
Next
The problem was that I has not declared the TenkanCurrent with a value.

How to prevent more than one "," in a text field

Does anyone know how I can prevent users from entering more than one comma in a text field using an if statement? For example:
if txtWidth.text (contains more than one ",") then
MsgBox ("Please enter only one comma.")
Else ...
What should I put in the place of the first pair of brackets? The program I am making this for is Solidworks.
I have only just started programming and some of the terms are not very clear to me. If it is explained elsewhere and I havent searched properly I apologize.
If someone could help me that would be very nice.
Thank you in advance!
Rob
You can use the InStr() function twice :
if (InStr(1,txtWidth.Value,",") > 0 ) then
if (InStr(InStr(1,txtWidth.Value,",") + 1,txtWidth.Value,",") > 0) then
MsgBox ("Please enter only one comma.")
end if
end if
See here for more info on InStr.
I think you would need some nested if statements.
Something like this:
If input = "," Then
If numcommas = 0 Then
numcommas = 0
Else
'Don't allow input of character
End If
End If
You'll need to alter this a little bit in order to use it but this should get you started, or at least give you a geneeral idea of where to go.
This is a way to do it with just one if function:
If (Len(txtWidth.text) - Len(Replace(txtWidth.text, ",", ""))) > 1 Then
'Your Message'
End If
What this code does is it compares the length of your string with the length of your string, cleaned of all commas. So Len("Test,") would give back a length of 5 while Len(Replace("Test,",",","") returns 4 and by substracting those values you get the number of commas in your string.

I need someone to explain permutations of a string to me

Over the summer I decided to write a program that would solve an anagram by using all possible combinations of the letters in a word that you would enter. I managed to do it but in such a way that it could work out a 3-4 letter word out quickly but anything more and it would take ages! Anyway, after asking on some other site for help some guy/girl fixed my problem by writing some code for me.
At the time I didn't understand it even though it had been annotated and I had another look today to see if I could get anything from it but alas, nothing...
I've been researching permutations for about 2 hours now and I'm getting nowhere - the code is on the internet but no-one has explained it well enough (or simply enough) for me to grasp the concept. I will post the code that I have yet to understand below and if anyone can explain this in detail and how it works then it would be great!
Public Sub permutations(ByVal WordLength As Integer, ByVal SplitLetters As List(Of Char), ByVal word As String)
If WordLength = 1 Then
results.Add(word & SplitLetters(0))
count += 1
If count Mod updateCount = 0 Then
ldBar.Value = ((count / pCombinations) * 100)
End If
Else
For i = 0 To WordLength - 1
Dim newWord = word & SplitLetters(i)
Dim newSplitLetters As List(Of Char) = New List(Of Char)(SplitLetters)
newSplitLetters.RemoveAt(i)
Call permutations(WordLength - 1, newSplitLetters, newWord)
Next i
End If
End Sub
The method gets all permutations by using recursion.
Given a word, for example FUBAR, it uses each character in turn as the first character and gets all possible permutations by combining that will all permutation for the remaining characters:
F + permutatons(UBAR)
U + permutatons(FBAR)
B + permutatons(FUAR)
A + permutatons(FUBR)
R + permutatons(FUBA)
For that first recursive call it gets all permutations that starts with each character:
U + permutatons(BAR)
B + permutatons(UAR)
A + permutatons(UBR)
R + permutatons(UBA)
And so on. When it gets down to a single character string, all possible permutations is just that single character.