forked from apache/lucenenet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyzerProfile.cs
More file actions
104 lines (94 loc) · 4.54 KB
/
Copy pathAnalyzerProfile.cs
File metadata and controls
104 lines (94 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// lucene version compatibility level: 4.8.1
using Lucene.Net.Util;
using System;
using System.IO;
using System.Security;
namespace Lucene.Net.Analysis.Cn.Smart
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/// <summary>
/// Manages analysis data configuration for <see cref="SmartChineseAnalyzer"/>
/// <para/>
/// <see cref="SmartChineseAnalyzer"/> has a built-in dictionary and stopword list out-of-box.
/// <para/>
/// NOTE: To use an alternate dictionary than the built-in one, put the "bigramdict.dct" and
/// "coredict.dct" files in a subdirectory of your application named "smartcn-data". This subdirectory
/// can be placed in any directory up to and including the root directory (if the OS permission allows).
/// To place the files in an alternate location, set an environment variable named "smartcn.data.dir"
/// with the name of the directory the "bigramdict.dct" and "coredict.dct" files can be located within.
/// <para/>
/// The default "bigramdict.dct" and "coredict.dct" files can be found at:
/// <a href="https://issues.apache.org/jira/browse/LUCENE-1629">https://issues.apache.org/jira/browse/LUCENE-1629</a>.
/// <para/>
/// @lucene.experimental
/// </summary>
public static class AnalyzerProfile // LUCENENET specific: CA1052 Static holder types should be Static or NotInheritable
{
/// <summary>
/// Global indicating the configured analysis data directory
/// </summary>
// LUCENENET specific - changed from a mutable static field to a property.
public static string ANALYSIS_DATA_DIR { get; set; } = "";
static AnalyzerProfile()
{
Init();
}
// LUCENENET specific - changed the logic here to leave the
// ANALYSIS_DATA_DIR an empty string if it is not found. This
// allows us to skip loading files from disk if there are no files
// to load (and fixes LUCENE-1817 that prevents the on-disk files
// from ever being loaded).
private static void Init()
{
// LUCENENET: Support for GB2312 encoding. See: https://docs.microsoft.com/en-us/dotnet/api/system.text.codepagesencodingprovider?view=netcore-2.0
EncodingProviderInitializer.EnsureInitialized();
string dirName = "smartcn-data";
//string propName = "analysis.properties";
// Try the system property:-Danalysis.data.dir=/path/to/analysis-data
//ANALYSIS_DATA_DIR = System.getProperty("analysis.data.dir", "");
// LUCENENET specific - reformatted with :, renamed from "analysis.data.dir"
ANALYSIS_DATA_DIR = SystemProperties.GetProperty("smartcn:data:dir", "");
if (ANALYSIS_DATA_DIR.Length != 0)
return;
string currentPath = AppDomain.CurrentDomain.BaseDirectory;
string candidatePath = System.IO.Path.Combine(currentPath, dirName);
if (Directory.Exists(candidatePath))
{
ANALYSIS_DATA_DIR = candidatePath;
return;
}
try
{
while (Directory.GetParent(currentPath) is { } parent) // LUCENENET: Reduce DirectoryInfo allocations by only getting parent once per iteration (#832)
{
candidatePath = System.IO.Path.Combine(parent.FullName, dirName);
if (Directory.Exists(candidatePath))
{
ANALYSIS_DATA_DIR = candidatePath;
return;
}
currentPath = parent.FullName;
}
}
catch (SecurityException)
{
// ignore security errors
}
}
}
}