How to read from an Excel file? - x++

I have to read from a Excel file using X++ Code. Bellow is the some content as part of a large excel file. I would only need to filter list *_BillingCode, *_PSN, AccDistRuleAdvLedgerEntryExt etc on the basis of Public Sector in first column cells.
Public Sector SL1 *_BillingCode
Public Sector SL1 *_PSN
Public Sector SL1 AccDistRuleAdvLedgerEntryExt (Class)
Public Sector SL1 AccJourRuleAdvLedgerEntryExt
Public Sector SL1 AccountantActivities
Public Sector SL1 AccountingManagerActivities
Public Sector SL1 AdvancedLedgerEntry (Class)
Public Sector SL1 AdvLedgerEntry (Prefix)
Public Sector SL1 AxAdvancedLedgerEntry (Prefix)
Public Sector SL1 AxdAdvancedLedgerEntry
Public Sector SL1 AxdCustomerTransaction (Class)
Public Sector SL1 BudgetAccountDetail (Prefix)
I searched on google I found following lines of code to read from excel file.
static void ReadFromExcel(Args _args)
{
SysExcelApplication application;
SysExcelWorkbooks workbooks;
SysExcelWorkbook workbook;
SysExcelWorksheets worksheets;
SysExcelWorksheet worksheet;
SysExcelCells cells;
COMVariantType type;
int row;
CustAccount account;
CustName name;
#define.Filename('C:\\X++ Ownership.xls')
;
application = SysExcelApplication::construct();
workbooks = application.workbooks();
try
{
workbooks.open(#Filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
workbook = workbooks.item(1);
worksheets = workbook.worksheets();
worksheet = worksheets.itemFromNum(1);
cells = worksheet.cells();
do
{
row++;
account = cells.item(row, 1).value().bStr();
name = cells.item(row, 2).value().bStr();
info(strfmt('%1 - %2', account, name));
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
application.quit();
}
It is taking time to understand how could I utilize the above code, in order to meet my requirements. I would be thankful for your help.

There are better ways to read Excel files, see Axaptapedia (I am the author by the way).

Related

Text Similarity Percentage in Document Files using ML.Net

I want to group the 80% or above similar pdf documents using K Mean Algorithm and ML.Net.
I am reading the text from PDF files.
My requirement is whatever similarity percentage user enters, the document files should grouped according to that percentage only which means if user entered the 70% then document should be at least 70% similar.
Also how can i get the euclidean distance for each document from centroid?
I am new to ML.Net and Algorithm Please help and guide. Thanks
public class Prediction
{
[ColumnName("PredictedLabel")]
public uint Cluster { get; set; }
[ColumnName("Score")]
public float[] Distances { get; set; }
}
public void Train(IEnumerable<TextData> data, int numberOfClusters)
{
var mlContext = new MLContext();
var textDataView = mlContext.Data.LoadFromEnumerable(data);
var textEstimator = mlContext.Transforms.Text.NormalizeText("Text")
.Append(mlContext.Transforms.Text.TokenizeIntoWords("Text"))
.Append(mlContext.Transforms.Text.RemoveDefaultStopWords("Text"))
.Append(mlContext.Transforms.Conversion.MapValueToKey("Text"))
.Append(mlContext.Transforms.Text.ProduceNgrams("Text"))
.Append(mlContext.Transforms.NormalizeLpNorm("Text"))
.Append(mlContext.Transforms.NormalizeMinMax("Text"))
.Append(mlContext.Clustering.Trainers.KMeans("Text",
numberOfClusters: numberOfClusters));
var model = textEstimator.Fit(textDataView);
_predictionEngine = mlContext.Model.CreatePredictionEngine<TextData, Prediction>
_predictionEngine.Predict(new TextData() { Text = "19D000XXX Susan Porter 821442289
Information required"}).Cluster;
}

Why does F# compiler prefer to generate closed implementations of FSharpFunc types?

For this code:
module Module =
let func x y z = 0
[<EntryPoint>]
let main args =
func 1
func 1 1
0
Decompilation yields:
[CompilationMapping(SourceConstructFlags.Module)]
public static class Main
{
[CompilationMapping(SourceConstructFlags.Module)]
public static class Module
{
[Serializable]
internal sealed class main#30 : OptimizedClosures.FSharpFunc<object, object, int>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[CompilerGenerated]
[DebuggerNonUserCode]
public int x;
[CompilerGenerated]
[DebuggerNonUserCode]
internal main#30(int x)
{
this.x = x;
}
public override int Invoke(object y, object z)
{
return func(x, y, z);
}
}
[Serializable]
internal sealed class main#31-1 : FSharpFunc<object, int>
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[CompilerGenerated]
[DebuggerNonUserCode]
public int x;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[CompilerGenerated]
[DebuggerNonUserCode]
public int y;
[CompilerGenerated]
[DebuggerNonUserCode]
internal main#31-1(int x, int y)
{
this.x = x;
this.y = y;
}
public override int Invoke(object z)
{
return func(x, y, z);
}
}
[CompilationArgumentCounts(new int[]
{
1,
1,
1
})]
public static int func<a, b, c>(a x, b y, c z)
{
return 0;
}
[EntryPoint]
public static int main(string[] args)
{
int x = 1;
new main#30(x);
int x2 = 1;
int y = 1;
new main#31-1(x2, y);
return 0;
}
}
public static a Dump<a>(a arg00)
{
return arg00.Dump();
}
}
It generates a concrete type, that is generic parameters are provided at type definition. Why is not this done at the point of construction? I also noticed that types are generated in the module where call occurs, not where func is defined.
Having let func x y z = ... we need implementations of types to cover all possibilities:
FSharpFunc<T1,FSharpFunc<T2,T3,TReturn>>
FSharpFunc<T1,T2,FSharpFunc<T3,TReturn>>
FSharpFunc<T1,FSharpFunc<T2,FsharpFunc<T3,TReturn>>>
Compiler could generate all possible combinations in the same place, where function is defined, closing only for parameters with inferenced types.
You could argue that for the list of 7 args the set of types going to be quite large, but types like FSharpFunc<T1,T2,..,Tn, FSharpFunc<...>> are a mere optimazation. And FSharpFunc supports up to six generic types, then compiler has to switch to FSharpFun<T1,T2,T3,T4,T5,FSharp<...>>.
As pointed out by Fyodor it's not function creation that makes the compiler generating the hidden classes. The hidden classes are used to implement partial application.
In F# a partial application and lambdas are implemented as a compiler generated class that extends an abstract class. C# lambdas rely on delegates instead. IIRC Java and Scala use a similar technique to F# as JVM doesn't have delegates.
I suspect the F# compiler generates a class per partial application because it's simpler than collecting all partial applications and coalesce the identical ones.
It also helps the debuggability of F# programs as the name hints where the partial application was done: main#31-1 => In the main function at row 31. This name if included in logs or performance runs can help identifying what partial application is causing problems.
This comes at the cost of increasing the size of the F# assembly file as noted in a comment by Pavel.

Why is Neo4j(Client) running out of memory?

I am creating 1 million nodes. I had to create these in smaller batches to avoid running out of memory. Now I am trying to delete the existing nodes but again I am getting an out of memory exception. I should really be able to delete this level of data without running out of memory and without having to code around this limit. Am I doing something wrong here?
I know I could increase the Java heap size, but I feel that would just be deferring the real problem to a later point in time when I have much more data to create/delete.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Neo4jClient;
namespace Neo4JWontDelete
{
class Program
{
private const int MaximumNumberOfWordsToCreate = 1000*1000;
private const int BatchSize = 5 * 1000;
static void Main(string[] args)
{
var client = new GraphClient(new Uri("http://neo4j:Xxxxxx1#localhost:7474/db/data"));
client.Connect();
Console.WriteLine("Starting with a clean database");
DeleteAllObjects(client);
Console.WriteLine("Creating data");
int currentWordNumber = 1;
while (currentWordNumber < MaximumNumberOfWordsToCreate)
{
int numberOfWordsToCreate = MaximumNumberOfWordsToCreate - currentWordNumber;
if (numberOfWordsToCreate > BatchSize)
numberOfWordsToCreate = BatchSize;
var words = Enumerable.Range(currentWordNumber, BatchSize).Select(x => new Word {Value = x.ToString()});
client.Cypher
.Create("(w:Word {words})")
.WithParam("words", words)
.ExecuteWithoutResults();
currentWordNumber += numberOfWordsToCreate;
Console.WriteLine(currentWordNumber - 1);
}
Console.WriteLine("Deleting data");
DeleteAllObjects(client);
}
private static void DeleteAllObjects(GraphClient client)
{
client.Cypher
.Match("(w :Word)")
.Delete("w")
.ExecuteWithoutResults();
}
}
class Word
{
public string Value { get; set; }
}
}
This problem only seems to exist in the community edition. The 30 day trial of the enterprise edition works fine.
Do your words have relationships?
Otherwise you can batch it simply too.
MATCH (w:Word) with w limit 500000 delete w
With relationships you'd do
MATCH (w:Word) with w limit 50000 optional match (w)-[r]-() delete w,r

Retrieving item text with JNA and SendMessage()

I am attempting to retrieve the item text from a Win32 ListView-like control. I am using JNA and SendMessageW() to send LVM_GETITEMTEXTW to the control. I have been successful at retrieving the item count (via LVM_GETITEMCOUNT) but am stumped at this point. My User32 class is setup like so:
public interface MyUser32 extends User32 {
MyUser32 INSTANCE = (MyUser32)Native.loadLibrary("user32", MyUser32.class);
LRESULT SendMessageW(HWND hWnd, int msg, WPARAM wParam, LVITEM lParam);
}
My LVITEM class is setup like so:
public class LVITEM extends Structure{
public LVITEM() {
pszText = new Memory(MEMSIZE);
cchTextMax = MEMSIZE;
}
private static final int MEMSIZE = 256;
public UINT mask;
public int iItem;
public int iSubItem;
public UINT state;
public UINT stateMask;
public Pointer pszText;
public int cchTextMax;
public int iImage;
public LPARAM lParam;
public int iIndent;
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "mask", "iItem", "iSubItem", "state", "stateMask", "pszText", "cchTextMax", "iImage", "lParam", "iIndent"});
}
}
And the code that calls it all is like so:
MyUser32 u32 = MyUser32.INSTANCE;
LVITEM lvItem = new LVITEM();
WPARAM wPar = new WPARAM(1);
...
lvItem.iSubItem = 0;
res = u32.SendMessageW(handle, LVM_GETITEMTEXTW, wPar, lvItem);
System.out.println(res.intValue());
s = lvItem.pszText.getString(0);
System.out.println(s);
I've left out a bit of the code but I believe those are the important parts. My issue is that when I print out res.intValue() it is always 0 (meaning no text was returned) and when I print out the string value of pszText it is always some garbage characters. I'm completely stumped at this point so any suggestions are greatly appreciated. Thanks.

How to Convert a C++ Structure into C# Structure

I need to convert a complex C++ structure into C# Structure,I have converted other structures in C#, this one contains some Two Dimensional array that is what the problem how to change it,Here is my structure,
this is other structure, which I Converted properly,
C++:
typedef struct
{
BYTE sSerialNumber[DH_SERIALNO_LEN]; BYTE byAlarmInPortNum;
BYTE byAlarmOutPortNum;
BYTE byDiskNum;
BYTE byDVRType;
BYTE byChanNum;
} NET_DEVICEINFO, *LPNET_DEVICEINFO;
C#:
public struct NET_DEVICEINFO
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 48)]
public byte[] sSerialNumber;
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
}
And This structure Which I want Convert,this has 2 dim Array
C++:
typedef struct
{
DWORD dwSize;
DWORD dwDecProListNum;
char DecProName[DH_MAX_DECPRO_LIST_SIZE][DH_MAX_NAME_LEN];
DH_485_CFG stDecoder[DH_MAX_DECODER_NUM];
DWORD dw232FuncNameNum;
char s232FuncName[DH_MAX_232FUNCS][DH_MAX_NAME_LEN];
DH_RS232_CFG st232[DH_MAX_232_NUM];
} DHDEV_COMM_CFG;
and this is my try in C#,But it is giving me an error,
C#:
[StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Auto)]
public struct DHDEV_COMM_CFG
{
public uint dwSize;
public uint dwDecProListNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public string[] DecProName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
DH_485_CFG[] stDecoder;
public uint dw232FuncNameNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]
public string[] s232FuncName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public DH_RS232_CFG[] st232;
} ;
Please tell me how to to this....
By Bala
I know this is kinda useless 6 years down the road, but in any case, the converter from here worked nicely for me...

Resources