$page = Get-Item -Path '/sitecore/content/Habitat/Home/Modules/Feature/Accounts' | |
$links = [Sitecore.Globals]::LinkDatabase | |
$processed = New-Object System.Collections.ArrayList | |
# template names of items that will be linked from the presentation field that are not datasources | |
$skip = @('Device','Layout','View rendering', 'Controller rendering', 'Test Definition', 'Multivariate Test Variable') | |
# shared and final layout | |
$layouts = @('{F1A1FE9E-A60C-4DDB-A3A0-BB5B29FE732E}', '{04BF00DB-F5FB-41F7-8AB7-22408372A981}') | |
function GetContent($item, $layout = $False) | |
{ | |
# Prevent circular reference infinite recursion | |
if ($processed -contains $item.ID.ToString()) | |
{ | |
return @() | |
} | |
$processed.Add($item.ID.ToString()) | |
# Read content fields stripping out markup, quotes, and white spaces | |
$values = $item.Fields ` | |
| Where { $_.Name -notmatch '^__' } ` | |
| Select -ExpandProperty 'Value' ` | |
| % { $_ -replace "<[\w/][^<>]*[\w/]?>", ""} ` | |
| % { $_ -replace "[\s\t]+", " "} ` | |
| %{ $_ -replace '[''"]', ''} ` | |
| %{ $_ -replace '\n|(nbsp;)', ' '} ` | |
$content = @() | |
# Ignore GUIDs and single word values | |
$content += $values ` | |
| Where { $_ -notmatch '[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}' } ` | |
| Where { $_.Split(' ').Length -gt 1 } | |
# Recursively resolve references | |
$content += $values ` | |
| Where { $_ -match '[\d\w]{8}-[\d\w]{4}-[\d\w]{4}-[\d\w]{4}-[\d\w]{12}' } ` | |
| %{ $_.Split('|') | %{ $_ } } ` | |
| Where { [Sitecore.Data.ID]::IsID($_) } ` | |
| %{ Get-Item . -ID $_ } ` | |
| %{ GetContent $_ $False } | |
# Read content from datasources | |
if ($layout) | |
{ | |
$content += $links.GetReferences($item) ` | |
| Where { $_.SourceItemVersion -eq $item.Version.Number } ` | |
| Where { $layouts -contains $_.SourceFieldID.ToString() } ` | |
| Where { [Sitecore.Data.ID]::IsID($_.TargetItemID) } ` | |
| % { Get-Item . -ID $_.TargetItemID } ` | |
| Where { $skip -notcontains $_.Template.Name }` | |
| % { GetContent $_ $False } | |
} | |
return $content | |
} | |
$content = GetContent $page $True ` | |
| Where { $_ -match '\D+' } ` | |
| %{ $_ -replace '\.$', ''} ` | |
| Sort-Object ` | |
| Get-Unique | |
$text = [String]::Join('. ', $content) | |
$keywords = Invoke-WebRequest ` | |
-Uri 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases' ` | |
-Body "{'documents': [ { 'language': 'en', 'id': '$($page.ID.ToString())', 'text': '$text' } ]}" ` | |
-ContentType "application/json" ` | |
-Headers @{'Ocp-Apim-Subscription-Key' = '<use-your-own-key>'} ` | |
-Method 'Post' ` | |
-UseBasicParsing | ConvertFrom-Json | |
$sentiment = Invoke-WebRequest ` | |
-Uri 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment' ` | |
-Body "{'documents': [ { 'language': 'en', 'id': '$($page.ID.ToString())', 'text': '$text' } ]}" ` | |
-ContentType "application/json" ` | |
-Headers @{'Ocp-Apim-Subscription-Key' = '<use-your-own-key>'} ` | |
-Method 'Post' ` | |
-UseBasicParsing | ConvertFrom-Json | |
Write-Host "The text is $($sentiment.documents[0].score*100)% positive" | |
# Limit to keyw phrases where all words are capitalized | |
$keywords.documents[0].keyPhrases ` | |
| Where { $_ -cmatch '^([A-Z]\w+\s?)*$' } ` | |
| %{ Write-Host $_ } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment