-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from Encamina/@vmunoz/add_html_and_doc_connec…
…tors Added two document connectors to SemanticKernel.Connectors.Document
- Loading branch information
Showing
6 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Connectors/DocDocumentConnector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Text; | ||
|
||
using CommunityToolkit.Diagnostics; | ||
|
||
using NPOI.HWPF; | ||
using NPOI.HWPF.Extractor; | ||
|
||
namespace Encamina.Enmarcha.SemanticKernel.Connectors.Document.Connectors; | ||
|
||
/// <summary> | ||
/// Extracts text from a document in the <c>.doc</c> format. | ||
/// </summary> | ||
public class DocDocumentConnector : IEnmarchaDocumentConnector | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DocDocumentConnector"/> class. | ||
/// </summary> | ||
public DocDocumentConnector() | ||
{ | ||
// Register the code pages encoding provider for the .doc files | ||
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IReadOnlyList<string> CompatibleFileFormats => [".DOC"]; | ||
|
||
/// <inheritdoc/> | ||
public virtual string ReadText(Stream stream) | ||
{ | ||
Guard.IsNotNull(stream); | ||
|
||
var document = new HWPFDocument(stream); | ||
|
||
var extractor = new WordExtractor(document); | ||
|
||
return extractor.Text.Trim(); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual void Initialize(Stream stream) | ||
{ | ||
// Intentionally not implemented to comply with the Liskov Substitution Principle... | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual void AppendText(Stream stream, string text) | ||
{ | ||
// Intentionally not implemented to comply with the Liskov Substitution Principle... | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Encamina.Enmarcha.SemanticKernel.Connectors.Document/Connectors/HtmlDocumentConnector.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using CommunityToolkit.Diagnostics; | ||
|
||
using HtmlAgilityPack; | ||
|
||
namespace Encamina.Enmarcha.SemanticKernel.Connectors.Document.Connectors; | ||
|
||
/// <summary> | ||
/// Extracts text from a document in the <c>.html</c> format. | ||
/// </summary> | ||
public class HtmlDocumentConnector : IEnmarchaDocumentConnector | ||
{ | ||
/// <inheritdoc/> | ||
public IReadOnlyList<string> CompatibleFileFormats => [".HTML"]; | ||
|
||
/// <inheritdoc/> | ||
public virtual string ReadText(Stream stream) | ||
{ | ||
Guard.IsNotNull(stream); | ||
|
||
var htmlDoc = new HtmlDocument(); | ||
htmlDoc.Load(stream); | ||
|
||
var text = htmlDoc.DocumentNode.InnerText.Trim(); | ||
|
||
// Remove all html tags from the text | ||
var cleanedText = HtmlEntity.DeEntitize(text); | ||
|
||
return cleanedText; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual void Initialize(Stream stream) | ||
{ | ||
// Intentionally not implemented to comply with the Liskov Substitution Principle... | ||
} | ||
|
||
/// <inheritdoc/> | ||
public virtual void AppendText(Stream stream, string text) | ||
{ | ||
// Intentionally not implemented to comply with the Liskov Substitution Principle... | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters