TraceLab Component Library
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties
GibbsLDAScript.cs
Go to the documentation of this file.
1 // TraceLab Component Library
2 // Copyright © 2012-2013 SEMERU
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 using RPlugin.Core;
18 using System;
19 using System.Collections.Generic;
20 using System.IO;
21 using System.Reflection;
22 using TraceLab.Components.DevelopmentKit.IO;
23 using TraceLab.Components.RPlugin.Properties;
24 using TraceLab.Components.Types.Tracers.InformationRetrieval;
25 using TraceLabSDK.Types;
26 
27 namespace TraceLab.Components.DevelopmentKit.Tracers.InformationRetrieval
28 {
32  public class GibbsLDAScript : RScript
33  {
34  private readonly string _baseScript = Settings.Default.Resources + "GibbsLDA.R";
35  private readonly string[] _requiredPackages = new string[] { "slam", "tm", "topicmodels" };
36 
37  private TLArtifactsCollection _source;
38  private TLArtifactsCollection _target;
39  private GibbsLDAConfig _config;
40  private string _outputFile;
41 
45  public override string BaseScript
46  {
47  get
48  {
49  return _baseScript;
50  }
51  }
52 
56  public override string[] RequiredPackages
57  {
58  get
59  {
60  return _requiredPackages;
61  }
62  }
63 
70  public GibbsLDAScript(TLArtifactsCollection source, TLArtifactsCollection target, GibbsLDAConfig config) : base()
71  {
72  _source = source;
73  _target = target;
74  _config = config;
75  }
76 
80  public override void PreCompute()
81  {
82  RUtil.RegisterScript(Assembly.GetExecutingAssembly(), _baseScript);
83  DirectoryInfo sourceInfo = SaveArtifactsToCache(_source, "GibbsLDA.source");
84  DirectoryInfo targetInfo = SaveArtifactsToCache(_target, "GibbsLDA.target");
85  _outputFile = RUtil.ReserveCacheFile("GibbsLDA.out");
86  _arguments = new List<object>();
87  _arguments.Add(sourceInfo.FullName);
88  _arguments.Add(targetInfo.FullName);
89  _arguments.Add(_outputFile);
90  _arguments.Add(_config.NumTopics);
91  _arguments.Add(_config.GibbsIterations);
92  _arguments.Add(_config.Alpha);
93  _arguments.Add(_config.Beta);
94  _arguments.Add(_config.Seed);
95  }
96 
102  public override object ImportResults(RScriptResult result)
103  {
104  return Similarities.Import(_outputFile);
105  }
106 
112  private DirectoryInfo SaveArtifactsToCache(TLArtifactsCollection artifacts, string name)
113  {
114  DirectoryInfo info = RUtil.CreateCacheDirectory(name);
115  foreach (TLArtifact artifact in artifacts.Values)
116  {
117  TextWriter tw = new StreamWriter(Path.Combine(info.FullName, artifact.Id));
118  tw.Write(artifact.Text);
119  tw.Flush();
120  tw.Close();
121  }
122  return info;
123  }
124  }
125 }