My goal is to test this code to make sure that Stanford Core NLP installed properly.
First I installed StanfordCOreNLP package using NuGet package manager and then I downloaded a zip file that contained a jar file that needed to be installed using jar -xf command , and then I ran the code.
At (var pipeline = new StanfordCoreNLP(props);)
I'm getting an error that says:
edu.stanford.nlp.io.RuntimeIOException: Error while loading a tagger model(probably missing model file)"
Inner Exception IOException:Unable to open"edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger" as class path, filename or URL
var jarRoot = #"D:/VisualStudioProjects/C#MachineLearningProjects/Chapter3TwiterSentiment/CoreNLPTest2/CoreNLPTest2/edu/stanford/nlp/models/pos-tagger";
var text = "We're going to test our CoreNLP instalation!!";
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
props.setProperty("ner.useSUTime", "0");
var curDir = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(jarRoot);
var pipeline = new StanfordCoreNLP(props);
Directory.SetCurrentDirectory(curDir);
var annotation = new Annotation(text);
pipeline.annotate(annotation);
using (var stream = new ByteArrayOutputStream())
{
pipeline.prettyPrint(annotation, new PrintWriter(stream));
Console.WriteLine(stream.toString());
stream.close();
}
Console.ReadKey();
Please follow the below steps:
Step 1: Download Core NLP
Step 2: Unzip d:\stanford-corenlp-full-2018-10-05
Step 3: Unzip d:\stanford-corenlp-full-2018-10-05\stanford-corenlp-3.9.2-models.jar
Step 4: Change var jarRoot = #"d:/stanford-corenlp-full-2018-10-05/stanford-corenlp-3.9.2-models";
Step 5: Change props.setProperty("ner.useSUTime", "0"); to props.setProperty("sutime.binders", "0")
Related
My new projects generated by VS 2019 cannot read or write files. Even the following simple code does not work.
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("hello");
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
var folder = Path.GetDirectoryName(path);
var filePath = Path.Combine(folder, "test.txt");
Console.WriteLine(filePath);
// File.WriteAllLines(filePath, new[] { "hello" });
using (var f = new FileStream(filePath, FileMode.OpenOrCreate))
{
f.Write(new byte[] { 0x39 }, 0, 1);
}
}
}
Accessing files using
.Net Core 3.0 + debugger
.Net Core 3.0 + generated exe
.Net Framework 4.6.2, 4.8 (have not tested others)
gives the exception
Unhandled exception. System.UnauthorizedAccessException: Access to the path 'C:\Users\user\Documents\ConsoleApp1\ConsoleApp1\Debug\netcoreapp3.1\test.txt' is denied.
However, it works in .Net Core 2.2 or below or using dotnet ConsoleApp1.dll.
My other few years old projects work. I have never encountered such a problem before. What is the problem and how to fix it?
It turns out my anti-virus blocked the file access. There were no pop-ups the first day so I could not know. Later, there were finally popups.
Is there a way to compile a WebAPI app from console app in .NET Core 3.1?
I have tried many different approaches, such as:
1.
var collection = ProjectCollection.GlobalProjectCollection;
var project = collection.LoadProject($#"{path}\Project.csproj");
project.SetProperty("Configuration", configuration);
project.Build();
(Path to .sln)
ERROR: MSB4041: The default XML namespace of the project must be the MSBuild XML namespace.
2.
new Microsoft.Build.Execution.ProjectInstance("PathToProject.sln").Build();
(Path to .sln)
ERROR: Microsoft.Build.Exceptions.InvalidProjectFileException: 'The project file could not be loaded. Data at the root level is invalid.
(Path to .csproj)
ERROR: Microsoft.Build.Exceptions.InvalidProjectFileException: 'The SDK 'Microsoft.NET.Sdk.Web' specified could not be found.
3.
ProjectCollection pc = new ProjectCollection();
Dictionary<string, string> GlobalProperty = new Dictionary<string, string>();
GlobalProperty.Add("Configuration", "Debug");
GlobalProperty.Add("Platform", "Any CPU");
GlobalProperty.Add("OutputPath", Directory.GetCurrentDirectory() + "\\MyOutput");
BuildParameters bp = new BuildParameters(pc);
BuildManager.DefaultBuildManager.BeginBuild(bp);
BuildRequestData BuildRequest = new BuildRequestData(projectFilePath, GlobalProperty, null, new string[] { "Build" }, null);
BuildSubmission BuildSubmission = BuildManager.DefaultBuildManager.PendBuildRequest(BuildRequest);
BuildSubmission.Execute();
BuildManager.DefaultBuildManager.EndBuild();
if (BuildSubmission.BuildResult.OverallResult == BuildResultCode.Failure)
{
throw new Exception();
}
(Both path to .sln and .csproj)
ERROR: Build result is a failure without reported exception
However, none of the approaches worked.
Therefore, I am wondering is it even possible to compile the .NET Core 3.1 WebAPI code?
The only way I could get this to work was to use the following code snippet (.NET 6):
var commandText = $"myProject.csproj";
p.StartInfo = new ProcessStartInfo(#"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\msbuild.exe", commandText);
p.StartInfo.WorkingDirectory = projectFileLocation;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
while (!p.StandardOutput.EndOfStream)
{
var line = p.StandardOutput.ReadLine();
sb.Append(line);
}
p.WaitForExit();
I hope this saves somebody a lot more time.
I am trying to extract a dll from a nuget package programatically and load the dll at runtime.
I want to avoid using any command line tools - I want my program to be completely self contained, and not rely on external executables.
I am trying to use the various nuget.client nuget packages listed at https://www.nuget.org/profiles/nuget, but there is no documentation for them whatsoever and I can't work out how.
I have the nupkg, and I am able to work out the location of the dll in the nupkg via a PackageReader, but I don't know how to extract the nupkg so that I can get the file out.
Edit
Thanks to the people who have pointed out that a nupkg is just a zip. I've now done the following:
var archive = new ZipArchive(downloadResourceResult.PackageStream);
var entry = archive.GetEntry(dllPath);
var assemblyLoadContext = new System.Runtime.Loader.AssemblyLoadContext(null, isCollectible: true);
var assembly = assemblyLoadContext.LoadFromStream(entry.Open());
However this throws a NotSupportedException with the following stack trace
System.IO.Compression.DeflateStream.get_Length() at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly, Stream assemblySymbols) at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly)
Here is a full method to download a nuget package and load it. It's just a POC - you'll want to configure it for your use case.
public async Task<Assembly> LoadFromNuget(string id, string version, string? nugetFeedUrl = null, CancellationToken cancellationToken = default)
{
var repository = Repository.Factory.GetCoreV3(nugetFeedUrl ?? "https://api.nuget.org/v3/index.json");
var downloadResource = await repository.GetResourceAsync<DownloadResource>();
if (!NuGetVersion.TryParse(version, out var nuGetVersion))
{
throw new Exception($"invalid version {version} for nuget package {id}");
}
using var downloadResourceResult = await downloadResource.GetDownloadResourceResultAsync(
new PackageIdentity(id, nuGetVersion),
new PackageDownloadContext(new SourceCacheContext()),
globalPackagesFolder: Path.GetTempDirectory(),
logger: _nugetLogger,
token: cancellationToken);
if (downloadResourceResult.Status != DownloadResourceResultStatus.Available)
{
throw new Exception($"Download of NuGet package failed. DownloadResult Status: {downloadResourceResult.Status}");
}
var reader = downloadResourceResult.PackageReader;
var archive = new ZipArchive(downloadResourceResult.PackageStream);
var lib = reader.GetLibItems().First()?.Items.First();
var entry = archive.GetEntry(lib);
using var decompressed = new MemoryStream();
entry.Open().CopyTo(decompressed);
var assemblyLoadContext = new System.Runtime.Loader.AssemblyLoadContext(null, isCollectible: true);
decompressed.Position = 0;
return assemblyLoadContext.LoadFromStream(decompressed);
}
You'll have to implement or use a version of the Nuget ILogger to download the nupkg.
You can rename the nuget extension to zip extension and should be able to extract to folder.
Now, you can get the dlls from the extracted folder.
In my project I have 2 files that I load using a C# script, however I am getting a System.IO.FileNotFoundException when I run the project from anywhere but the project root folder.
For example, if I'm in the project root on the terminal and I run dotnet run, the program will run fine but if I run it from the outside the project root with dotnet run -p (path to .csproj) I get the exception.
These are the pieces of code used for loading files:
_shader = new Shader("shader.vert", "shader.frag");
-
public Shader(string vertPath, string fragPath)
{
var shaderSource = LoadSource(vertPath);
...
}
-
private static string LoadSource(string path)
{
using (var sr = new StreamReader(path, Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
Sounds like it is searching for those files in the current directory.
You have two options:
Set the current directory when the program starts
Environment.CurrentDirectory =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Use absolute paths
var folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
_shader = new Shader(folder + "\shader.vert", folder + "\shader.frag");
I installed the three following packages into my console application:
Microsoft.Build
Microsoft.Build.Framework
Microsoft.Build.Tasks.Core
Microsoft.Build.Utilities.Core
And I tried to use the following method to build a project:
static void Build(string projectPath)
{
var logger = new ConsoleLogger(LoggerVerbosity.Normal);
logger.ShowSummary = true;
var manager = BuildManager.DefaultBuildManager;
var projectInstance = new ProjectInstance(projectPath);
var result = manager.Build(
new BuildParameters()
{
DetailedSummary = true,
Loggers = new List<ILogger>() { logger }
},
new BuildRequestData(projectInstance, new string[] { "Build" }));
var buildResult = result.ResultsByTarget["Build"];
var buildResultItems = buildResult.Items;
}
However, after I ran the code, I got the error that described in the following image:
Why is this happening and how can I fix it?
I think you're not using tht right MSBuild version. Try to set the variable explicitly in your .proj :
<MSBuildExtensionsPath>C:\Program Files (x86)\MSBuild</MSBuildExtensionsPath>
It seems the best solution is to use MSBuild command line in Process class. A working sample is as follows:
var buildOutput = new List<string>();
var buildError = new List<string>();
var buildProcess = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\MSBuild.exe",
Arguments = projectPath + " /t:Rebuild /p:Configuration=Debug",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
buildProcess.Start();
while (!buildProcess.StandardOutput.EndOfStream)
{
buildOutput.Add(buildProcess.StandardOutput.ReadLine());
}
while (!buildProcess.StandardError.EndOfStream)
{
buildError.Add(buildProcess.StandardError.ReadLine());
}
And then you could use the output to determine whether the build was successful or not. The important note is that you have to find the correct path of MSBuild.exe file as there are several versions of this file and in my case (VS 2017) the correct path is the one in the sample code.
One of BuildRequestData constructor overloads supports a parameter called "toolVersion". Since you are using Visual Studio 2017, set it as "15.0".
EDIT: I quitted using the .Net Framework provided MSBuild version (the one located here):
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
C:\Windows\Microsoft.NET\Framework\v4.0.30319
Instead, I'm using the one located here:
C:\Program Files (x86)\MSBuild\{version}\Bin
This version provide extra parameters as LangVersion or DeployOnBuild.