Showing posts with label Bible. Show all posts
Showing posts with label Bible. Show all posts

Tuesday, January 23, 2018

Showing Bible Verse when PowerShell is Opened

Using the code created in a previous post in our PowerShell profile, we can have PowerShell display a random Bible quote whenever it is opened.

Steps

  1. Download PSBible-NET.psm1 from Github.
  2. Place the file in %UserProfile%\Documents\WindowsPowerShell\Modules.  You may need to create the folders if they do not already exist.
  3. Create "profile.ps1" in %UserProfile%\Documents\WindowsPowerShell\ and put this inside:

Import-Module "C:\Users\YOUR-USERNAME\Documents\WindowsPowerShell\Modules\PSBible-NET.psm1"

Get-BibleVerse -Random



Every time PowerShell is opened on your machine it will show a random Bible quote!


Keep in mind that the more commands you place inside the profile.ps1, the longer it will take to open PowerShell.

Sunday, January 21, 2018

Search the Internet for Bible References on a Topic

Scraping a Web Site for Bible References

Using the function from the prior post, we can leverage PowerShell to download a web page and parse it for us to find references to scripture.

$requestResult = Invoke-WebRequest -Uri "https://blogs.lcms.org/2018/loving-your-internet-neighbor" -DisableKeepAlive -UseBasicParsing -ErrorAction SilentlyContinue

if ($requestResult.StatusCode -eq 200)
{
    Get-BibleReferences $requestResult.Content | Format-Table -AutoSize
}


Output
Reference    Book    Chapter Verses From To
---------    ----    ------- ------ ---- --
1 Cor. 1:23  1 Cor   1       23     23     
1 PETER 3:15 1 PETER 3       15     15     
John+5:39    John    5       39     39     
John 5:39    John    5       39     39     
Luke 24:27   Luke    24      27     27      


Search the Internet for Bible References on a Topic

So let's take this a bit farther.  Let's say someone sends you an email wanting to know more about the Biblical position on, let's say, heaven and hell.  So you could fire up Google and spend the next half hour looking at the first ten posts on heaven and hell.  But really, we should take what scripture says first, then consider the opinions of others.  This lead me to wanting to look at only the Bible verses relating to Heaven or Hell.

I have built a function which will query Google, read the first page of results then download each of the results, scrape them for Bible references, and return not only the reference but also the Bible text in NET Bible format.  Although it takes about a minute to run it saves a bunch of time!

Get-BibleReferencesOnTopic "Lutheran Heaven Hell" | Select-Object Reference, Text | Export-Csv -Path "References.csv" -NoTypeInformation


Sample output (400 Bible References):


The code is available on Github!

Find Bible Verses using PowerShell

Using regular expressions and PowerShell we can create some code which will parse a Bible verse and then these values can be used in other ways.  In the function below I've used the .NET RegEx class to read Bible verses, which can be read from a text/CSV file into a variable.  It will find Bible verses within other text too.

function Get-BibleReferences {
    <#
        .SYNOPSIS
            Finds scripture references within the provided text.

        .DESCRIPTION
            Uses regular expressions to find references to the Bible and returns them as a set.

        .PARAMETER Text
            The text to parse for scripture references.

        .EXAMPLE
            PS C:\> Get-BibleReferences "Titus 3:1  Really any text can be here -- 1 Timothy 2:1-2 -" | Format-Table -AutoSize

            Reference       Book      Chapter Verses From To
            ---------       ----      ------- ------ ---- --
            Titus 3:1       Titus     3       1      1     
            1 Timothy 2:1-2 1 Timothy 2       1-2    1    2

        .NOTES
            Dag Calafell
            01.20.2018

        .LINK
            https://dynamicsax365trix.blogspot.com/
    #>
    [CmdletBinding(DefaultParameterSetName="Default")]
    param(
        [Parameter(ParameterSetName="Default",
            ValueFromPipelineByPropertyName=$true,
            Mandatory=$true,
            Position=0)]
        [ValidateNotNullOrEmpty()]
        [string[]]$text
    )

    $regex = new-object System.Text.RegularExpressions.Regex("(?(?:(?:[123]|I{1,3})\s*)?(?:[A-Z][a-zA-Z]+|Song of Songs|Song of Solomon)).?\s*(?1?[0-9]?[0-9]):\s*(?\d{1,3})(?:[,-]\s*(?\d{1,3}))*", [System.Text.RegularExpressions.RegexOptions]::MultiLine)
    $regexMatches = $regex.Matches($text)

    foreach ($match in $regexMatches)
    {
        $groups = $match.Groups
        $book         = $groups[1].Value
        $chapter      = $groups[2].Value
        $fromVerseNum = $groups[3].Value
        $toVerseNum   = $groups[4].Value

        $object = New-Object –TypeName PSObject
        $object | Add-Member –MemberType NoteProperty –Name Reference –Value $groups[0].Value
        $object | Add-Member –MemberType NoteProperty –Name Book –Value $book
        $object | Add-Member –MemberType NoteProperty –Name Chapter –Value $chapter

        if ($groups[4].Success)
        {
            $object | Add-Member –MemberType NoteProperty –Name Verses –Value ("{0}-{1}" -f $fromVerseNum, $toVerseNum)
            $object | Add-Member –MemberType NoteProperty –Name From –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name To –Value $toVerseNum
        }
        else
        {
            $object | Add-Member –MemberType NoteProperty –Name Verses –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name From –Value $fromVerseNum
            $object | Add-Member –MemberType NoteProperty –Name To –Value ""
        }

        # Return the info
        $object
    }
}

# Example
Get-BibleReferences "Titus 3:1  Really any text can be here -- 1 Timothy 2:1-2 -" | Format-Table -AutoSize

Output
Reference       Book      Chapter Verses From To
---------       ----      ------- ------ ---- --
Titus 3:1       Titus     3       1      1      
1 Timothy 2:1-2 1 Timothy 2       1-2    1    2 

This code is part of a larger script I'm working on to take a file of scripture references and return the full text to aid me in developing Bible studies.

Credit goes to RegexLib for the starting regex that I modified capture the data into groups.  Many times it is easier to find something to start with and extend it to what is desired.