|
if ((remSentence.Contains(keywords[i]))) That's the position your trouble starts: you execute it once only regardless of the number of occurrences. You'll better use a function to split the sentence, and apply that function recursively on the resulting sub-sentences.
And yes: Regular Expressions are preferred.
|
|
|
|
|
here is what i did using regex. it works well. But doing this way, Splitting with regex, i lost the control over the word "and" for further processing. I have a lexicon of 20 words that normally appears before the "and (اور)" in urdu language. In next step I want to have a way to check the word before "and" against the lexicon and if found the sentence is broken else display the complete sentence.
private void button1_Click(object sender, EventArgs e)
{
{
richTextBox2.Text = "";
richTextBox3.Text = "";
string[] sentences = SentenceTokenizer(richTextBox1.Text);
string remSentence;
// these are urdu conjunctions. i am actually working on urdu language.
Regex r = new Regex("(کہ |اور | تاکہ| مگر | تاہم | کیونکہ | لیکن )");
foreach (string sentence in sentences)
{
remSentence = sentence;
remSentence = r.Replace(remSentence, "|");
string[] phrases = remSentence.Split('|');
for (int i = 0; i < phrases.Length; i++)
{
richTextBox2.Text += phrases[i] + '\n';
}
}
}
}
|
|
|
|
|
would you plz share some coded modification?
-- modified 2-Apr-14 5:26am.
|
|
|
|
|
I am not Bernhard Hiller.
All the label says is that this stuff contains chemicals "... known to the State of California to cause cancer in rats and low-income test subjects." Roger Wright http://www.codeproject.com/lounge.asp?select=965687&exp=5&fr=1#xx965687xx
|
|
|
|
|
Something like the below will loop for each keyword until there are no more matches left for that keyword - but it wont be perfect because it will skip over other keywords while it's looking
e.g "if you had this and that or the other and something"
you'd strip out all the Ands before stripping out the Or
What you need to do is
Repeat
Find the first occurrence of ANY of the keywords in your sentence.
Split the sentence
Until no occurrences found
Using the IndexOf method you can loop through your keywords, finding the lowest, non zero value of IndexOf and storing that word.
When the loop finishes , split the sentence using that word.
bool finished = false;
while (not finished)
if ((remSentence.Contains(keywords[i])))
{
richTextBox2.Text += remSentence.Substring(0, remSentence.IndexOf(keywords[i])) + '\n' + keywords[i] + '\n';
remSentence = remSentence.Substring(remSentence.IndexOf(keywords[i]) + keywords[i].Length);
}
else
{
finished = true;
}
|
|
|
|
|
Private objTestStr As String
Public Property Get TestStr() As String
Set TestStr = objTestStr
End Property
how will the code looks like in c#?
|
|
|
|
|
In C# you can create a Property in multiple ways :
Full blown Property :
private string testString;
public string TestString
{
get
{
return testString;
}
set
{
testString = value;
}
}
Or you can use Auto Implemented Properties:
public string TestString { get; set; };
For a good explanation of C# properties see the MSDN page here[^]
Everyone dies - but not everyone lives
|
|
|
|
|
does the vb code above includes both the Get and Set? or its just a readonly Property where is should only include Get?
|
|
|
|
|
Your code shows a read-only property.
private string objTestStr;
public string TestStr
{
get { return objTestStr; }
}
Well, naming conventions differ a little, _TestString would be preferred instead of objTestStr.
You could also use:
public string TestStr { get; private set; }
|
|
|
|
|
i am very new to c# and i want to write a program to manage a small inventory,i will like to know how i can connect to microsoft access database and using oledbAdapter and write code to save,delete and Update my datasource.pls
|
|
|
|
|
Let me Google that for you...[^]
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
|
Check here[^]
The link to connectionstrings.com is in the article.
Although if you can, forget about access. There are free versions of MySQL, PostgreSQL, Oracle and SQL-Server which are far better.
hope this helps.
|
|
|
|
|
|
For you all, "if one method throws an exception, the other methods will not run." this is the main reason I am using separate try..catch blocks. So that, even if a function gives an exception, the next can execute.
Can you suggest me a nice approach other than I am using here to execute the next function even if an exception occurred during the first function.
My Code
public XmlDocument GetXML(string[] arrInputInfo)
{
string rtnErrno=null;
ArrayList rtnValidateLogin=null;
string rtnPostSMS = null;
string strStartDateTime = DateTime.Now.ToString();
string strEndDateTime = DateTime.Now.ToString();
DataSet dst=new DataSet();
string[] arrLogReq = new string[5];
string strLoginRef = "-999";
string strstatus = "20";
try
{
//Validate Input Details
rtnErrno = ValidateInputInfo(arrInputInfo);
if (rtnErrno != "0")
{
throw new Exception("Exception raised at Input Validation");
}
//Validate Login Details
rtnValidateLogin = objLogin.ValidateCPLogin(arrInputInfo[0], arrInputInfo[1]);
if (rtnValidateLogin[0].ToString() != "0")
{
rtnErrno = rtnValidateLogin[0].ToString();
throw new Exception("Exception raised at Login Validation");
}
else
{
dst = (DataSet)rtnValidateLogin[1];
strLoginRef = dst.Tables[0].Rows[0][0].ToString();
}
//Insert Message
rtnPostSMS = PostSMSText(arrInputInfo);
if (rtnPostSMS.ToString() != "0")
{
rtnErrno = rtnPostSMS[0].ToString();
throw new Exception("Exception raised at Inserting Message");
}
}
catch (Exception ex)
{
strstatus = "30";
LogErrorHelper.LogError(Constants.ERROR_WEBSERVICES_LOGFILE, ex, "BLL.CSMSService-->GetXML");
}
finally
{
GC.Collect();
}
try
{
strEndDateTime = DateTime.Now.ToString();
arrLogReq[0] = strLoginRef; //Login Ref no
arrLogReq[1] = string.Concat(arrInputInfo[2], "~", arrInputInfo[3]); //Mobile No and Message
arrLogReq[2] = strStartDateTime; //start time
arrLogReq[3] = strEndDateTime; //end time
arrLogReq[4] = strstatus;
//To Log Incoming Requests
rtnErrno = LogConfirmation(arrLogReq);
}
catch (Exception ex)
{
LogErrorHelper.LogError(Constants.ERROR_WEBSERVICES_LOGFILE, ex, "BLL.CSMSService-->GetXML");
}
finally
{
GC.Collect();
}
return GetFinalXml(rtnErrno);
}
|
|
|
|
|
..one does not add a try-block around each statement; not every statement throws exceptions, and it makes your code hard to read and harder to maintain.
DoSafe(() => { throw new Exception(); });
DoSafe(() => Console.WriteLine("hi"));
Console.ReadKey();
}
static void DoSafe(Action whatEva)
{
try
{
whatEva.Invoke();
}
catch (Exception)
{
}
}
Then again, if I ever come across code using above construct, I'll delete it without asking permission. And no, you don't need to call GC.Collect.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: if I ever come across code using above construct, I'll delete it without asking permission
I really can't decide whether to vote you up for that comment, or down for introducing the concept of "On Error Resume Next" to c#...
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I've been thinking about this a lot recently. So much code I see catches an exception, logs it, then just carries on.
I'm subscribing more and more to the idea (in general) that if something exceptional has happened, the application is now in an unstable state and should be shut down until investigation has found out what went wrong. Obviously exceptions which we sort of expect we can handle with try/catch blocks but for now anyway, I'm avoiding all exception handling for anything else except at the AppDomain level which logs it, emails an alert and Environment.Exit s.
I hate exceptions. In fact, I've just removed TcpListener/TcpClient from my vocabulary because they are too prone to throwing them. The other end disconnected - is that really that exceptional? I'd argue not. TcpClient.Connect - couldn't connect, that's because I have started the other end yet. Why is there no TryConnect??
It's interesting in interviews that they always ask about Exceptions - try/catch/finally - what happens to disposable objects etc. What they should ask is not how you do it, but when and why you should do it and how you are going to recover afterwards.
Well, if you ask me, that is.
Regards,
Rob Philpott.
|
|
|
|
|
I'd agree - knowing when to catch is more important than how, and recovery / reporting is very important. But I'd rather log and report to the user than bomb to the OS - I remember too well how much I hated that with older apps and I'd lose all my data because I pressed the wrong key. At least if you report and stay alive you give the user the opportunity to "copy'n'paste" his typing into something else even if you can't save it directly any more.
And "log and ignore" is much, much better than "ignore"!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Yes quite! I omitted to say that my day-to-day development is server side Windows Services stuff, it would be a different matter on the client!
Regards,
Rob Philpott.
|
|
|
|
|
Rob Philpott wrote: So much code I see catches an exception, logs it, then just carries on. Unless one can handle it, logging is all you can do. The idea would be that you look at the log (when dogfooding the app), and use it to stop logging the exceptions you DO expect, handle those you can, or provide a neat message to the user. You'll find that the amount of "unexpected" exceptions decline over time.
Rob Philpott wrote: I'm subscribing more and more to the idea (in general) that if something exceptional has happened, the application is now in an unstable state and should be shut down until investigation has found out what went wrong. Halleluja! You're 100% right; tryordie is the way to go. If you don't, you'll encounter an equivalent of backing up data, swallowing the exception that your backup failed and continue to wreck the only valid datastore.
Rob Philpott wrote: Why is there no TryConnect?? Write one.
If you expect the exception, you can handle it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Eddy Vluggen wrote: Write one.
Well I could, but I like to run the debugger with first chance exceptions being thrown, its a really powerful tool to track things that fail. Yes, I can ignore that particular exception but I'd rather not. The answers as it turns out is to use the socket class which isn't so fascist.
Regards,
Rob Philpott.
|
|
|
|
|
Add an attribute to that single method;
[DebuggerStepThrough]
static bool TryFail()
{
try
{
throw new NotImplementedException();
}
catch (NotImplementedException)
{
Debug.WriteLine("Caught expected NotImplementedException");
return false;
}
return true;
}
Rob Philpott wrote: Yes, I can ignore that particular exception but I'd rather not. Did you mean "ignore each instance of that exception", or "that particular catch in that specific method"?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
OriginalGriff wrote: I really can't decide whether to vote you up for that comment An unhelpful remark is not worth the upvote.
OriginalGriff wrote: or down for introducing the concept of "On Error Resume Next" to c#... Now that you mention it, there's an "abuse" button
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Oh, that crossed my mind as well!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|