Saturday, November 27, 2021

#1 2021-11-22 02:38:43 am

drsn
Member
Registered: 2020-12-01
Posts: 3

How can I excerpt a whole sentence from paragrapgh contaning a keyword

Hi!

I want to ask how can I excerpt a whole sentence from a paragraph which contains keyword?

Example: keyword= Australian
I want this sentence = ""It was an oversight but not a deliberate snub," he told The Australian newspaper."


Paragraph = "Matt Doran - from Channel 7 - flew from Sydney to London on 4 November to meet Adele for her only Australian interview about her new album, 30.
But after admitting during it that he had not listened to the album, Sony withheld the interview footage.
Doran apologised and said he had missed an email with a preview copy of the songs.
"It was an oversight but not a deliberate snub," he told The Australian newspaper. "This is the most important email I have ever missed."
He denied reports he had been suspended from Channel 7, after being absent from on-air duties."

My aim is excerpt any sentence which contain my keyword.  Thanks

Last edited by drsn (2021-11-22 02:39:51 am)


Filed under: TextEdit

Offline

 

#2 2021-11-22 04:56:46 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: How can I excerpt a whole sentence from paragrapgh contaning a keyword

First, I rewrote  your text as single paragraph, because your text is not single paragraph. Assuming you want process the paragraph and not the text.

Also, your text in the example has 2 sentences containing the keyword "Australian". So, you will get 2 sentences in the result. To got only second sentence, make the keyword more specific. For example do it "Australian newspaper".

Applescript:


set myKeyword to "Australian"

set aParagraph to "Matt Doran - from Channel 7 - flew from Sydney to London on 4 November to meet Adele for her only Australian interview about her new album, 30. But after admitting during it that he had not listened to the album, Sony withheld the interview footage. Doran apologised and said he had missed an email with a preview copy of the songs. \"It was an oversight but not a deliberate snub\", he told The Australian newspaper.\" This is the most important email I have ever missed.\""

set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"."}
set theSentences to text items of aParagraph
set AppleScript's text item delimiters to ATID

set filteredSentences to {}
repeat with aSentence in theSentences
   if aSentence contains myKeyword then
       set end of filteredSentences to contents of aSentence & "."
   end if
end repeat

if (count filteredSentences) = 1 then
   return filteredSentences as text
else
   display dialog "The result contains multiple matches.
So, provide more specific keyword."

   return filteredSentences
end if

Last edited by KniazidisR (2021-11-22 05:31:10 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-11-22 06:00:53 am

drsn
Member
Registered: 2020-12-01
Posts: 3

Re: How can I excerpt a whole sentence from paragrapgh contaning a keyword

Hej KniazidisR

Thank you very much for your help. I am new to AppleScript.  Your script working but  I cant append the result to an example.txt. How can I solve this problem. Thank you very much.

Offline

 

#4 2021-11-22 06:43:25 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2154

Re: How can I excerpt a whole sentence from paragrapgh contaning a keyword

Here is example. :

Applescript:


do shell script "echo " & filteredSentences as text & linefeed & " >> /users/123/Desktop/testFile.txt"

Replace return filteredSentences as text code line with this example. You should indicate full Posix path of your text file as well. Instead of mine.

Last edited by KniazidisR (2021-11-22 06:54:49 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-11-22 07:55:30 am

drsn
Member
Registered: 2020-12-01
Posts: 3

Re: How can I excerpt a whole sentence from paragrapgh contaning a keyword

Hi KniazidisR.

Thank you very much. It worked.:)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)