|
|
|
@ -17,7 +17,7 @@ public class ChartImage |
|
|
|
|
private static Dictionary< Tuple<Keys,Keys>, List<ChartImage> > mShortcuts = new Dictionary< Tuple<Keys,Keys>, List<ChartImage> >() ; |
|
|
|
|
|
|
|
|
|
private string mFullPath ; |
|
|
|
|
private dynamic mConfig ; |
|
|
|
|
private JsonConfig mJsonConfig ; |
|
|
|
|
private HashSet<string> mKeywords = new HashSet<string>() ; |
|
|
|
|
private Image mImage ; |
|
|
|
|
private ImageListViewItem mImageListViewItem ; |
|
|
|
@ -26,15 +26,13 @@ public class ChartImage |
|
|
|
|
{ |
|
|
|
|
// initialize the ChartImage |
|
|
|
|
mFullPath = fullPath ; |
|
|
|
|
mConfig = config ; |
|
|
|
|
if ( config == null ) |
|
|
|
|
mConfig = new JObject() ; |
|
|
|
|
mJsonConfig = new JsonConfig( config ) ; |
|
|
|
|
mImage = Image.FromFile( fullPath ) ; |
|
|
|
|
|
|
|
|
|
// parse the shortcut |
|
|
|
|
ILog logger = LogManager.GetLogger( "shortcuts" ) ; |
|
|
|
|
string val = mConfig[ "shortcut" ] ; |
|
|
|
|
if ( val != null ) { |
|
|
|
|
string val = mJsonConfig.getStringVal( new string[]{"shortcut"} ) ; |
|
|
|
|
if ( val != "" ) { |
|
|
|
|
Tuple<Keys,Keys> shortcut = parseShortcut( val ) ; |
|
|
|
|
if ( shortcut == null ) |
|
|
|
|
Program.logStartupMsg( "bad-shortcut", $"{key}: {val}" ) ; |
|
|
|
@ -47,8 +45,8 @@ public class ChartImage |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// prepare for search scoring |
|
|
|
|
if ( mConfig["keywords"] != null ) { |
|
|
|
|
foreach( string kywd in mConfig["keywords"] ) |
|
|
|
|
if ( config != null && config["keywords"] != null ) { |
|
|
|
|
foreach( string kywd in config["keywords"] ) |
|
|
|
|
mKeywords.Add( kywd.ToUpper() ) ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -59,11 +57,11 @@ public class ChartImage |
|
|
|
|
mImageListViewItem.Text = caption() ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public float getSearchScore( string searchQuery ) |
|
|
|
|
public double getSearchScore( string searchQuery ) |
|
|
|
|
{ |
|
|
|
|
// initialize |
|
|
|
|
searchQuery = searchQuery.ToUpper() ; |
|
|
|
|
List< Tuple<string,float> > scores = new List<Tuple<string,float>>() ; |
|
|
|
|
List< Tuple<string,double> > scores = new List<Tuple<string,double>>() ; |
|
|
|
|
ILog logger = LogManager.GetLogger( "search" ) ; |
|
|
|
|
if ( logger.IsDebugEnabled ) { |
|
|
|
|
if ( mKeywords.Count == 0 ) |
|
|
|
@ -78,27 +76,27 @@ public class ChartImage |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// initialize the search score weights |
|
|
|
|
float exactKeywordMatchScore = Program.appConfig.getFloatVal( new string[]{"search","exactKeywordMatchScore"}, 10f ) ; |
|
|
|
|
float leadingPartialKeywordMatchScore = Program.appConfig.getFloatVal( new string[]{"search","leadingPartialKeywordMatchScore"}, 2f ) ; |
|
|
|
|
float internalPartialKeywordMatchScore = Program.appConfig.getFloatVal( new string[]{"search","internalPartialKeywordMatchScore"}, 1.5f ) ; |
|
|
|
|
float exactCaptionMatchScore = Program.appConfig.getFloatVal( new string[]{"search","exactCaptionMatchScore"}, 5f ) ; |
|
|
|
|
float leadingPartialCaptionMatchScore = Program.appConfig.getFloatVal( new string[]{"search","leadingPartialCaptionMatchScore"}, 1f ) ; |
|
|
|
|
float internalPartialCaptionMatchScore = Program.appConfig.getFloatVal( new string[]{"search","internalPartialCaptionMatchScore"}, 0.5f ) ; |
|
|
|
|
double exactKeywordMatchScore = Program.appConfig.getDoubleVal( new string[]{"search","exactKeywordMatchScore"}, 10 ) ; |
|
|
|
|
double leadingPartialKeywordMatchScore = Program.appConfig.getDoubleVal( new string[]{"search","leadingPartialKeywordMatchScore"}, 2 ) ; |
|
|
|
|
double internalPartialKeywordMatchScore = Program.appConfig.getDoubleVal( new string[]{"search","internalPartialKeywordMatchScore"}, 1.5 ) ; |
|
|
|
|
double exactCaptionMatchScore = Program.appConfig.getDoubleVal( new string[]{"search","exactCaptionMatchScore"}, 5 ) ; |
|
|
|
|
double leadingPartialCaptionMatchScore = Program.appConfig.getDoubleVal( new string[]{"search","leadingPartialCaptionMatchScore"}, 1 ) ; |
|
|
|
|
double internalPartialCaptionMatchScore = Program.appConfig.getDoubleVal( new string[]{"search","internalPartialCaptionMatchScore"}, 0.5 ) ; |
|
|
|
|
|
|
|
|
|
// look for keyword matches |
|
|
|
|
foreach ( string kywd in mKeywords ) { |
|
|
|
|
if ( kywd == searchQuery ) { |
|
|
|
|
scores.Add( new Tuple<string,float>( "exactKeywordMatch", exactKeywordMatchScore ) ) ; |
|
|
|
|
scores.Add( new Tuple<string,double>( "exactKeywordMatch", exactKeywordMatchScore ) ) ; |
|
|
|
|
continue ; |
|
|
|
|
} |
|
|
|
|
int pos = kywd.IndexOf( searchQuery ) ; |
|
|
|
|
if ( pos == 0 ) { |
|
|
|
|
scores.Add( new Tuple<string,float>( |
|
|
|
|
scores.Add( new Tuple<string,double>( |
|
|
|
|
$"leadingPartialKeywordMatch[{kywd}]", |
|
|
|
|
Math.Min( leadingPartialKeywordMatchScore * searchQuery.Length, exactKeywordMatchScore ) |
|
|
|
|
) ) ; |
|
|
|
|
} else if ( pos > 0 ) { |
|
|
|
|
scores.Add( new Tuple<string,float>( |
|
|
|
|
scores.Add( new Tuple<string,double>( |
|
|
|
|
$"internalPartialKeywordMatch[{kywd}]", |
|
|
|
|
Math.Min( internalPartialKeywordMatchScore * searchQuery.Length, exactKeywordMatchScore ) |
|
|
|
|
) ) ; |
|
|
|
@ -108,16 +106,16 @@ public class ChartImage |
|
|
|
|
// look for caption matches |
|
|
|
|
string caption = this.caption().ToUpper() ; |
|
|
|
|
if ( searchQuery == caption ) |
|
|
|
|
scores.Add( new Tuple<string,float>( "exactCaptionMatch", exactCaptionMatchScore ) ) ; |
|
|
|
|
scores.Add( new Tuple<string,double>( "exactCaptionMatch", exactCaptionMatchScore ) ) ; |
|
|
|
|
else { |
|
|
|
|
int pos = caption.IndexOf( searchQuery ) ; |
|
|
|
|
if ( pos == 0 ) { |
|
|
|
|
scores.Add( new Tuple<string,float>( |
|
|
|
|
scores.Add( new Tuple<string,double>( |
|
|
|
|
$"leadingPartialCaptionMatch[{caption}]", |
|
|
|
|
Math.Min( leadingPartialCaptionMatchScore * searchQuery.Length, exactCaptionMatchScore ) |
|
|
|
|
) ) ; |
|
|
|
|
} else if ( pos > 0 ) { |
|
|
|
|
scores.Add( new Tuple<string,float>( |
|
|
|
|
scores.Add( new Tuple<string,double>( |
|
|
|
|
$"internalPartialCaptionMatch[{caption}]", |
|
|
|
|
Math.Min( internalPartialCaptionMatchScore * searchQuery.Length, exactCaptionMatchScore ) |
|
|
|
|
) ) ; |
|
|
|
@ -125,7 +123,7 @@ public class ChartImage |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// calculate the total score |
|
|
|
|
float totalScore = 0 ; |
|
|
|
|
double totalScore = 0 ; |
|
|
|
|
StringBuilder buf = logger.IsDebugEnabled ? new StringBuilder() : null ; |
|
|
|
|
for ( int i=0 ; i < scores.Count ; ++i ) { |
|
|
|
|
totalScore += scores[i].Item2 ; |
|
|
|
@ -190,12 +188,14 @@ public class ChartImage |
|
|
|
|
|
|
|
|
|
public string caption() |
|
|
|
|
{ |
|
|
|
|
string caption = (mConfig != null) ? mConfig["caption"] : null ; |
|
|
|
|
if ( caption == null ) |
|
|
|
|
caption = Path.GetFileNameWithoutExtension( mFullPath ) ; |
|
|
|
|
return caption ; |
|
|
|
|
// return the ChartImage's caption |
|
|
|
|
return jsonConfig.getStringVal( |
|
|
|
|
new string[]{ "caption" }, |
|
|
|
|
Path.GetFileNameWithoutExtension( mFullPath ) |
|
|
|
|
) ; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public JsonConfig jsonConfig { get { return mJsonConfig ; } } |
|
|
|
|
public Image image { get { return mImage ; } } |
|
|
|
|
public ImageListViewItem imageListViewItem { get { return mImageListViewItem ; } } |
|
|
|
|
} |
|
|
|
|