Visit Our UNIX and Linux User Community


How to extract a sentences of word from a text file.


 
Thread Tools Search this Thread
Top Forums Programming How to extract a sentences of word from a text file.
# 1  
Old 04-22-2009
How to extract a sentences of word from a text file.

Hi ,

i have a text file that contain a story

How do i extract the out all the sentences that contain the word Mon. in C++

I only want to show those sentences that contain the word mon

eg.
Monkey on a tree.
Rabbit jumping around the tree.
I am very rich, I have lots of money.
Today is monday.
tomorrow is tuesday.

but only those words that contain the word mon is show.
Monkey on a tree
<Press enter to continue or q to quit>
I have lots of money
<Press enter to continue or q to quit>
Today is monday
<Press enter to continue or q to quit>

any advices on that


below is my codes, but it come out as a chunk of word that i search for.

Code:
#include <iostream>
#include <fstream>

using namespace std;


int main()

{
ifstream fin;
char filename[50];
string searchword;
string s;
int count;

cout << "Enter filename (including .txt): ";
cin >> filename;

fin.open(filename);

// stores Search Word
string askforsearchword();
searchword = askforsearchword();

// ends program if no txt file is found
if (!fin.good())
{
cout << "File not found" << endl;
system("pause");
return 1;
}

while(!fin.eof())
{

// use getline to read entire line 

getline(fin, s);


int i, position;

//converts text to all lowercase

for (i=0; i<s.length();i++)
s[i]=tolower(s[i]);
for (i=0; i<searchword.length();i++)
searchword[i]=tolower(searchword[i]); 


position = s.find(searchword);


if (position != string :: npos)

{

cout << s << endl;

}


} 


fin.close();


cout << "End Of Search." << endl;

system("pause");
return 0;

}

//function to ask for search word.
string askforsearchword()
{
string search;
cout<< "Type in your Search Word: " << endl;
cin >> search;
return search;
}

# 2  
Old 04-28-2009
This sounds like homework, but since you have actually posted some working code, I'll give you the benefit of the doubt.

You must concatenate s until you find an end-of-sentence delimiter. An end-of-sentence delimiter should look like one of: ". ", "! ", "? ". So first, you do a find for each of these strings, and if not sound, you concatenate the entire line to s. When you do find one, you split the line, concatenating the first part to s, and leaving the remainder for the next sentence (new variable). Then you search s for your target string. Then you replace s with the remainder of the sentence, and continue your loop.

Previous Thread | Next Thread
Test Your Knowledge in Computers #107
Difficulty: Easy
Each Unix kernel subsystem has key features like concurrency, virtual memory, paging, and a virtual file system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract sentence and its details from a text file based on another file of sentences

Hi I have two text files. The first file is TEXTFILEONE.txt as given below: <Text Text_ID="10155645315851111_10155645333076543" From="460350337461111" Created="2011-03-16T17:05:37+0000" use_count="123">This is the first text</Text> <Text Text_ID="10155645315851111_10155645317023456"... (7 Replies)
Discussion started by: my_Perl
7 Replies

2. Shell Programming and Scripting

Extract all the sentences from a text file that matches a pattern list

Hi I have a big text file. I want to extract all the sentences that matches at least 70% (seventy percent) of the words from each sentence based on a word list called A. Say the format of the text file is as given below: This is the first sentence which consists of fifteen words... (4 Replies)
Discussion started by: my_Perl
4 Replies

3. Shell Programming and Scripting

Extract all the sentences that matched two patterns

Hi I have two lists of patterns named A and B consisting of around 200 entries in each and I want to extract all the sentences from a big text file which match atleast one pattern from both A and B. For example, pattern list A consists of : ama ani ahum mari ... ... and pattern... (1 Reply)
Discussion started by: my_Perl
1 Replies

4. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

5. Shell Programming and Scripting

extract a word from text file name

Hi i want to extract the word present before .txt in the text file. For example, Sample_ab_a.txt ----------> i need 'a' Sample_abc_b.txt -----------> i need 'b' Can anyone help me in getting the word extracted (5 Replies)
Discussion started by: Sindhuap
5 Replies

6. Shell Programming and Scripting

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot (6 Replies)
Discussion started by: jamestan
6 Replies

7. UNIX for Dummies Questions & Answers

extracting sentences that only contain a word

Hi guys Need your help how do I extract sentences with only a word i.e. today is hot hot very humid humid2 Sample output hot very (0 Replies)
Discussion started by: jamestan
0 Replies

8. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

9. Shell Programming and Scripting

Perl: array, assigning multi-word sentences with quotes

Just wondering if there's a better way to get these complete sentences into an array and keep the quotes intact? All the quotes make it look ugly to me but it works. I want to be able to refer to the full sentences by index. I've tried a few qw and qq/ aproaches but what I have below seems about... (4 Replies)
Discussion started by: gctaylor
4 Replies

10. Shell Programming and Scripting

Anyways to find sentences with data format and extract it???

Hi guys,i got this problem which is..i need to find those sentences with date inside and extract them out,the input is somehow like this eg: $DATA42.GANTRY2.GA161147 DISKFILE 2007-10-16 11:56:45 SUPER.OPR \NETS.$Y4CB.#IN ... (4 Replies)
Discussion started by: cyberray
4 Replies

Featured Tech Videos