TisaneLampClient.psm1

#Region '.\Private\Add-Emoji.ps1' 0
Function Add-Emoji($emoji, $longName, $shortName) { 
    $properNoun = ''
    $hypernym = 0
    if ($shortName -like ':flag-*') { 
      $longName = [cultureinfo]::GetCultureInfo("en-US").TextInfo.ToTitleCase($longName)
      $properNoun = '1'
      $hypernym = 54389
    }
    if ($longName -like 'woman*') {
        $assign = "5=F"
        $longName = $longName -replace "woman"
    }
    if ($longName -like 'man*') {
        $assign = "5=M"
        $longName = $longName -replace "man"
    }
    if ($longName -like '*:*') {  $longName = $longName.Substring(0, $longName.IndexOf(':')) }
    if ($longName -like '*symbol') { $longName = $longName -replace "symbol" }
    if ($longName -like '*sign') { $longName = $longName -replace "sign" }
    if ($longName -like '*with*') { $longName = $longName.Substring(0, $longName.IndexOf('with'))  }
    if ($longName -like 'squared*') { $longName = $longName -replace "squared" }
    if ($longName -like 'open*') { $longName = $longName -replace "open" }
    if ($longName -like 'closed*') { $longName = $longName -replace "closed"}
    if ($longName -like 'oncoming*') { $longName = $longName -replace "oncoming" }
    if ($longName -like 'large*') { $longName = $longName -replace "large" }
    if ($longName -like 'small*') { $longName = $longName -replace "small" }

    if ($longName -like '*orange*' ) {  $longName = $longName.Substring($longName.IndexOf('orange')) }
    if ($longName -like '*yellow*') { $longName = $longName.Substring($longName.IndexOf('yellow')) } 
    if ($longName -like '*green*') { $longName = $longName.Substring($longName.IndexOf('green')) } 
    if ($longName -like '*purple*') { $longName = $longName.Substring($longName.IndexOf('purple')) } 
    if ($longName -like '*brown*') { $longName = $longName.Substring($longName.IndexOf('brown')) } 
    if ($longName -like '*face') { $longName = $longName -replace "face" }
    if ($longName -like '*hand') { $longName = $longName -replace "hand"}
    if ($longName -like '*ing') {
        $longName = $longName -replace "ing"
        $feature = 'VERB'
    }
    $longName = [System.Web.HttpUtility]::UrlEncode($longName.Trim())
    $shortName = [System.Web.HttpUtility]::UrlEncode($shortName.Trim())
    $emoji = $emoji.Trim()
    $encodedAssign = [System.Web.HttpUtility]::UrlEncode($assign)
    $uri = "$global:lampHost/import?lexeme=$emoji&reference=$longName&proper=$properNoun&hypernym=$hypernym&source=$source&orgId=$shortName&assign=$assign"
    #$uri
    $response = Invoke-WebRequest -Uri $uri -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
    #$uri
    if ($assign) {
      $assign = "51=EXPL,$assign"
    } else {
      $assign = "51=EXPL"
    }
    $encodedAssign = [System.Web.HttpUtility]::UrlEncode($assign)
    $uri = "$global:lampHost/import?lexeme=$shortName&reference=$longName&proper=$properNoun&hypernym=$hypernym&source=$source&orgId=$shortName&assign=$encodedAssign"
    $uri
    $response = Invoke-WebRequest -Uri $uri -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
    #$response
}
#EndRegion '.\Private\Add-Emoji.ps1' 57
#Region '.\Private\Add-Lexeme.ps1' 0
Function Add-Lexeme($familyId, $word) {
    Write-Host "Creating lexeme $word and linking it to the newly created family $familyId`r`n" -ForegroundColor Green 
   # 1. Insert a new lexeme
    $newLexeme = @{
      id=0
      lemma=$word
      stem=$word
    }
    $lexemeJson = ConvertTo-Json -InputObject $newLexeme
    $response = Invoke-WebRequest -Uri "$global:lampHost/lexeme" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($lexemeJson)) -UseBasicParsing
    #'Lexeme insert response: ' + $response
    $parsedResponse = ConvertFrom-Json -InputObject $response
    if ($parsedResponse.success -and $parsedResponse.id) {
      $lexemeId = $parsedResponse.id
      # 2. Link to the family
      $whatever = Invoke-WebRequest -Uri "$global:lampHost/lexemeFamilies?lexeme=$lexemeId&families=$familyId" -Method POST -Headers $global:authorizationToken -UseBasicParsing
      # 3. Tag
      $newLexeme.id = $lexemeId
      $lexemeJson = ConvertTo-Json -InputObject $newLexeme
      #"Tagging: " + $lexemeJson
      $taggedLexeme = Invoke-RestMethod -Uri "$global:lampHost/tagLemma" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($lexemeJson)) -UseBasicParsing
      #'Tagged lexeme: ' + $taggedLexeme
      #$parsedtaggedLexeme = ConvertFrom-Json -InputObject $taggedLexeme
      # 4. Update from the tag results
      $newLexeme.grammar = $taggedLexeme.grammar
      $newLexeme.stem = $taggedLexeme.stem
      #"Updating: " + $lexemeJson
      $response = Invoke-RestMethod -Uri "$global:lampHost/lexeme?id=$lexemeId" -Method GET -Headers $global:authorizationToken -UseBasicParsing
      #'Updating lexeme: ' + $response
      $newLexeme.requestId = $response.requestId
      $lexemeJson = ConvertTo-Json -InputObject $newLexeme
      $response = Invoke-RestMethod -Uri "$global:lampHost/lexeme" -Method PUT -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($lexemeJson)) -UseBasicParsing
      #$response
    }
  
  }
  
#EndRegion '.\Private\Add-Lexeme.ps1' 37
#Region '.\Public\Copy-InflectionPatterns.ps1' 0
## =============================================================================
##
## Copies a set of inflection patterns, replacing advanced criteria and / or strings
##
## =============================================================================
function Copy-InflectionPatterns {
[CmdletBinding()] 
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Language ID: ")][int] $language,
     [Parameter(Mandatory = $true, HelpMessage="Range ID: ")][int] $range,
     [Parameter(Mandatory = $false, HelpMessage="Advanced criteria to replace: ")][int[]] $oldCriteria,
     [Parameter(Mandatory = $false, HelpMessage="Advanced criteria to replace with: ")][int[]] $newCriteria,
     [Parameter(Mandatory = $false, HelpMessage="Replace strings: ")][String[]] $replaceRegexes, # an array of Tisane feature values
     [Parameter(Mandatory = $false, HelpMessage="Replace with: ")][String[]] $replaceWith,
     [Parameter(Mandatory = $false, HelpMessage="Old trigger: ")][int] $oldTrigger,
     [Parameter(Mandatory = $false, HelpMessage="New trigger: ")][int[]] $newTriggers
   )

if (-not($replaceRegexes -or $oldCriteria -or ($oldTrigger -gt 0 -and $newTriggers))) {
  Write-Host "Nothing to replace. Either supply an array of regexes/strings to replace (under replaceRegexes parameter, array of strings) or an array of criteria (under oldCriteria parameter, array of integers), or new triggers" -ForegroundColor Red
  Break
}
if ($replaceRegexes -and (-not($replaceWith) -or $replaceRegexes.Length -ne $replaceWith.Length)) {
  Write-Host "replaceRegexes and replaceWith must have the same number of elements" -ForegroundColor Red
  Break
}
if ($oldCriteria -and (-not($newCriteria) -or $oldCriteria.Length -ne $newCriteria.Length)) {
  Write-Host "oldCriteria and newCriteria must have the same number of elements" -ForegroundColor Red
  Break
}

## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

# $authenticationBody = '["' + $user + '", "' + $password + '"]'
# [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls
# $productionAuthentication = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)
Login-Lamp
 
# $somethingUnimportant = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=$language" -Method POST -Headers $global:authorizationToken -Body '' -UseBasicParsing

Set-LampLanguage -languageId $languageId
$affixList = Invoke-RestMethod -Uri "$global:lampHost/affixList?tagging=false&arg=$range&type=range" -Headers $global:authorizationToken -UseBasicParsing

## create an array of inflection IDs to retrieve and copy
$affixList | ForEach-Object {
  $_.inflectionRules | ForEach-Object { 
    $currentInflectionPatternId = $_.inflectionRuleId
    #"Passsing $currentInflectionPatternId"
    $inflectionPattern = Invoke-RestMethod -Uri "$global:lampHost/inflectionRule?id=$currentInflectionPatternId" -Headers $global:authorizationToken -UseBasicParsing
    $changed = $false
    $targetTriggers = @()
    $targetTriggers += $inflectionPattern.triggerId
    if ($oldTrigger -eq $inflectionPattern.triggerId) {
      $inflectionPattern.triggerId = 0 # $newTrigger
      $targetTriggers = $newTriggers
      $changed = $true
    }
    $originalPatternId = $inflectionPattern.id
    $inflectionPattern.affixes | ForEach-Object {
      if ($oldCriteria) {
        $indexInLabelArray = [array]::IndexOf($oldCriteria, $_.advancedCriteriaId)
        #"Index: $indexInLabelArray"
        #"Advanced criteria: " + $_.advancedCriteriaId
        if ($indexInLabelArray -gt -1) {
          $_.advancedCriteriaId = $newCriteria[$indexInLabelArray]
          $changedAdvancedCriteriaId = $_.advancedCriteriaId
          $changed = $true
          Write-Host "New advanced criteria in inflection pattern $originalPatternId : $changedAdvancedCriteriaId" -ForegroundColor Green
        }
      }
      if ($replaceRegexes) {
        For ($i = 0; $i -lt $replaceRegexes.length; $i++) {
          $currentRegex = $replaceRegexes[$i]
          $currentReplacement = $replaceWith[$i]
          $affixText = $_.affix
          #"Replacing $currentRegex with $currentReplacement in $affixText"
          $_.affix = $affixText -replace $currentRegex, $currentReplacement
          if ($affixText -ne $_.affix) {
            $affixText = $_.affix
            $changed = $true
            Write-Host "New affix text in inflection pattern $originalPatternId : $affixText" -ForegroundColor Green
          }
        }
      }
    }
    if ($changed) {
      "New pattern will be created based on pattern $originalPatternId"
      $inflectionPattern.id = 0 ## when adding new inflection patterns, it has to be set to 0
      $targetTriggers | ForEach-Object {
        $inflectionPattern.triggerId = $_
        if ($newTriggers) {
            $currentTrigger = $_
            Write-Host "Copying to trigger $currentTrigger" -ForegroundColor Green
            $inflectionPattern
        }
        ##$inflectionPattern | Get-Member
        $outJson = $inflectionPattern | ConvertTo-Json -Depth 30
        #$outJson
        ##Read-Host -Prompt "Press Enter to continue"
        ## Non-ASCII strings have issues. Workaround from here: https://stackoverflow.com/questions/15290185/invoke-webrequest-issue-with-special-characters-in-json
        $response = Invoke-WebRequest -Uri "$global:lampHost/inflectionRule" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($outJson)) -UseBasicParsing
      }
    }
  }
}
}
#EndRegion '.\Public\Copy-InflectionPatterns.ps1' 126
#Region '.\Public\Edits-2CSV.ps1' 0
function Edits-2CSV {
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Filename")][String] $csv,
     [Parameter(Mandatory = $true, HelpMessage="Target user")][String] $targetUser
   )
   
   


# BEGIN fix for Powershell bug: in some cases, the configuration files aren't read properly
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
# END fix for Powershell bug: in some cases, the configuration files aren't read properly


## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Web;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


# Write-Progress -Activity "Authenticating" -Status "Normally takes a couple of seconds"
# $productionHost = 'https://lampws.tisane.ai:443'

# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$productionHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $authorizationToken = @{}
# $authorizationToken.Add('Authorization', $inJson.token)
# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

#$fileLines = Get-Content $csv

#$whatever = Invoke-WebRequest -Uri "$productionHost/setLanguage?language=$languageId" -Method POST -Headers $authorizationToken -Body ' '

Login-Lamp
Set-LampLanguage -languageId $languageId

$edits = Invoke-RestMethod -Uri "$global:lampHost/editList?arg=1176&type=range&user=$targetUser" -Method GET -Headers $global:authorizationToken -UseBasicParsing

$outContent = 'Transaction ID, Action, Data Table, Date/time, Lemma'

$edits | ForEach-Object {
  $lemma = ''
  if ($_.requestBody) {
    $parsedRequestBody = ConvertFrom-Json -InputObject $_.requestBody
    if ($parsedRequestBody.lemma) {
      $lemma = $parsedRequestBody.lemma
    }
  }
  $line = "" + $_.transaction + "," + $_.action + "," + $_.table + "," + $_.datetime + ", $lemma"
  $outContent = $outContent + [Environment]::NewLine + $line       
}

$outContent | Set-Content $csv -encoding UTF8

[gc]::Collect()
[gc]::WaitForPendingFinalizers()
}
#EndRegion '.\Public\Edits-2CSV.ps1' 78
#Region '.\Public\Import-CustomJson.ps1' 0
function Import-CustomJson {
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Filename including path: ")][String] $path,
     [Parameter(Mandatory = $true, HelpMessage="Corpora ID: ")][int] $corpora
)


$fileSize = (Get-Item $path).Length
$PROGRESS_UPDATE_INTERVAL = 10
$lastReported = 0
$processed = 0

$TISANE_PATH = 'C:\Tisane\TestConsole\'
$config_path = $TISANE_PATH + "Tisane.TestConsole.exe.Config"

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path) # assign the configuration file

# BEGIN fix for Powershell bug: in some cases, the configuration files aren't read properly
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
# END fix for Powershell bug: in some cases, the configuration files aren't read properly

[Reflection.Assembly]::LoadFrom($tisane_path + "Tisane.Runtime.dll") # load the type

## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Web;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


# Write-Progress -Activity "Authenticating" -Status "Normally takes a couple of seconds"
# $productionHost = 'https://lampws.tisane.ai:443'

# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$productionHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $authorizationToken = @{}
# $authorizationToken.Add('Authorization', $inJson.token)
# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
# $whatever = Invoke-WebRequest -Uri "$productionHost/setLanguage?language=7" -Method POST -Headers $authorizationToken -Body ' '


Login-Lamp
Set-LampLanguage -languageId $languageId

$r = [IO.File]::OpenText($path)
$startedAt = Get-Date
"Everything is loaded. Starting at " + $startedAt.ToString('T')
$outContent = ''
$detected = 0
$control = 'not yet'
$totalPostCount = 0
while ($r.Peek() -ge 0) {
    $line = $r.ReadLine()
    #$processed += $line.Length
    # Process $line here...
    $inJson = ConvertFrom-Json -InputObject $line
    $objectCount = $inJson.length
    $inJson | ForEach-Object {
      $totalPostCount += 1
      $processed += $line.Length / $objectCount
      if ($processed - $lastReported -gt $PROGRESS_UPDATE_INTERVAL) {
        $pct = ($processed / $fileSize) * 100
        $timeTaken = ((Get-Date) - $startedAt).TotalMinutes
        Write-Progress -Activity "Importing $path" -Status "$pct% complete" -PercentComplete $pct

        $lastReported = $processed
      }
      $content = [System.Web.HttpUtility]::UrlEncode($_.body)
      
      $response = Invoke-WebRequest -Uri "$global:lampHost/testfragment?corpora=$corpora&fragment=$content&critical=false&test=true" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
      #$response
    }
}
#"Control: $control"
$r.Dispose()
# save content to a new file
$finishedAt = Get-Date
$timeTaken = ($finishedAt - $startedAt).TotalMinutes
"Done at $finishedAt after $timeTaken minutes"
}
#EndRegion '.\Public\Import-CustomJson.ps1' 100
#Region '.\Public\Import-CX.ps1' 0
# =======================================================================================
# Imports lexemes from Carabao eXchange XML files
# =======================================================================================
# script parameters
function Import-CX {
  [CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Filename")][String] $cx,
     [Parameter(Mandatory = $true, HelpMessage="Language ID")][String] $languageId,
     [Parameter(Mandatory = $true, HelpMessage="Feature value")][String] $feature,
     [Parameter(Mandatory = $false, HelpMessage="Feature ID")][String] $featureId,
     [Parameter(Mandatory = $false, HelpMessage="Source")][String] $source
   )
   

# read the content of the Carabao eXchange XML and parse it
[xml]$dictX = Get-Content $cx


if (-not($featureId)) {
  # if the feature ID was not supplied, use part of speech
  $featureId = 1
}
# BEGIN fix for Powershell bug: in some cases, the standard .NET configuration files (.config) aren't read properly
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
# END fix for Powershell bug: in some cases, the standard .NET configuration files (.config) aren't read properly


## fix the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Web;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

# # log on to LaMP, and get the authentication token
# Write-Progress -Activity "Authenticating" -Status "Normally takes a couple of seconds"
# $productionHost = 'https://lampws.tisane.ai:443'
# $authenticationBody = '["' + $user + '", "' + $password + '"]'
# $productionAuthentication = Invoke-WebRequest -Uri "$productionHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $authorizationToken = @{}
# $authorizationToken.Add('Authorization', $inJson.token)
# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

# # set active language
# $whatever = Invoke-WebRequest -Uri "$productionHost/setLanguage?language=$languageId" -Method POST -Headers $authorizationToken -Body ' ' -UseBasicParsing
Login-Lamp
Set-LampLanguage -languageId $languageId
# process XML entries from Carabao eXchange
$dictX.list.row | ForEach-Object {
    $word = $_.native
    $referenceWord = $_.english
    if ($word -and $referenceWord) {
      # we have the word and its translation to English. Call the POST /import method
      "Importing $word -> $referenceWord"
      $response = Invoke-WebRequest -Uri "$global:lampHost/import?lexeme=$word&reference=$referenceWord&proper=1&hypernym=0&featureList=$featureId&featureValue=$feature&source=$source&orgId=$referenceWord" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
      #$response
    }
}

# clean up RAM
[gc]::Collect()
[gc]::WaitForPendingFinalizers()
}
#EndRegion '.\Public\Import-CX.ps1' 81
#Region '.\Public\Import-Emoji.ps1' 0
function Import-Emoji{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Filename")][String] $csv,
     [Parameter(Mandatory = $true, HelpMessage="Language ID")][String] $languageId,
     [Parameter(Mandatory = $false, HelpMessage="Feature ID")][String] $featureId,
     [Parameter(Mandatory = $false, HelpMessage="Source")][String] $source
   )
   


# BEGIN fix for Powershell bug: in some cases, the configuration files aren't read properly
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
# END fix for Powershell bug: in some cases, the configuration files aren't read properly


## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Web;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


# Write-Progress -Activity "Authenticating" -Status "Normally takes a couple of seconds"
# $productionHost = 'http://94.237.77.124:8001'
# #$productionHost = 'https://lampws.tisane.ai:443'

# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$productionHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $authorizationToken = @{}
# $authorizationToken.Add('Authorization', $inJson.token)
# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Login-Lamp
$fileLines = Get-Content $csv
Set-LampLanguage -languageId $languageId
# $whatever = Invoke-WebRequest -Uri "$productionHost/setLanguage?language=$languageId" -Method POST -Headers $authorizationToken -Body ' '

$fileLines | ForEach-Object {
    $emoji, $longName, $shortName = $_.Split(",")
    AddEmoji -emoji $emoji -longName $longName -shortName $shortName
}

[gc]::Collect()
[gc]::WaitForPendingFinalizers()
}
#EndRegion '.\Public\Import-Emoji.ps1' 64
#Region '.\Public\Import-LexemesFromWiktionary.ps1' 0
## =============================================================================
##
## This script's purpose is to set features from Wiktionary. MUST BE RUN ON THE LAMP SERVER
##
## =============================================================================
function Import-LexemesFromWiktionary{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Language code: ")][String] $language,
     [Parameter(Mandatory = $true, HelpMessage="Path: ")][String] $path,
     [Parameter(Mandatory = $true, HelpMessage="Part of speech: ")][String] $pos,
     [Parameter(Mandatory = $false, HelpMessage="Prune Feature List ID: ")][int] $pruneListId,
     [Parameter(Mandatory = $false, HelpMessage="Prune Feature Value: ")][String] $pruneValue,
     [Parameter(Mandatory = $false, HelpMessage="Feature List ID: ")][int] $listId,
     [Parameter(Mandatory = $false, HelpMessage="Wiktionary labels: ")][String[]] $labels,
     [Parameter(Mandatory = $false, HelpMessage="Feature values: ")][String[]] $values, # an array of Tisane feature values
     [Parameter(Mandatory = $true, HelpMessage="Wiktionary category: ")][String] $category,
     [Parameter(Mandatory = $false, HelpMessage="Complement existing lexemes: ")][bool] $complement,
     [Parameter(Mandatory = $false, HelpMessage="Regex to verify: ")][String] $validatingRegex
   )
   
class Feature {
  [string]$index
  [string]$value
  [string]$type
  
  Feature([string]$index,
    [string]$value,
    [string]$type) {
    $this.index = $index
    $this.value = $value
    $this.type = $type
  }
}

# $config_path = $path + "Tisane.TestConsole.exe.config"
# [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
  
# [Reflection.Assembly]::LoadFrom($path + "Tisane.Runtime.dll")


# $authenticationBody = '["' + $user + '", "' + $password + '"]'
# [Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
# [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls
# $global:productionHost = 'https://lampws.tisane.ai:443'
# $productionAuthentication = Invoke-WebRequest -Uri "$global:productionHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)
Login-Lamp
$languageJSON = Invoke-RestMethod -Uri "$global:productionHost/languages" -Method GET -UseBasicParsing
$languageNamesToCodes = @{}
$languageID = 0
$languageJSON | foreach {
  if ($_.ISOCode -eq $language) {
    $languageID = $_.id
    $languageName = $_.englishName
    $commaPos = $languageName.IndexOf(',')
    if ($commaPos -gt 0) {
      $languageName = $languageName.Substring(0, $commaPos)
    }
    $languageNamesToCodes.Add($languageName, $_.ISOCode)
  }
}

$languageNamesToCodes.Add('Norwegian Bokmål', 'no')
$languageNamesToCodes.Add('Norwegian Nynorsk', 'no')

# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

# $whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=$languageID" -Method POST -Headers $authorizationToken -Body ' ' -UseBasicParsing
Set-LampLanguage -languageId $languageId
$posFeatureList = '1'

$tisanePOSValue = $pos.ToUpper()
switch ($tisanePOSValue) {
  'ADJECTIVE' { $tisanePOSValue = 'ADJ' }
  'ADVERB' { $tisanePOSValue = 'ADV' }
  'PREPOSITION' { $tisanePOSValue = 'PREP' }
  'POSTPOSITION' { $tisanePOSValue = 'PREP' }
  'CONJUNCTION' { $tisanePOSValue = 'CJ' }
  'INTERJECTION' { $tisanePOSValue = 'INTJ' }
  'PHRASE' { $tisanePOSValue = '' }
  'PROVERB' { $tisanePOSValue = 'FORE' }
  'IDIOM' { $tisanePOSValue = '' }
  'NUMERAL' { $tisanePOSValue = 'NOUN' }
  'PREPOSITIONAL PHRASE' { $tisanePOSValue = '' }
  'PREPOSITIONAL_PHRASE' { $tisanePOSValue = '' }
  'POSTPOSITIONAL PHRASE' { $tisanePOSValue = '' }
  'POSTPOSITIONAL_PHRASE' { $tisanePOSValue = '' }
}


if ($tisanePOSValue.Length -lt 1) {
  $posFeatureList = '0'
}

$tagsFound = @()

$articleCount = 0
$sComplement = '0'
if ($complement) {
  $sComplement = '1'
}

do {
$wikidataUrl = "https://en.wiktionary.org/w/api.php?action=query&generator=categorymembers&format=json&gcmtitle=Category:$category&prop=pageprops&gcmlimit=500&gcmcontinue=$bookmark"
"Loading entries from $wikidataUrl"
$wikidataResponse = Invoke-WebRequest -Uri $wikidataUrl -Method GET -UseBasicParsing
$wikidataResponse = $wikidataResponse.Content # -replace '(?<=pages["]:{["])[^"]+', 'results'
$listOfInstances = ConvertFrom-Json -InputObject $wikidataResponse
if ($listOfInstances.continue) {
  $bookmark = $listOfInstances.continue.gcmcontinue
} else {
  $bookmark = $null
}
#"Bookmark: $bookmark"
$listOfInstances.query.pages.PSObject.Properties | foreach {
  $word = $_.Value.title
  #$word
  if ($word -and (-not ($validatingRegex) -or $word -match $validatingRegex)) {
      $articleCount += 1
      $pageId = $_.Value.pageid
      $normalizedWord = $word -replace ' ', '_'
      #"PageId=$pageId word=$normalizedWord"
      try {
        $wiktionaryParser = New-Object Tisane.Helper.EnglishWiktionaryParser -ArgumentList ($normalizedWord, $languageNamesToCodes)
        $fullDoc = $wiktionaryParser.ToJson()
        $articleJSON = (($fullDoc | Where Key -eq $language).Value | Where Key -eq $pos).Value
        if ($articleJSON) {
          if ($language -like 'zh-*') {
            #$sFullDoc = $fullDoc.ToString()
            $orgWord = $word
            switch ($language) {
              'zh-CN' { $word = $fullDoc['simplified'].ToString() -replace '"', '' }
              'zh-TW' { $word = $fullDoc['traditional'].ToString() -replace '"', '' }
            }
            if ($word.Length -gt $orgWord.Length) {
              Write-Host "Chinese script adjustment inconsistent: $word / $orgWord" -ForegroundColor Red
              $word = ''
            }
            #"Result: $orgWord / $word -> $sFullDoc"
          }
          $sArticleJSON = $articleJSON.ToString()
          if ($sArticleJSON) {
            #"Parsing: $sArticleJSON"
            $article = ConvertFrom-Json -InputObject $sArticleJSON
            $referenceWords = ''
            $tag = $article.tag
            $article.interpretations | ForEach-Object {
              $english = $_.english
              if ($english) {
                $referenceWords = $english -join ","
                if ($labels) {
                  $indexInLabelArray = [array]::IndexOf($labels, $tag)
                }
                $assign = ""
                if ($indexInLabelArray -gt -1 -and $values) {
                  if ($values[$indexInLabelArray] -like '*=*') {
                    $assign = "assign=" + [System.Web.HttpUtility]::UrlEncode($values[$indexInLabelArray])
                  } else {
                    $assign = "assign=" + [System.Web.HttpUtility]::UrlEncode("$listId=" + $values[$indexInLabelArray])
                  }
                }
                $assignments = @()
                if ($pos -eq 'Noun' -and ($tag -eq 'f' -or $tag -eq 'm')) {
                  $tag = $tag.ToUpper()
                  $assignments += [System.Web.HttpUtility]::UrlEncode("5=$tag")
                }
                if ($_.figurative -or $_.idiomatic) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("51=FS")
                }
                if ($_.literary) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("40=BOOK")
                }
                if ($_.aspect -or $tag -eq 'impf' -or $tag -eq 'pf') {
                  $aspect = $_.aspect
                  if (-not($aspect)) {
                    $aspect = $tag
                  }
                  switch ($aspect) {
                    'impf' { $assignments += [System.Web.HttpUtility]::UrlEncode("27=IPI") }
                    'pf' { $assignments += [System.Web.HttpUtility]::UrlEncode("27=PI") }
                  }
                }
                if ($_.colloquial) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("40=TALK")
                }
                if ($_.formal) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("40=OFCL")
                }
                if ($_.dialect) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("43=DIAL")
                }
                if ($_.slang) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("40=JAR")
                }
                if ($_.obscure) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("51=OBSC")
                }
                if ($_.nonstandard) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("49=ERR")
                }
                if ($_.humorous) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("51=HUM")
                }
                if ($_.offensive) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("47=DIRT")
                }
                if ($_.derogatory) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("47=DER")
                }
                if ($_.euphemism) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("47=EUPH")
                }
                if ($_.familiar) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("47=FAM")
                }
                if ($_.obsolete) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("41=OBSL")
                }
                if ($_.polite) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("47=POS")
                }
                if ($_.childish) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("40=CHLD")
                }
                if ($_.military) {
                  $assignments += [System.Web.HttpUtility]::UrlEncode("42=MILI")
                }
                if ($assignments -and $assignments.Length -gt 0) {
                  $assignmentsTogether = $assignments -join ','
                  if ($assign -and $assign.Length -gt 0) {
                    $assign = "$assign,$assignmentsTogether"
                  } else {
                    $assign = $assignmentsTogether
                  }
                }
                if ($pruneListId -gt 0) {
                  $assign = "$assign&featureList2=$pruneListId&featurValue2=$pruneValue"
                }
                $assignDescription = ""
                if ($assign) {
                  $assign = "assign=$assign"
                  $assignDescription = "[ASSIGNING: $assign]"
                }
                $example = $article.example
                if ($example) {
                  $exampleTranslation = $article.example_translation
                  $example = "E.g. '$example'"
                  if ($exampleTranslation) {
                    $example = "$example ('$exampleTranslation')"
                  }
                }
                if ($referenceWords -and $word) {
                  "Importing $word -> $referenceWords $tag $assignDescription $example"
                  $response = Invoke-WebRequest -Uri "$global:productionHost/import?lexeme=$word&reference=$referenceWords&proper=0&hypernym=0&featureList=$posFeatureList&featureValue=$tisanePOSValue&source=wiktionary&orgId=$pageId&$assign&complement=$sComplement&note=$example" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
                }
              }
            }
          }
        }
      } catch {
        Write-Host "IMPORT FAILED FOR $word $_" -ForegroundColor Red
      }
    }
  }
} while ($bookmark)

"Tags encountered:" 
$tagsFound

"Number of articles: $articleCount"

# cleanup:
#"DELETE l FROM Lexemes l WHERE l.SourceType = 'wiktionary' AND l.Note LIKE '%Generated by matching: proper=0%' AND l.LastUpdatedBy = 'bulkimport'
# "DELETE f FROM Lexemes l INNER JOIN Features f ON f.ConnectionType = 1 AND f.EntityId = l.Id AND f.FeatureListId IN (1, 3, 4, 7, 8, 9, 13, 22, 23, 24, 26, 32, 33, 34) WHERE l.SourceType = 'wiktionary'"

# "DELETE f FROM Lexemes l INNER JOIN Features f ON f.ConnectionType = 1 AND f.EntityId = l.Id AND f.FeatureListId = 2 AND f.FeatureValue = '1' WHERE l.SourceType = 'wiktionary'"

# "DELETE f FROM Lexemes l INNER JOIN Features f ON f.ConnectionType = 1 AND f.EntityId = l.Id AND f.FeatureListId = 5 INNER JOIN LexemeFamilies lf ON lf.LexemeId = l.Id INNER JOIN Features pos ON pos.ConnectionType = 2 AND pos.EntityID = lf.FamilyID AND pos.FeatureListId = 1 AND pos.FeatureValue <> 'NOUN' WHERE l.SourceType = 'wiktionary'"

}
#EndRegion '.\Public\Import-LexemesFromWiktionary.ps1' 290
#Region '.\Public\Import-LinkedLexeme.ps1' 0
function Import-LinkedLexeme {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, HelpMessage="Target language code")][String] $language,
        [Parameter(Mandatory = $true, HelpMessage="Path: to translated csv fil")][string] $path
    )

    Login-Lamp
    $languageJSON = Invoke-RestMethod -Uri "$global:productionHost/languages" -Method GET -UseBasicParsing
    $languageId = 0
    $languageJSON | foreach {
    if ($_.ISOCode -eq $language) {
        $languageId = $_.id
        break
    }
    }
    Set-LampLanguage -languageId $languageId
    $csv = Import-Csv $path
    foreach($item in $csv)
    {
        $word=$item.translated & $englishLexemeId=$item.Id & $styleFeatures=$item.$styleFeatures
        $note = "Created by machine-translating '$englishLexeme'"
        $response = Invoke-WebRequest -Uri "$global:productionHost/import?lexeme=$word&sameFamiliesAs=$englishLexemeId&assign=$styleFeatures&source=mt&orgId=$englishLexemeId&note=$note" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
    }
}
#EndRegion '.\Public\Import-LinkedLexeme.ps1' 25
#Region '.\Public\Import-MonolingualLog.ps1' 0
function Import-MonolingualLog{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Filename")][String] $pathname,
     [Parameter(Mandatory = $true, HelpMessage="Language ID")][String] $languageId,
     [Parameter(Mandatory = $true, HelpMessage="Corpora ID")][String] $corpora
   )
   
   
$fileLines = Get-Content $pathname


# BEGIN fix for Powershell bug: in some cases, the configuration files aren't read properly
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
# END fix for Powershell bug: in some cases, the configuration files aren't read properly


## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Web;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


# Write-Progress -Activity "Authenticating" -Status "Normally takes a couple of seconds"
# $productionHost = 'https://lampws.tisane.ai:443'

# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$productionHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $authorizationToken = @{}
# $authorizationToken.Add('Authorization', $inJson.token)
# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

# $whatever = Invoke-WebRequest -Uri "$productionHost/setLanguage?language=$languageId" -Method POST -Headers $authorizationToken -Body ' ' -UseBasicParsing
Login-Lamp
Set-LampLanguage -languageId $languageId

$i = 0
$fileLines | ForEach-Object {
    $originalLine = $_
    $ln = [System.Web.HttpUtility]::UrlEncode($_)
    $pct = $i / $fileLines.length * 100
    $i += 1
    Write-Progress -Activity "Importing" -Status "$pct% $originalLine" -PercentComplete $pct

    $response = Invoke-WebRequest -Uri "$global:lampHost/testfragment?corpora=$corpora&fragment=$ln&test=true&ref=cht" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
}

"$i lines imported"

[gc]::Collect()
[gc]::WaitForPendingFinalizers()
}
#EndRegion '.\Public\Import-MonolingualLog.ps1' 72
#Region '.\Public\Import-WordNet.ps1' 0
function Import-WordNet{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Filename")][String] $inputXml,
     [Parameter(Mandatory = $true, HelpMessage="Language ID")][String] $languageId,
     [Parameter(Mandatory = $false, HelpMessage="Source")][String] $source
   )
   
   
[xml]$wnXml = Get-Content $inputXml


# BEGIN fix for Powershell bug: in some cases, the configuration files aren't read properly
# Add-Type -AssemblyName System.Configuration
# [Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
# [Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
# ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)
# [Configuration.ConfigurationManager]::ConnectionStrings[0].Name
# END fix for Powershell bug: in some cases, the configuration files aren't read properly


## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Web;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy


# Write-Progress -Activity "Authenticating" -Status "Normally takes a couple of seconds"
# $productionHost = 'https://lampws.tisane.ai:443'

# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$productionHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $authorizationToken = @{}
# $authorizationToken.Add('Authorization', $inJson.token)
# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding

# $whatever = Invoke-WebRequest -Uri "$productionHost/setLanguage?language=$languageId" -Method POST -Headers $authorizationToken -Body ' '
Login-Lamp
Set-LampLanguage -languageId $languageId

$wnXml.list.synset | ForEach-Object {
    $familyId = $_.family
    $sourceId = $_.id
    #$localization = Invoke-RestMethod -Uri "$productionHost/familyLocalization?id=$familyId" -Method GET -Headers $authorizationToken
    #$localization.definition = $_.definition
    #$localization.description = $_.word[0]
    #$localizationJson = ConvertTo-Json -InputObject $localization
    #"$familyId : $localizationJson"
    #Invoke-WebRequest -Uri "$productionHost/familyLocalization" -Method PUT -Headers $authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($localizationJson))
    $word = $_.native # $_.matchTokens[0].matchToken[1]
    $referenceWord = $_.english # [0]
    $_.word | ForEach-Object {
      $word = $_
      "Importing $word -> $familyId"
      $response = Invoke-WebRequest -Uri "$global:lampHost/importFamilies?lexeme=$word&families=$familyId&behavior=complement&source=$source&orgId=$sourceId" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes('')) -UseBasicParsing
    }
}

[gc]::Collect()
[gc]::WaitForPendingFinalizers()
}
#EndRegion '.\Public\Import-WordNet.ps1' 77
#Region '.\Public\New-AdjFamily.ps1' 0
## =============================================================================
##
## This script's purpose is to add a new noun family and new lexemes in English and another language, link them, and tag them
##
## =============================================================================
function New-AdjFamily{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(HelpMessage="Language code: ")][Int32] $lang,
     [Parameter(HelpMessage="Definition: ")][String] $definition,
     [Parameter(HelpMessage="Wikidata ID: ")][String] $wikidata,
     [Parameter(Mandatory = $true, HelpMessage="English lemmas, delimited by commas")][String[]] $english,
     [Parameter(HelpMessage="Native lemmas, delimited by commas ")][String[]] $native,
     [Parameter(Mandatory = $true, HelpMessage="Family ID ")][Int32] $family,
     [Parameter(Mandatory = $true, HelpMessage="Hypernym ID ")][Int32] $hypernym
   )
   
   



#$lang = 26 # code found in the language details

## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

class Feature {
  [string]$index
  [string]$value
  [string]$type
  
  Feature([string]$index,
    [string]$value,
    [string]$type) {
    $this.index = $index
    $this.value = $value
    $this.type = $type
  }
}


# $global:lampHost = 'https://lampws.tisane.ai:443'
# #$pPass = ConvertFrom-SecureString $password
# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)

# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Login-Lamp

$description = $english -join ', '

$grammar=@()
$grammar += @([Feature]::new("1", "ADJ", "Grammar"))
$grammar += @([Feature]::new("26", "ALL", "Grammar"))
if (-not $definition) {
  $definition = '???'
}

$newFamily = @{
  id=$family
  description=$description
  definition=$definition
  phraseType='not a phrase'
  grammar=$grammar
  wikidataId=$wikidata
}

Write-Host "Creating family...`r`n" -ForegroundColor Green 
$newFamily

$familyJson = ConvertTo-Json -InputObject $newFamily

$response = Invoke-WebRequest -Uri "$global:lampHost/family" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($familyJson)) -UseBasicParsing

$parsedResponse = ConvertFrom-Json -InputObject $response

if (-not $parsedResponse.success -or -not $parsedResponse.id) {
  Write-Host "Can't continue, no valid acknowledgement: $response" -ForegroundColor Red
  break # end the script
}

if ($family -ne $parsedResponse.id) {
  $actualFamilyId = $parsedResponse.id
  Write-Host "$family was occupied, inserted at $actualFamilyId" -ForegroundColor Yellow
}
$family = $parsedResponse.id

Write-Host "Linking hypernym $hypernym...`r`n" -ForegroundColor Green 
# link the hypernym
$whatever = Invoke-WebRequest -Uri "$global:lampHost/familyLinks?family=$family&type=hypernyms&links=$hypernym" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

Write-Host "Lexeme(s) in English...`r`n" -ForegroundColor Green 
# set the language to English
$whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=7" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

$english | Foreach-Object {
   Add-Lexeme $family $_
}

if ($native -and $native.length -gt 0 -and $lang -gt 0) {
  Write-Host "Lexeme(s) in language $lang...`r`n" -ForegroundColor Green 
  # set the native language
  $whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=$lang" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

  $native | Foreach-Object {
     Add-Lexeme $family $_
  }
}
}
#EndRegion '.\Public\New-AdjFamily.ps1' 128
#Region '.\Public\New-FormulaicFamily.ps1' 0
## =============================================================================
##
## This script's purpose is to add a new noun family and new lexemes in English and another language, link them, and tag them
##
## =============================================================================
function New-FormulaicFamily{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(HelpMessage="Language code: ")][Int32] $lang,
     [Parameter(HelpMessage="Definition: ")][String] $definition,
     [Parameter(Mandatory = $true, HelpMessage="English lemmas, delimited by commas")][String[]] $english,
     [Parameter(HelpMessage="Native lemmas, delimited by commas ")][String[]] $native,
     [Parameter(Mandatory = $true, HelpMessage="Family ID ")][Int32] $family,
     [Parameter(Mandatory = $false, HelpMessage="Hypernym ID ")][Int32] $hypernym
   )
   
#$lang = 26 # code found in the language details

# ## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

class Feature {
  [string]$index
  [string]$value
  [string]$type
  
  Feature([string]$index,
    [string]$value,
    [string]$type) {
    $this.index = $index
    $this.value = $value
    $this.type = $type
  }
}


# $global:lampHost = 'https://lampws.tisane.ai:443'
# #$pPass = ConvertFrom-SecureString $password
# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)

# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Login-Lamp
$description = $english -join ', '

$grammar=@()
$grammar += @([Feature]::new("1", "FORE", "Grammar"))
$grammar += @([Feature]::new("26", "ALL", "Grammar"))
if (-not $definition) {
  $definition = '???'
}

$newFamily = @{
  id=$family
  description=$description
  definition=$definition
  phraseType='mainClause'
  phraseTag='S'
  grammar=$grammar
}

Write-Host "Creating family...`r`n" -ForegroundColor Green 
$newFamily

$familyJson = ConvertTo-Json -InputObject $newFamily

$response = Invoke-WebRequest -Uri "$global:lampHost/family" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($familyJson)) -UseBasicParsing

$parsedResponse = ConvertFrom-Json -InputObject $response

if (-not $parsedResponse.success -or -not $parsedResponse.id) {
  Write-Host "Can't continue, no valid acknowledgement: $response" -ForegroundColor Red
  break # end the script
}

if ($family -ne $parsedResponse.id) {
  $actualFamilyId = $parsedResponse.id
  Write-Host "$family was occupied, inserted at $actualFamilyId" -ForegroundColor Yellow
}
$family = $parsedResponse.id

If ($hypernym -gt 0) {
  Write-Host "Linking hypernym $hypernym...`r`n" -ForegroundColor Green 
  # link the hypernym
  $whatever = Invoke-WebRequest -Uri "$global:lampHost/familyLinks?family=$family&type=hypernyms&links=$hypernym" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing
}

Write-Host "Lexeme(s) in English...`r`n" -ForegroundColor Green 
# set the language to English
$whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=7" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

$english | Foreach-Object {
   Add-Lexeme $family $_
}

if ($native -and $native.length -gt 0 -and $lang -gt 0) {
  Write-Host "Lexeme(s) in language $lang...`r`n" -ForegroundColor Green 
  # set the native language
  $whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=$lang" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

  $native | Foreach-Object {
     Add-Lexeme $family $_
  }
}
}
#EndRegion '.\Public\New-FormulaicFamily.ps1' 124
#Region '.\Public\New-NounFamily.ps1' 0
## =============================================================================
##
## This script's purpose is to add a new noun family and new lexemes in English and another language, link them, and tag them
##
## =============================================================================
function New-NounFamily{
[CmdletBinding()]
Param(
    # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
    # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(HelpMessage="Language code: ")][Int32] $lang,
     [Parameter(HelpMessage="Definition: ")][String] $definition,
     [Parameter(HelpMessage="Wikidata ID: ")][String] $wikidata,
     [Parameter(Mandatory = $true, ValueFromPipelineByPropertyName=$true, HelpMessage="English lemmas, delimited by commas")][String[]] $english,
     [Parameter(HelpMessage="Native lemmas, delimited by commas ")][String[]] $native,
     [Parameter(Mandatory = $true, HelpMessage="Family ID ")][Int32] $family,
     [Parameter(Mandatory = $true, HelpMessage="Hypernym ID ")][Int32] $hypernym,
     [Parameter(Mandatory = $false, HelpMessage="Hypernym ID ")][Int32] $domain,
     [Parameter()][Int32] $proper,
     [Parameter()][Int32] $person
   )
   
   



# . ".\normalizationLib.ps1"

$global:nativeLanguageIETF = ''

#$lang = 26 # code found in the language details

## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

class Feature {
  [string]$index
  [string]$value
  [string]$type
  
  Feature([string]$index,
    [string]$value,
    [string]$type) {
    $this.index = $index
    $this.value = $value
    $this.type = $type
  }
}


# $global:lampHost = 'https://lampws.tisane.ai:443'
# #$pPass = ConvertFrom-SecureString $password
# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)

# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Login-Lamp
$description = $english -join ', '
if ($description.Length -gt 95) {
  $description = $description.Substring(0, 95)
}

$grammar=@()
$grammar += @([Feature]::new("1", "NOUN", "Grammar"))
if ($proper -gt 0) {
  $grammar += @([Feature]::new("3", "NO", "Grammar"))
  $grammar += @([Feature]::new("14", "NA", "Grammar"))
} else {
  $grammar += @([Feature]::new("3", "YES", "Grammar"))
  $grammar += @([Feature]::new("14", "NPA", "Grammar"))
}
$grammar += @([Feature]::new("4", "REG", "Grammar"))
if ($person -gt 0) {
  $grammar += @([Feature]::new("22", "PERS", "Grammar"))
  $grammar += @([Feature]::new("23", "BODY", "Grammar"))
  $grammar += @([Feature]::new("24", "BODY", "Grammar"))
} else {
  $grammar += @([Feature]::new("22", "NO", "Grammar"))
  $grammar += @([Feature]::new("23", "THNG", "Grammar"))
  $grammar += @([Feature]::new("24", "THNG", "Grammar"))
}
$grammar += @([Feature]::new("26", "ALL", "Grammar"))
if (-not $definition) {
  $definition = '???'
}

$newFamily = @{
  id=$family
  properNoun=$proper
  description=$description
  definition=$definition
  phraseType='not a phrase'
  grammar=$grammar
  wikidataId=$wikidata
}

Write-Host "Creating family...`r`n" -ForegroundColor Green 
$newFamily

$familyJson = ConvertTo-Json -InputObject $newFamily

$response = Invoke-WebRequest -Uri "$global:lampHost/family" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($familyJson)) -UseBasicParsing

$parsedResponse = ConvertFrom-Json -InputObject $response

if (-not $parsedResponse.success -or -not $parsedResponse.id) {
  Write-Host "Can't continue, no valid acknowledgement: $response" -ForegroundColor Red
  break # end the script
}

if ($family -ne $parsedResponse.id) {
  $actualFamilyId = $parsedResponse.id
  Write-Host "$family was occupied, inserted at $actualFamilyId" -ForegroundColor Yellow
}
$family = $parsedResponse.id

Write-Host "Linking hypernym $hypernym...`r`n" -ForegroundColor Green 
# link the hypernym
$whatever = Invoke-WebRequest -Uri "$global:lampHost/familyLinks?family=$family&type=hypernyms&links=$hypernym" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

if ($domain -gt 0) {
  Write-Host "Linking domain $domain...`r`n" -ForegroundColor Green 
  # link the hypernym
  $whatever = Invoke-WebRequest -Uri "$global:lampHost/familyLinks?family=$family&type=domains&links=$domain" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing
}

Write-Host "Lexeme(s) in English...`r`n" -ForegroundColor Green 
# set the language to English
$activeLanguage = Invoke-RestMethod -Uri "$global:lampHost/setLanguage?language=7" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

$english | Foreach-Object {
   Add-Lexeme $family $_
}

if ($native -and $native.length -gt 0 -and $lang -gt 0) {
  Write-Host "Lexeme(s) in language $lang...`r`n" -ForegroundColor Green 
  # set the native language
  $activeLanguage = Invoke-RestMethod -Uri "$global:lampHost/setLanguage?language=$lang" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing
  $global:nativeLanguageIETF = $activeLanguage.ISOCode

  $native | Foreach-Object {
     Add-Lexeme $family $_
  }
}
}
#EndRegion '.\Public\New-NounFamily.ps1' 162
#Region '.\Public\New-VerbFamily.ps1' 0
## =============================================================================
##
## This script's purpose is to add a new noun family and new lexemes in English and another language, link them, and tag them
##
## =============================================================================
function New-VerbFamily{
[CmdletBinding()]
Param(
# [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
# [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(HelpMessage="Language code: ")][Int32] $lang,
     [Parameter(HelpMessage="Definition: ")][String] $definition,
     [Parameter(Mandatory = $true, HelpMessage="English lemmas, delimited by commas")][String[]] $english,
     [Parameter(HelpMessage="Native lemmas, delimited by commas ")][String[]] $native,
     [Parameter(Mandatory = $true, HelpMessage="Family ID ")][Int32] $family,
     [Parameter(Mandatory = $true, HelpMessage="Hypernym ID ")][Int32] $hypernym,
     [Parameter()][Int32] $person
   )
   
   



#$lang = 26 # code found in the language details

## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# If (-not ("TrustAllCertsPolicy" -as [type])) {
# Add-Type @"
# using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
# "@
# }
# [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

class Feature {
  [string]$index
  [string]$value
  [string]$type
  
  Feature([string]$index,
    [string]$value,
    [string]$type) {
    $this.index = $index
    $this.value = $value
    $this.type = $type
  }
}


# $global:lampHost = 'https://lampws.tisane.ai:443'
# #$pPass = ConvertFrom-SecureString $password
# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)

# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Login-Lamp
$description = $english -join ', '

$grammar=@()
$grammar += @([Feature]::new("1", "VERB", "Grammar"))
$grammar += @([Feature]::new("14", "TRAN", "Grammar"))
$grammar += @([Feature]::new("8", "REG", "Grammar"))
if ($person -gt 0) {
  $grammar += @([Feature]::new("23", "BODY", "Grammar"))
} else {
  $grammar += @([Feature]::new("23", "THNG", "Grammar"))
}
$grammar += @([Feature]::new("24", "ALL", "Grammar"))
$grammar += @([Feature]::new("26", "ALL", "Grammar"))
if (-not $definition) {
  $definition = '???'
}

$newFamily = @{
  id=$family
  description=$description
  definition=$definition
  phraseType='not a phrase'
  grammar=$grammar
}

# . ".\normalizationLib.ps1"


Write-Host "Creating family...`r`n" -ForegroundColor Green 
$newFamily

$familyJson = ConvertTo-Json -InputObject $newFamily

$response = Invoke-WebRequest -Uri "$global:lampHost/family" -Method POST -Headers $global:authorizationToken -Body ([System.Text.Encoding]::UTF8.GetBytes($familyJson)) -UseBasicParsing

$parsedResponse = ConvertFrom-Json -InputObject $response

if (-not $parsedResponse.success -or -not $parsedResponse.id) {
  Write-Host "Can't continue, no valid acknowledgement: $response" -ForegroundColor Red
  break # end the script
}

if ($family -ne $parsedResponse.id) {
  $actualFamilyId = $parsedResponse.id
  Write-Host "$family was occupied, inserted at $actualFamilyId" -ForegroundColor Yellow
}
$family = $parsedResponse.id

Write-Host "Linking hypernym $hypernym...`r`n" -ForegroundColor Green 
# link the hypernym
$whatever = Invoke-WebRequest -Uri "$global:lampHost/familyLinks?family=$family&type=hypernyms&links=$hypernym" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

Write-Host "Lexeme(s) in English...`r`n" -ForegroundColor Green 
# set the language to English
$whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=7" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

$english | Foreach-Object {
   Add-Lexeme $family $_
}

if ($native -and $native.length -gt 0 -and $lang -gt 0) {
  Write-Host "Lexeme(s) in language $lang...`r`n" -ForegroundColor Green 
  # set the native language
  $whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=$lang" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing

  $native | Foreach-Object {
     Add-Lexeme $family $_
  }
}
}
#EndRegion '.\Public\New-VerbFamily.ps1' 137
#Region '.\Public\Normalization-Lib.ps1' 0
Function GetNormalizedWikidataLemma([String] $language,[String] $entry) {
 if ($entry -like '* *') {
   return $entry
 } else {
   $suffixes = [ordered]@{}
   switch ($language) {
    'sq' {
        $suffixes.Add('i', '')
        $suffixes.Add('u', '')
        $suffixes.Add('(?<=[^aeëiouy])ulla', 'ull')
        $suffixes.Add('(?<=[^aeëiouy])ura', 'ur')
        $suffixes.Add('(?<=[^aeëiouy])eja', 'e')
        $suffixes.Add('(?<=[^aeëiouy])ëza', 'ëz')
        $suffixes.Add('(?<=[^aeëiouy])ia', 'i')
        $suffixes.Add('(?<=[^aeëiouy])ra', 'ër')
        $suffixes.Add('(?<=[^aeëiouy])rra', 'ërr')
        $suffixes.Add('(?<=[^aeëiouy])a', 'ë')
        $suffixes.Add('ca', 'cë')
        $suffixes.Add('da', 'dë')
        $suffixes.Add('dea', 'de')
        $suffixes.Add('fa', 'fë')
        $suffixes.Add('ga', 'gë')
        $suffixes.Add('dha', 'dhë')
        $suffixes.Add('sha', 'shë')
        $suffixes.Add('cia', 'ci')
        $suffixes.Add('media', 'medie')
        $suffixes.Add('pedia', 'pedi')
        $suffixes.Add('logia', 'logji')
        $suffixes.Add('ogjia', 'ogji')
        $suffixes.Add('ia', 'i')
        $suffixes.Add('aja', 'a')
        $suffixes.Add('dja', 'dje')
        $suffixes.Add('anija', 'anije')
        $suffixes.Add('fëmija', 'fëmijë')
        $suffixes.Add('bizelja', 'bizelja')
        $suffixes.Add('lulja', 'lule')
        $suffixes.Add('lja', 'lje')
        $suffixes.Add('mja', 'mje')
        $suffixes.Add('nja', 'një')
        $suffixes.Add('goja', 'gojë')
        $suffixes.Add('soja', 'sojë')
        $suffixes.Add('qja', 'qe')
        $suffixes.Add('tja', 'te')
        $suffixes.Add('ka', 'kë')
        $suffixes.Add('(?<=[^l])la', 'lë')
        $suffixes.Add('kumbulla', 'kumbull')
        $suffixes.Add('ma', 'më')
        $suffixes.Add('na', 'në')
        $suffixes.Add('ra', 'rë')
        $suffixes.Add('sa', 'së')
        $suffixes.Add('ta', 'të')
        $suffixes.Add('va', 'vë')
        $suffixes.Add('ëza', 'ëz')
        $suffixes.Add('za', 'zë')
      }
      'ru' {
        $suffixes.Add('ые', 'ое')
        $suffixes.Add('ы', '')
        $suffixes.Add('и', '')
        # '(\w+[оиые]е[ ])+\w+[иыяа]'
      }
      default {
        return $entry
      }
   }
   
   $suffixes.Keys | ForEach-Object {
      $fullRegex = $_ + '$'
      if ($entry -match $fullRegex) {
        $res = $entry -replace $fullRegex, $suffixes[$_]
        return $res
      }
   } | Select -First 1
    #if (-not($res)) {
    # return $entry
    #}
    return $entry
    
  }
}

Function GetVerbSameInflectionAs([int] $languageId,[String] $entry) {
  
  $entries = $entry.Split(" ")
  $lexemeId = 0
  $stem = $entry
  if ($entries -and $entries.Length -gt 1) {
    $firstWord = $entries[0]
    $sql = "USE tisane; SELECT TOP 1 l.Id, l.Stem FROM dbo.Lexemes l WHERE l.LanguageId = $languageId AND l.MainLemma = N'$firstWord' AND dbo.HasFamilyFeature(l.Id, 1, 'VERB') = 1"
    $ds = Invoke-Sqlcmd -Query $sql
    if ($ds) {
      $lexemeId = $ds.Id
      $headStem = $ds.Stem
      $stem = "$headStem " + $entry.Substring($firstWord.Length + 1)
    }
  }
  #känna som sin egen ficka
  return $lexemeId, $stem
}

Function GetSegmentSQL([String] $language,[String] $entry) {

 $res = ''
 $segmentCount = $entry.Split(" ").Length
 $penultimateIndex = $segmentCount - 1
 $regexes = [ordered]@{} # it's important to keep it ordered / unsorted, because the order matters
 switch -regex ($language) {
  'he' {
    $regexes.Add("\w+[ ]\w+[י]", "InflectingSegment = 1, AgreeingSegment1st = 2, AgreeingSegmentLast = $segmentCount")
    $regexes.Add("\w+ה([ ]\w+[תה])+", "InflectingSegment = 1, AgreeingSegment1st = 2, AgreeingSegmentLast = $segmentCount")
    $regexes.Add("\w+[ה][ ]\w+", "InflectingSegment = 1, AgreeingSegment1st = 2, AgreeingSegmentLast = $segmentCount")
    $regexes.Add("\w+ות([ ]\w+[תה])+", "InflectingSegment = 1, AgreeingSegment1st = 2, AgreeingSegmentLast = $segmentCount")
    $regexes.Add("\w+[ת][ ]\w+[^תה]", "InflectingSegment = NULL, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL")
   }
    'de' {
      $regexes.Add("[\p{Ll} ]+[ ](\p{Lu}\p{Ll}+[ ])+\p{Ll}+en", "InflectingSegment = $segmentCount, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL") # verb phrase
      $regexes.Add("\p{Ll}+en[ ][\p{Ll}+ ]*([ ]\p{Lu}\p{Ll}+)+", "InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL") # verb phrase
    }
    'sq' {
    }
    'ru' {
      $regexes.Add("(\w+ая[ ])+\w+[аья]", "InflectingSegment = $segmentCount, AgreeingSegment1st = 1, AgreeingSegmentLast = $penultimateIndex")
      $regexes.Add("(\w+[ое][е][ ])+\w+[ое]", "InflectingSegment = $segmentCount, AgreeingSegment1st = 1, AgreeingSegmentLast = $penultimateIndex")
      $regexes.Add("\w+([ ][\w\-]+(ых|го|а|ы|[ое]в))+", "InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL")
      $regexes.Add("\w+[ ]\w{1,3}([ ][\w\-]+)+", "InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL")
      $regexes.Add("(\w+[оиы][й][ ])+\w+", "InflectingSegment = $segmentCount, AgreeingSegment1st = 1, AgreeingSegmentLast = $penultimateIndex")
      $regexes.Add("\w+т[ьи][ ][\w\-]+","InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL")
      $regexes.Add("(\w+[оиые]е[ ])+\w+[иыяа]", "InflectingSegment = $segmentCount, AgreeingSegment1st = 1, AgreeingSegmentLast = $penultimateIndex")
      $regexes.Add("\w+([ ]+\w+[ой])?[ ]\w+[иаы]([ ][\w\-]+)*", "InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL")
    }
    'es|fr|it|pt' {
          $regexes.Add("\w+[ ]d[']\w+", 'InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL')
          $regexes.Add("\w+[ ]\w{2,4}[ ]\w+", 'InflectingSegment = 1, AgreeingSegment1st = NULL, AgreeingSegmentLast = NULL')
          $regexes.Add("\w+([ ]\w+)+", "InflectingSegment = 1, AgreeingSegment1st = 2, AgreeingSegmentLast = $segmentCount")

    }
 }

 $regexes.Keys | ForEach-Object {
    $fullRegex = '^' + $_ + '$'
    if ($entry -match $fullRegex) {
      $res = $regexes[$_]
      return $res
    }
 } | Select -First 1
}

Function GetSegmentValues([String] $lang,[String] $ent) {
  $sql = GetSegmentSQL -language $lang -entry $ent
  if ($sql) {
    $sql.Split(', ') | ForEach-Object {
       $seg = $_.Trim().Split('=')
       $currentMember = $seg[0].Trim()
       if ($currentMember -match '^[0-9]$' -or $currentMember -eq 'NULL') {
          return $currentMember
       }
    }
  }
  else {
    return $sql
  }
}
#EndRegion '.\Public\Normalization-Lib.ps1' 162
#Region '.\Public\Retag.ps1' 0
function Retag{
[CmdletBinding()]
Param(
     # [Parameter(Mandatory = $true, valueFromPipeline=$true, HelpMessage="LaMP user: ")][String] $user,
     # [Parameter(Mandatory = $true, HelpMessage="LaMP password: ")][String] $password,
     [Parameter(Mandatory = $true, HelpMessage="Language numeric ID: ")][int] $languageId,
     [Parameter(Mandatory = $true, HelpMessage="Range ID: ")][int] $rangeId,
     [Parameter(Mandatory = $false, HelpMessage="Reset list IDs: ")][String] $reset
)


# $global:lampHost = 'https://lampws.tisane.ai:443'
# $authenticationBody = '["' + $user + '", "' + $password + '"]'

# $productionAuthentication = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
# $inJson = ConvertFrom-Json -InputObject $productionAuthentication.Content
# $global:authorizationToken = @{}
# $global:authorizationToken.Add('Authorization', $inJson.token)

# $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
Login-Lamp

$languageStructure = Invoke-RestMethod -Uri "$global:lampHost/setLanguage?language=$languageId" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing
$languageEnglishName = $languageStructure.EnglishName
Write-Host "Retagging using range: $rangeId ($languageEnglishName), resetting listIDs: $reset" -ForegroundColor Green
Invoke-WebRequest -Uri "$global:lampHost/retag?arg=$rangeId&type=range&reset=$reset" -Method PUT -Headers $global:authorizationToken -Body ' ' -UseBasicParsing
}
#EndRegion '.\Public\Retag.ps1' 27