[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

extract sentence based on words. C#

Posted on 2010-03-25
4
Medium Priority
?
1,194 Views
Last Modified: 2012-05-09
HI,
i  have text file and i want to extract sentences containing particular words and send it to a database

the problem is i have 6 arraylists and have the particular words their as they belong to 6 diff groups
i want to be able to loop through the text file get the sentences that belong to these groups based on the words in the arraylists and display each sentence, its group and the sentence after it.

am using c# vs 2008 .txt files

string uploadedFileName = TextBox1.Text;
        string uploadedFileFullPath = System.IO.Path.Combine(@"C:\DocUpload\", Path.GetFileName(uploadedFileName));
        StreamReader Filereader = File.OpenText(uploadedFileFullPath);
        string textline;
textline = Filereader.ReadToEnd();
        string[] sites = textline.Split(seperator);
        string[] commasite = textline.Split(commanseperator);
//string[] wordsToMatch;

        string[] wordsToMatch = (string[])cueContrast.ToArray(typeof(string));
        wordsToMatch.ToString().Split(' ');
        //wordsToMatch[0] = TextBox2.Text;

        // Find sentences that contain all the terms in the wordsToMatch array.

        // Note that the number of terms to match is not specified at compile time.

        var sentenceQuery = from sentence in commasite

                            let w = sentence.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },

                                                    StringSplitOptions.RemoveEmptyEntries)

                            where w.Distinct().Intersect(wordsToMatch).Any() == wordsToMatch.Any()

                            select sentence;

        foreach (string str in sentenceQuery)
        {

            Response.Write(str + ".");
            ListBox1.Items.Add(str);

        }

Open in new window

0
Comment
Question by:uddy
  • 2
  • 2
4 Comments
 

Author Comment

by:uddy
ID: 28574231
ArrayList cueContrast = new ArrayList();
        cueContrast.Add("but");
        cueContrast.Add("whereas");
        cueContrast.Add("however");

it the array t
0
 
LVL 30

Assisted Solution

by:Alexandre Simões
Alexandre Simões earned 1000 total points
ID: 28656610
What are you missing there on your code? The evaluation if the thing contains any of the words on the array?

Have a look at the attached code.

Cheers!
Alex
foreach (string k in cueContrast)
{
	if (sentence.Contains(k))
	{
		// do what you got to do with it.
	}

	// if you need it to be case insensitive the easiest way is this
	if (sentence.ToLower().Contains(k.ToLower()))
	{
		// do what you got to do with it.
	}
}

Open in new window

0
 

Accepted Solution

by:
uddy earned 0 total points
ID: 29123504
thanks i sorted it out
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 29172995
You can always post your solution and mark it as the accepted answer.
This way you leave the question on the knowledge base with the correct solution that solved your problem.

Cheers!
Alex
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Nadia
Suppose you use Uber application as a rider and you request a ride to go from one place to another. Your driver just arrived at the parking lot of your place. The only thing you know about the ride is the license plate number. How do you find your U…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses
Course of the Month7 days, 15 hours left to enroll

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question