Adding two columns using Deedle in C# - c#

Given the following CSV file
A,B
2,3
5,7
9,11
I'd like to add the two columns, resulting in
A,B,C
2,3,5
5,7,12
9,11,20
using C# and Deedle.
using Deedle;
using System.IO;
using System.Linq;
namespace NS
{
class AddTwoColumns
{
static void main(string[] args)
{
var root = "path/to";
var df = Frame.ReadCsv(Path.Combine(root, "data.csv"));
var a = df.GetColumn<int>("A");
var b = df.GetColumn<int>("B");
var c = df.Select(x => x.a + x.b);
df.AddColumn("C", c);
df.Print();
}
}
}
Neither the
reference
nor the tutorial
(series,
frame)
is particularly illuminating.
What is the correct df.Select() for this simple operation?

a and b are just Deedle.Series which you can perform numerical operations on. So, you can do this just by adding both series:
// simply add the series
var c = a + b;
df.AddColumn("C", c);
df.Print();
// output
A B C
0 -> 2 3 5
1 -> 5 7 12
2 -> 9 11 20
The Statistics and calculations section (of the page you linked to) briefly mentions arithmetic operations. It also features a note on missing data which you might need to consider:
Point-wise and scalar operators automatically propagate missing data.
When calculating s1 + s2 and one of the series does not contain data
for a key k, then the resulting series will not contain data for k.

I know this question is particularly addressed for C#, but I hope this F# approach can help somehow:
Frame.ReadCsv(#"C:\Users\flavi\Downloads\sample.txt")
|> fun frame->
Frame.addCol "C"
(Frame.mapRowValues (fun row ->
row.GetAs<int>("A") + row.GetAs<int>("B")
)frame) frame

Related

How to do a row based process using Deedle (Frame in and Frame out)

I am trying to use Deedle to do row based process on a DataFrame. But i just can't get my mind tuned into the Deedle way.
Say for a Frame like
Indicator1 Indicator2
1 100 200
2 300 500
3 -200 1000
Say there are some rules needs to be applied to each indicator:
if Indicator value is less than 500 and larger than 0, multiply it by 1.1
if Indicator value is less than 0, make it NaN
I have been trying to use Frame.mapRow .... functions.
I know I can use the
fun v -> let indVal = v.GetAs<Int>("Indicator1");
let newIndVal = match indVal with
|...... logic
|...... some other logic
let indVal2 = v.GetAs<Int>("Indicator2");
let newIndVal2 = match indVal2 with
|...... logic
|...... some other logic
with the Frame.mapRow ....
But I am stuck at how to make the newIndVal and newIndVal2 back into the a row and eventually back into a new data frame.
What I am trying to achieve is a frame in and frame out. Also I only know to process the column one by one (after retrieving them by index or name). If the logic to be applied are generic, is there a way NOT to apply the logic one column by one column?
A imperative (and really simple) way to do this with C or C# 2d array is
loop through the row dimension
loop through the column dimension
apply the rule as the side effect to the array[row,col]
How to achieve this in Deedle?
UPDATE:
Leaf Garland's suggestion works great if the calculation doesn't need to reference other columns from the same row. For my case, I need to look at the data row by row, hence I would like to use Frame.mapRows. I should have been clear on the simplified requirements:
Say for a Frame like
Indicator1 Indicator2
1 100 200
2 <Missing> 500
3 -200 1000
4 100 <Missing>
5 <Missing> 500
6 -200 100
For example
if indicator1 is less than 300, new Indicator2 value is Indicator2 + 5% * Indicator1
I need to use
mapRows fun k v -> let var1 = v.get("Indicator1")
let var2 = v.get("Indicator2")
run through the conditions and produce new var1 and var2
produce a objectSeries
|> Frame.ofRows
The pesudo code above sounds simple but i just can figure out how to reproduce a proper objectSeries to recreate the Frame.
I also noticed something i can't explain with mapRows function [SO question]: Deedle Frame.mapRows how to properly use it and how to construct objectseries properly
Update
Since the original question was posted, I have since used Deedle in C#. To my surprise the row based calculation is very easy in C# and the way C# Frame.rows function handle missing values are very different than F# mapRows function. The following is a very basic example i used to try and true the logic. it might be useful to anyone who is searching for similar application:
Things to pay attention to are:
1. The rows function didn't remove the row while both columns' value are missing
2. The mean function is smart enough to calculate mean based on available data point.
using System.Text;
using System.Threading.Tasks;
using Deedle;
namespace TestDeedleRowProcessWithMissingValues
{
class Program
{
static void Main(string[] args)
{
var s1 = new SeriesBuilder<DateTime, double>(){
{DateTime.Today.Date.AddDays(-5),10.0},
{DateTime.Today.Date.AddDays(-4),9.0},
{DateTime.Today.Date.AddDays(-3),8.0},
{DateTime.Today.Date.AddDays(-2),double.NaN},
{DateTime.Today.Date.AddDays(-1),6.0},
{DateTime.Today.Date.AddDays(-0),5.0}
}.Series;
var s2 = new SeriesBuilder<DateTime, double>(){
{DateTime.Today.Date.AddDays(-5),10.0},
{DateTime.Today.Date.AddDays(-4),double.NaN},
{DateTime.Today.Date.AddDays(-3),8.0},
{DateTime.Today.Date.AddDays(-2),double.NaN},
{DateTime.Today.Date.AddDays(-1),6.0}
}.Series;
var f = Frame.FromColumns(new KeyValuePair<string, Series<DateTime, double>>[] {
KeyValue.Create("s1",s1),
KeyValue.Create("s2",s2)
});
s1.Print();
f.Print();
f.Rows.Select(kvp => kvp.Value).Print();
// 29/05/2015 12:00:00 AM -> series [ s1 => 10; s2 => 10]
// 30/05/2015 12:00:00 AM -> series [ s1 => 9; s2 => <missing>]
// 31/05/2015 12:00:00 AM -> series [ s1 => 8; s2 => 8]
// 1/06/2015 12:00:00 AM -> series [ s1 => <missing>; s2 => <missing>]
// 2/06/2015 12:00:00 AM -> series [ s1 => 6; s2 => 6]
// 3/06/2015 12:00:00 AM -> series [ s1 => 5; s2 => <missing>]
f.Rows.Select(kvp => kvp.Value.As<double>().Mean()).Print();
// 29/05/2015 12:00:00 AM -> 10
// 30/05/2015 12:00:00 AM -> 9
// 31/05/2015 12:00:00 AM -> 8
// 1/06/2015 12:00:00 AM -> <missing>
// 2/06/2015 12:00:00 AM -> 6
// 3/06/2015 12:00:00 AM -> 5
//Console.ReadLine();
}
}
}
You could map over all values in your frame using Frame.mapValues. Provide it a function that takes your data type and returns the updated value.
let indicator1 = [100.0;300.0;-200.0] |> Series.ofValues
let indicator2 = [200.0;500.0;1000.0] |> Series.ofValues
let frame = Frame.ofColumns ["indicator1" => indicator1; "indicator2" => indicator2]
// val frame : Frame<int,string> =
//
// indicator1 indicator2
// 0 -> 100 200
// 1 -> 300 500
// 2 -> -200 1000
let update v =
match v with
|v when v<500.0 && v>0.0 -> v * 1.1
|v when v<0.0 -> nan
|v -> v
let newFrame = frame |> Frame.mapValues update
// val newFrame : Frame<int,string> =
//
// indicator1 indicator2
// 0 -> 110 220
// 1 -> 330 500
// 2 -> <missing> 1000

New mechanism of ordering

I am trying to invent bycicle which should be already invented :)
so, I have sorted dictionary:
1:val1,
2:val3,
3:val3
now, I need insert new item between 1 - 2 key without moving the key(2 and 3). There is one way- insert key with 1.5 value but it is under question how many item can I insert between 1-2 and which algorithm is suitable to do so.
It is possible to insert N items between them......
the Data type or the format of the key dos not metter, just it crucial that key dos not change
You can use alphabet as keys. New key will be concatenated string of previous and next key.
For example:
If you have A and B, new key will be AB:
A
AB (A + B)
B
to add new key between A and AB, you will have AAB:
A
AAB (A + AB)
AB (A +B)
B
and so on...
You can use BigDecimal in Java to get new_key=(key1+key2)/2.
import java.math.BigDecimal;
public class CodeSheet {
public static void main(String[] args) {
BigDecimal a = new BigDecimal(1);
BigDecimal two = new BigDecimal(2);
for(int i = 0; i <= 6000;i ++)
{
a = a.divide(two);
if (i%1000 == 0)
System.out.println(a.toString());
}
}
}
output:
0.5
4.666318092516094394950447723619085848085457231858540123108571698979834554878878172272201635489405511797974949651621213121077437606770161974207604086019653781172053330691625751369975379929509157555502453981325565591202562573979668954025891355627075519053491894272132405597349071143304796110088314552213992280847244435737332640031641842263237146309149310826013965976447468035589253318343705327199027653590681602999224130209770506066148149347510972573049521073043341806223964760174134323088289634580237100329681945208689479110591825390227783142221369626937585639273983907781732018574388408834499276960346327197120043559868373508749313133453736483812679019646881169169905234639372793026268482208251953125E-302
4.354904908108608337788097747389436147929551871352694308321746614749144426703133706892369375399893940532782043588727560318102151185994168162541395412261518430550755321155148659223854561694954711924029281870986173595315518548585511706690334120707350364118146386757419736828045574038868295421914635929079059744555673086284578352082725382878484893441482333124196570054633787834328204149323035102391048682919453726835836055542393496585133335537308533180937973567503195969258360476033334393391821525284565813777886412721596101172364149931162673561491299813436308231347105575294992080347939004544602896748346183684569136737241491985864479016388335577898767470263560575437108148985779824881400524519876725232838928477267134127170926478227142121467310560266975687300142692457732481335134494432171071782408627253792130227205541778953790826318813538185488987468045762324286746160892737356344542977960827017103483581102720485435212671525873919071359975592227058010595009966338494815791830176096729490346274067433317969857326763618026109468018115811724815116133999445116665877671509729075069315638648723850028991409064960833766332205861977229046767218582849546541487471215035845260620260257995532242830003020146307439405502318054114708229860672592763223579453418947532879305088491927160146050190013971612697710103760111069333206902362883772710769598910416379683242622746115746412698399581131525337696075439453125E-603
4.064274312778867720235939028734255660766324543474839164530422188377250904692462466209591864260587971411331478774338224911275926886702626758550412028002712137706367368146396620253064136127073264858942335915257829998865098421249280979778656504235680311966272657056163696546167149941924660404935695895945028011534537971659758008693505558573200513078683510452651595620409830786639557494530833737093935568272875104139425826158032062394577918465219998245130052918160097042806762070602493653493626216406119810227553087853066722753388827490288177690729623901043570687373981862722140724176570320050741221327417827648494038121701089062505713056402546071605393776743633905210424237918734689348418886901559587016827788694266756655317144124682859190661867093972217932288628949680878455501011902943704213579623344964181809419242960540066091034125167128849103858978675507408455806146209191851472240678382070178087429856622047212175931655973014000019630325498473096560571602518169186419530087543188764835897219142880682252597989279812191578193716163187575327957591625965057446212814094560426730589262600928737872525933767331411105248595121827696798608491093243582843484129095100597421642936657760266153001856285613174837664729189258437310502932083077322244297462818854139350309998096123305359911095115904480646457999254743328163360465724236166676478809329603593817106724193744306683490394814672540103839616421520608733694040189710678874823730575477314252903579788549424755469905133470201451695223803806440596745401726146987923905610169014059776971596451074394178160713636961030621285663930888688208102523067395389210836279358605667255768920628104803766115059613394743085226143092748238730794960406636454230915469276113065925011064938129865019339567665464818230330165724148468941280304362503380341826473700203394927473837564110162168494638664204304239237821253725128209855798555318373561952586104070384276660875768023522310029611970728612015242875836349942659673401524737492334455205215164285934911013390410191470865217345765170604606902120008883954298607285769450192614645096800263246450413134880363941192626953125E-904
3.793039351733689286115602651843418575407592505563559767838544978475768217731986254376334108338556729108108742576832030998209717600316405171714055148655625096531883980134466070609936781559539370211169480252948213114277039837881063371677982969942447634651142331829192990506994554605009729046508341063337270064533739907321744792853183498549053351652988350920074096638285429862308391372954932006828359193622721419231928621489711340365241716248314514812885520219410680477334527856708284138839427973457868801553311949616569124359090488609460035208780642970555722141753015176081595007867635360093435776178190543350036841708399408539367940510076413956467316430916095391711744186998859008657799555644735253422641584786163850401369234332768914588781386187196795711351724957749391652361622962074629197788024298287754370852707402508636707853904761054362146259460871849920284565328911164746852351234728953672011980064743223911894197685446274274878390523329351901158826070800120347703561421615880371006639670348315766341149012537464083758587758525293663683636298511446099289911714323531235991277865531867843982015026878878896639255995472483117790417639250799634307655748030779632564691596760909404908081531372615164547043388535489528971883752026521919961407668947083407213423974005267001744595617615696630765504428689036809351860875424035621037482616342322215337934367594548330261003594070152341849674175878158149298226389343930852958679143549606816591025602305342014955716217132845940342769543266933325261087375477444832336677288699401172623142002746821390322113408026398127154228190552042800992261198805194376694845540628905243504033734992985876301276630991930510298121466339193065893457487944766698446689827603944852943168637004615202567494167670399243323951589706415785266079343514108658745562117732680244677936376530687808055754082068450365935259713154646823097603333358068171497216860297611437752243167697698110498170845456846020739750846543505713643988984041854908770199868636242151130688332117043118748773610315628039566704014306672360451008845657691837507078328158864945109567892609789595433570269036167524293587898139314884455743023719471414397201222765928299092144157389940419386413023206471168462291245388659261327754572315524233871897164259808663363315096885766828683298944863773587092451238950045819754126102259768472365375016760732714067551948222283479589066389122654198832035706196138937569618393555906093460831093138264657632808502714703693332035925939820567323717250446576767156644591421419293812145057111017406654972022884869525035195784771466087933420259995884070837958397749799705740049396202644025175073210593295755945620102108055480516934702115224195574139195098066319855672558679043638893669794670155410997428664928664168167953985116161210250297804169594684253752348013222217559814453125E-1205
3.539905630524086446192807579347028776473774255169715679364915111773183629594894261784683085891047097454394867262819826695919103869940157295563047917870123862073200485978698667987466291462969557667523695647811341020357717788109785770835114680897009767915137494440566026455659843912354456504192989188835705602179322924963912494888485940786809362154139116461482007358821801406237669112276567068833410302769662102872588881507070273133853988209179248987257143758222330905918518632062135785476152084443626925203518301132960766835299892043190742792567520558478108203462259662371962959310432074095927058133868959474972030842142538796230184620128885276788174481960845142745183370519589551950615610131186590563480089317314601509944118002032875732033201100247110459481847952990857037334823640679523424278765589162449234968023799573915320394430700449823949488280250506135330963124231931659920285823325225449622471007698126353485005939406839811953394571738969314132530527602613221562230127655722757290505931769316848401723278194683296217306185235865569986617096202061546172423665496763554374618060106337325218559850235824014071278034559473690204207288687313849631758829607486091753593924248760771142255825210360672986637626679989741155885434700486067230410700299575535149711341731609432984480384687544635995062804471136149305854175315966377810108275507681181651961962801421349165404250346126820728720208873595893440536648422148718112505064284071060898109782417605476476280361232169023368205490847212108780363322062863780340391237551435459420246337892681095889721559462791278479989710590315029810407934507056226372627879032580698604457266468300386458407724348590681589801941083795722493502969765170120695678675515620032539600652057815256453699379920243695102441630342646026753909888234075230277217434906456997302645472831528972628145734383468385440223694543434021735982488952306335977309013169460467883641860640785227636310262987940964022984673469826568517571050125270818176149707331140910524347851732575390841829346909410549839727953705987962211317256181818751290463639370119897704800571364206619105262584707180671238507585849999073849197663824198883130260095040819592313094603646217985433953185704099370284536904773266787536901400506007032774257894058018064436730924099462051548954038384791968747068019217638045788829854591320600007159149841443555646570855081618024360520958578760360280384849375071676855716557180607139713205883919099032478943967247930537312162289144679104372615090011632866858961375079019575267407833271784593151520703411904218370807609740113466986453924875951953906379335261873299732057632583548583707287549651159936981092460393588420646863927581217105974812665362938650418397789045364604339496313477110578729246893510209369181042422940691767021001414072916609146366788913996755700759640665234784333407084341110051886643788328930564570244055170922341783514820106649871716205106654635196708758096609371010578371733787205786872007626789590831722266502098400838009325576368679530618235118703501791946624067543742495370252936490448215272265394446201505681555959785096101950998232539156908671549704321558751663495139442633882009657071823899139741961998266368147671510768218115426296064382005662529050949281621806147707102909461952974903326945256007076524833299463667109290119355903366196219833733413695411303379451823231753263731780865552172559852473440089366781507217094187589639337530070328546865963879643466871402819511600155281016186295822285089803134472408174815427628345787525177001953125E-1506
3.303665137902827496041698814598251859956102752695243457545238134313877538516687455245209321720638823230490006282381480310772444510034045211226314330774840068284288371196446260296884583517814706659635628162189026783047191826750267043225489400771903093944259276619029927475346438031545811605946374504442097006994262477824439348857706776768617486310581249836618919417304749430900959706561113042262150517874802265433077999641328682509847882884924517121017190187846542512359062550646298184979756438755292545173270616579147035775541385671706654033870782649215204956912887364382156195690188215651083844934438738698733214152401938270023834336874746163318149027984994858285136502541795338316312101997589022837431637236440456040998512077161206236019051712081611565644859268804662919202586363218603552752695270832099692390467113884066534323925557128328604171993970853722160012919159551842690982027464367161257179808021863759049562797978645493867537105954179930724913679481936875402812572711930723882070284861941929119558170827897315859162842037326796133850257021269068336533236923850739658611999378702648242711666129597638095361076728543262011326217083677875215772308541136175153691149435654095931864251712782144524369655589716904881617799361462290466021866259726962247117462012115278442296220542386536419650551473602369400134797477831217636135509200328610036257616743356234677813111634192132502323701745358218078779524266341964037873127937848830826508508164096259725938628931626670796735885682591634721884490231165098692815205140693977889952801242893472229120340032093869453762832972291859794557911577639522395307940732945712107075755019740490962937164728167213432768492138733750524266274699077375733962811982278517502963812611476393952844259367082860095303675299020089115276657994155137119249817329420480064676642884472827028871906976341796718053546757401432467583130922019249330985492860548855596939221967584883875415697012658344666072424828836624264487286028591464953422057358292453173183196474756535257290117885536909618270459398220468815577926370607214507553414003598631496358187812992866734203795256335459900517763162601851984742584705374313070085033751129575420554964387441175846286562452089320606255797752016105950464227482065705408603260648257826551443686687882577303155206625947223484819915238490514881825717999121457137657714149353624922334220725600291951263162279298695401753356778309665095213843636885555987498807740209599646730604620908084012544399944192020917084890415757520265442239383238645767041482207162970579811093177791400644934010615516814079728309548220856096973675927990639940709613458493245984563211702111448359648433511680438377616298772795422516698822911002375856354580413147635506696458049588867868603320909589298339689100758949758682637103963988827307318959090420171350280219793288216670756666823872989199717083589126986974586632039928270417894835660021707866194543009725663505202689205234311075475898182424450167657545369789105339884283826964391562437254380134403620648901996109597800217991087232739433333005261148105778006362388323395808747788471187188991174879178895896934383118524229645808772348052329685868454757816134812834066970450014033639076242933255140291275675316837272240534404493876145292713293434547248272013302450195929488069705299508393401903558199250780015449549549844411621136134455877441994562247110233939116211180769579609101363083680816625026801067799314561701358285026548146460110929528534340290355738799466322677694228254707089730120937598182249688132410408196248344124873312357113507547279516555232213828894450359208043156415786747942667569034278302758632332792966168589148561952529827676455867620718491121519566778267284946472728779868353591764888425242532390195600063363530440734986641188564192928244738736960706468674352679639622457403160943430629924686778742180857897037492647553965360571395423889393190136792132808820964280842098926546195726573957235872072771514703732911328337271024285339124370833436519397291514689017778437236176793239632328400000098733961965128704164600778553840026356764016848771447647824695319245247442729526830585151011874390308115411481581839039062066822663560644921007465348898536790234192750403963145799934864044189453125E-1807
It can be store at least 6000 records...(with small possibility, always minimum key/2)
the implementation of Java's BigDecimal with some flaws, you can implement a own one if necessary.

Import F# block of code into C# application [duplicate]

This question already has answers here:
Run F# code in C# - (Like C# in C# using Roslyn)
(3 answers)
Closed 9 years ago.
I was looking for solution to import F# block of code into C# application (in order to gain calculation speed of some operations I have). Therefore I install FSharpx.Core to my C# solution in MS Visual Studio Express 2013.
And that's it...don't know how to continue :-)
Can someone help me with some simple code - e.g. when I click a button in the form, to subtract variable v1 and v2.
Form1.cs
...
private void buttonCalcVars_Click(object sender, EventArgs e)
{
int sum = CallFSharpFunction(1,2);
}
// This should be some F# block of code
private int CallFSharpFunction(int a, int b)
{
let v1 = a;
let v2 = b;
// do some calculations
return result;
}
...
Hope this is possible,
Thank you!
Before using F # one wonders, can you use an F # function in applications written in c #.
Become familiar with functional language F # you can, for example, at this address: http://msdn.microsoft.com/ru-ru/magazine/cc164244.aspx
But here, in my opinion, F # functional language is good for writing various mathematical functions (excuse my tautology), but why take the bread from the object-oriented languages, why put unintelligible code to work with WinForms or WebForms controls pages in F #? Because I immediately wondered how to invoke a function from F # assemblies. Just want to say that because there are difficulties in functional languages with tipizaciâmi when writing code, in the case of using F # functions from C # assemblies these difficulties only increase. Get down to business.
Create a project that includes, for example, the C # console application and an F # Assembly.
F # in the Assembly we need one file MyFunctions. fs. Here we describe some of the features that we believe it is easier for us to write at a functional language. For example, even if it is the translation function array bitmap images from RGB to YCbCr color space (this is just an example). Entry in F # can be roughly this:
open System
let RGB_to_YCbCr (r : double,g : double,b : double) =
let y = 0.299 * r + 0.587 * g + 0.114 * b in
let Cb = (-0.1687) * r - 0.3313 * g + 0.5 * b + 128.0 in
let Cr = 0.5 * r - 0.4187 * g - 0.0813 * b + 128.0 in
(y,Cb,Cr);
let RGB_to_YCbCr_v (v : _ array) =
RGB_to_YCbCr (v.[0], v.[1], v.[2]);
let Process (image : _ array) =
let lenght = Array.length image in
let imageYCbCr = Array.create lenght (0.0, 0.0, 0.0) in
for index = 0 to lenght - 1 do
imageYCbCr.[index] <- RGB_to_YCbCr_v (image.[index])
done
imageYCbCr
After Assembly, we can see that access to features not just have weird views the namespace, and how to use them is not clear. See the location of the functions in the Assembly we can using Reflector.
In order to describe the namespace and the class you need to add the following line immediately after #light:
module FSharp.Sample.MyFunctions
That said, the fact that all the functions written below will contain the class MyFunctions fsharp.Core namespace.
After the project again, we will see that in the Assembly have a clear the fsharp.Core namespace Sample that has the class MyFunctions static methods, which we have described above.
Read more in our console application, we set the Reference to the Assembly the fsharp.core Sample is the name of my Assembly to F # and the fsharp.core -in order to use types (classes) of the F # type Triple. And write the following code:
using System;
using FSharp.Sample;
namespace CSharp.Sample.Console
{
class Program
{
static void Main()
{
double[][] image = new double[1000][];
Random rand = new Random();
for (int i = 0; i < 1000; i ++ )
{
image[i] = new double[3];
image[i][0] = rand.Next();
image[i][1] = rand.Next();
image[i][2] = rand.Next();
}
foreach (var doubles in MyFunctions.Process(image))
{
System.Console.WriteLine(doubles);
}
}
}
}
Where initially we specify to use the fsharp.Core namespace. In the code we generate an array of data and pass it to the function MyFunction. Process, which converts it to the chosen algorithm. We are returned as an array of data types "in Microsoft fsharp.core.. Tuple'3".

how to join two Lists in linq

i have two List A,B which consists integer values ,list A contains 40 to 1 list B contains 40 to 79 i like to both values except 40 and store it in another list using Linq.The resultant list like this {80,80...}. how can i do this? Is it possible to do this?
It sounds like you're trying to "join" these in a pairwise fashion by index: the first element from each list, then the second element etc. That suggests you want Zip, which was introduced in .NET 4:
var zipped = list1.Zip(list2, (x1, x2) => x1 + x2);
If you're using .NET 3.5, you can use a separate implementation of the same method, such as the one in MoreLINQ.
EDIT: Alternatively, Eric Lippert posted some source code for Zip a while ago, too.
Check out the IEnumerable<T>.Join() method.
using System;
using System.Linq;
class Program
{
static void Main()
{
// Two source arrays.
var array1 = new int[] { 1, 2, 3, 4, 5 };
var array2 = new int[] { 6, 7, 8, 9, 10 };
// Add elements at each position together.
var zip = array1.Zip(array2, (a, b) => (a + b));
// Look at results.
foreach (var value in zip)
{
Console.WriteLine(value);
}
}
}
--- Output of the program ---
7
9
11
13
15
Try Joining them together
http://weblogs.asp.net/rajbk/archive/2010/03/12/joins-in-linq-to-sql.aspx
http://msdn.microsoft.com/en-us/library/bb397676.aspx

Spelling Suggestor in ASP.NET

I need to build a spelling suggestor in ASP.NET... The below are my requirement.
Case 1: My list of words are not just englist words but will also includes some codes like AACD, ESSA, BIMER etc... I may provide such (New) words from Database.
Case 2: I also need a similar spelling suggestor for Non-English Language, Even here, I can provide a list of words from a Database.
Now, Any suggestions as to how I implement the same is welcome.
Further, I found the following Python Code, from a website, which states it returns the most probable suggestion (in english ofcourse). If someone can translate it into C# that would be really helpful.
import re, collections
def words(text): return re.findall('[a-z]+', text.lower())
def train(features):
model = collections.defaultdict(lambda: 1)
for f in features:
model[f] += 1
return model
NWORDS = train(words(file('big.txt').read()))
alphabet = 'abcdefghijklmnopqrstuvwxyz'
def edits1(word):
s = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [a + b[1:] for a, b in s if b]
transposes = [a + b[1] + b[0] + b[2:] for a, b in s if len(b)>1]
replaces = [a + c + b[1:] for a, b in s for c in alphabet if b]
inserts = [a + c + b for a, b in s for c in alphabet]
return set(deletes + transposes + replaces + inserts)
def known_edits2(word):
return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)
def known(words): return set(w for w in words if w in NWORDS)
def correct(word):
candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
return max(candidates, key=NWORDS.get)
Thanks
- Raja
Another alternative is NHunspel
NHunspell is a free open source spell
checker for the .NET Framework. C# and
Visual Basic sample code is available
for spell checking, hyphenation and
sysnonym lookup via thesaurus.
using (Hunspell hunspell = new Hunspell("en_us.aff", "en_us.dic"))
{
bool correct = hunspell.Spell("Recommendation");
var suggestions = hunspell.Suggest("Recommendatio");
foreach (string suggestion in suggestions)
{
Console.WriteLine("Suggestion is: " + suggestion );
}
}
The commercial product I work on uses NETSpell Spell Checker, it has a dictionary tool that allows you to add custom dictionaries and words.
Free .NET spell checker based around a WPF text box that can be used client or server side can be seen here. This can be passed a list of words to ignore (your custom dictionary)
Full disclosure...written by yours truly with some help from stack overflow of course :)

Categories

Resources