Code for file processing
Show older comments
Please, can someone help me inspect the Matlab code (below) to see what I need to correct? Thank you.
Examples of a verse:
- Mat 6:27
- Luk 2:52
Example of a version:
- NIV
- AMP
Example of a sentence:
- Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].
- Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man.
There are repetition of verses in the file, but in some cases, there are no repetitions.
For example, Mat 6:27 occurs two times, but Col 2:10 occurs only once.
Pseudo code (The Matlab code is below):
- Open the text file “stature.txt”
- Create a new file in MATLAB and name it “processed_stature.txt”
- Find a sentence that has the first bible verse and “NIV” in the file “stature.txt” and write that sentence in the file “processed_stature.txt”
- If there is no sentence that has the first bible verse and “NIV”, then find a sentence that has the first bible verse and “NKJV” and write that sentence in the file “processed_stature.txt”
- If there is no sentence that has the first bible verse and “NKJV”, then write the sentence of the first bible version that has the bible verse in the file “processed_stature.txt”
- Write the first other bible version that has the first bible verse in a square bracket.
- Move to the second bible verse in the file “stature.txt”
- Find a sentence that has the second bible verse and “NIV” and write that sentence in the file “processed_stature.txt”
- If there is no sentence that has the second bible verse and “NIV”, then find a sentence that has the second bible verse and “NKJV” and write that sentence in the file “processed_stature.txt”
- If there is no sentence that has the second bible verse and “NKJV”, then write the sentence of the first bible version that has the bible in the file “processed_stature.txt”
- Write first other bible version that has the first bible verse in a square bracket
- Move to the third bible verse in the file “stature.txt”
- Find a sentence that has the third bible verse and “NIV” and write that sentence in the file “processed_stature.txt”
- If there is no sentence that has the third bible verse and “NIV”, then find a sentence that has the third bible verse and “NKJV” and write that sentence in the file “processed_stature.txt”
- If there is no sentence that has the third bible verse and “NKJV”, then write the sentence of the first bible version that has the bible in the file “processed_stature.txt”
- Write first other bible version that has the first bible verse in a square bracket
- There should be no repetition of a verse in a sentence. So, a verse should be inspected once in the file "stature.txt"
- Continue this iteration till the end of the file “stature.txt”
- Open processed_stature.txt
% Open the input file "stature.txt"
inputFile = fopen('stature.txt', 'r');
if inputFile == -1
error('Error opening "stature.txt"');
end
% Create the output file "processed_stature.txt"
outputFile = fopen('processed_stature.txt', 'w');
if outputFile == -1
fclose(inputFile);
error('Error creating "processed_stature.txt"');
end
% Read the lines from the input file
lines = textscan(inputFile, '%s', 'delimiter', '\n');
lines = lines{1};
fclose(inputFile);
% Initialize variables for verse and versions
% I don't want to input the verses. I want the code to get the verses from the input file.
verses = {'Col 2:10', 'Luk 2:52', 'Mat 6:27', 'Luk 19:3', 'Luk 12:25', 'Eph 4:13'};
% I don't want to be inputting the versionPriority.
% I want the versionPriority to be generated in the code.
% The version priority should be 'NIV', 'NKJV', then the first version that
% contain the verse
versionPriority = {'NIV', 'NKJV', 'AMP'};
% Loop through the verses
for i = 1:numel(verses)
verse = verses{i};
foundSentence = '';
remainingVersions = '';
% Check each line for the current verse
for j = 1:numel(lines)
line = lines{j};
% Check if the line contains the current verse
if contains(line, verse)
% Check for the preferred version
for k = 1:numel(versionPriority)
version = versionPriority{k};
versionPattern = ['\(', version, '\)'];
if contains(line, versionPattern)
foundSentence = line;
versionIndex = strfind(line, versionPattern);
line(versionIndex:versionIndex+numel(versionPattern)-1) = '';
remainingVersions = [remainingVersions, '[', line, '] '];
break;
end
end
if ~isempty(foundSentence)
break;
end
end
end
% If the preferred version is not found, choose the first available version
if isempty(foundSentence)
for j = 1:numel(lines)
line = lines{j};
if contains(line, verse)
foundSentence = line;
remainingVersions = [remainingVersions, '[', line, '] '];
break;
end
end
end
% Write the processed sentence to the output file
fprintf(outputFile, '%s %s\n', foundSentence, remainingVersions);
end
fclose(outputFile);
disp('Processed "stature.txt". Output saved in "processed_stature.txt".');
stature.txt
Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26]
Luk 19:3 (AMP) Zaccheus was trying to see who Jesus was, but he could not see because of the crowd, for he was short in stature.
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].
Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man.
Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature?
Luk 2:52 (NKJV) And Jesus increased in wisdom and stature, and in favor with God and men.
Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature?
Luk 19:3 (NKJV) And he sought to see who Jesus was, but could not because of the crowd, for he was of short stature.
Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ;
Processed_stature.txt (the result the Matlab code is generating)
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power]. [Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].]
Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26] [Luk 2:52 (AMP) And Jesus kept increasing in wisdom and in stature, and in favor with God and men. [1Sa 2:26]]
Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature? [Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature?]
Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature? [Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature?]
Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ; [Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ;]
Expected processed_stature.txt (this is the result I want the code to generate)
Luk 2:52 (NIV) And Jesus grew in wisdom and stature, and in favor with God and man.
[NKJV, AMP]
Luk 19:3 (NKJV) And he sought to see who Jesus was, but could not because of the crowd, for he was of short stature.
[AMP]
Col 2:10 (AMP) And in Him you have been made complete [achieving spiritual stature through Christ], and He is the head over all rule and authority [of every angelic and earthly power].
[]
Mat 6:27 (NKJV) Which of you by worrying can add one cubit to his stature?
[]
Luk 12:25 (NKJV) And which of you by worrying can add one cubit to his stature?
[]
Eph 4:13 (NKJV) till we all come to the unity of the faith and of the knowledge of the Son of God, to a perfect man, to the measure of the stature of the fullness of Christ;
[]
Accepted Answer
More Answers (1)
Image Analyst
on 1 Aug 2023
Instead of
lines = textscan(inputFile, '%s', 'delimiter', '\n');
you might like to use readlines
lines = readlines(inputFile);
to get all the lines separately in one variable.
Otherwise it looks like you're on your way to success. If you need help, see
Categories
Find more on Historical Contests in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!