Removing the helping words from sentence - c#

I have made a simple program in which a phrase is written and it displays the videos matching individual word.Let say i entered "I go to school". Here it should eliminate word "to"from the sentence and return just three words.
Here is a code which i have tried yet!! It works fine but when i enter some phrase it removes the helping verb and besides it, it replaces an empty string which makes problem. Any suggestions please
Code
class MyPlayer
{
string complete_name;
string root;
string[] supportedExtensions;
string videoname;
public MyPlayer(string snt)
{
videoname = snt;
}
public List<VideosDetail> test()
{
complete_name = videoname.ToLower() + ".wmv";
root = System.IO.Path.GetDirectoryName(#"C:\Users\Administrator\Desktop\VideosFrame\VideosFrame\Model\");
supportedExtensions = new[] { ".wmv" };
var files = Directory.GetFiles(Path.Combine(root, "Videos"), "*.*").Where(s => supportedExtensions.Contains(Path.GetExtension(s).ToLower()));
List<VideosDetail> videos = new List<VideosDetail>();
VideosDetail id;
bool flagefilefound = false;
foreach (var file in files)
{
id = new VideosDetail()
{
Path = file,
FileName = Path.GetFileName(file),
Extension = Path.GetExtension(file)
};
FileInfo fi = new FileInfo(file);
if (id.FileName == complete_name)
{
id.FileName = fi.Name;
id.Size = fi.Length;
videos.Add(id);
flagefilefound = true;
}
if (flagefilefound)
break;
}
if (!flagefilefound)
{
MessageBox.Show("no such video is available. ");
}
return videos;
}
}
private void play_Click(object sender, RoutedEventArgs e)
{
List<string> chk = new List<string>();
chk.Add("is");
chk.Add("am");
chk.Add("are");
chk.Add("were");
chk.Add("was");
chk.Add("do");
chk.Add("does");
chk.Add("has");
chk.Add("have");
chk.Add("an");
chk.Add("the");
chk.Add("to");
chk.Add("of");
string sen = vdo.Text;
List<string> tmp = new List<string>();
string[] split = sen.Split(' ');
foreach (var item in split)
{
tmp.Add(item);
}
foreach (var item in chk)
{
if( sen.Contains(item) )
{
int index = sen.IndexOf(item);
sen = sen.Remove(index,item.Length);
};
}
foreach (var i in tmp)
{
MyPlayer player = new MyPlayer(i);
VideoList.ItemsSource = player.test();
}
}

What you're actually doing is eliminating so called stop words, and, probably, creating bag of words:
private static HashSet<String> s_StopWords =
new HashSet<String>(StringComparer.OrdinalIgnoreCase) {
"is", "am", "are", "were", "was", "do", "does", "to", "from", // etc.
};
private static Char[] s_Separators = new Char[] {
'\r', '\n', ' ', '\t', '.', ',', '!', '?', '"', //TODO: check this list
};
...
String source = "I go to school";
// ["I", "go", "school"] - "to" being a stop word is removed
String[] words = source
.Split(s_Separators, StringSplitOptions.RemoveEmptyEntries)
.Where(word => !s_StopWords.Contains(word))
.ToArray();
// Combine back: "I go school"
String result = String.Join(" ", words);

Related

I want to load a text file into a data grid and apply a regular expression

string[] s = File.ReadAllLines(ofdl.FileName);
List<code> codes = new List<code>();
string textfile = ofdl.FileName;
var textvalues = s;
foreach (var item in textvalues)
{
codes.Add(new code() { Value = RemoveEmptyLines(item) });
}
dataGrid.ItemsSource = codes;
under_label.Content = textfile;
under_label1.Content = codes.Count();
private string RemoveEmptyLines(string lines)
{
return lines = Regex.Replace(lines, #"\n\s.+", "");
}
I want to load a text file into a data grid and apply a regular expression
but this code don't work for me
you don't need Regex to search for empty strings. String.IsNullOrWhiteSpace() method will do.
string[] lines = File.ReadAllLines(ofdl.FileName);
var codes = lines.Where(s => !String.IsNullOrWhiteSpace(s)).ToList();
dataGrid.ItemsSource = codes;
under_label.Content = ofdl.FileName;
under_label1.Content = codes.Count;
datagrid
I want to exclude the part of the circle separately
List<code> codes = new List<code>();
string[] s = File.ReadAllLines(ofdl.FileName);
string textfile = ofdl.FileName;
var textvalues = s;
foreach (var item in textvalues)
{
codes.Add(new code() { Value = item});
}
dataGrid.ItemsSource = codes;
}
}
}
private void streams()
{
}
private string RemoveEmptyLines(string lines)
{
return lines = Regex.Replace(lines, #"\n\s.+", "");
}

Implementing search in mvc 4

I have a textbox where user types the string he needs to search. If the user enters only a single word string, then I am able to retrieve the correct data from database but if the user enters a multi-word string then my code fails.
I am using EntityFramework to get the data.
Here is my code to get the data using a single word string.
public ActionResult SearchResult(string search)
{
var j = objCon.Mobiles.Where(oh => oh.MobileName.Contains(search) || oh.Description.Contains(search));
List<Mobiles> prod = new List<Mobiles>();
foreach (var p in j)
{
Mobiles Mob = new Mobiles();
Mob.Description = p.Description;
Mob.ImgUrl = p.Url;
Mob.MobileName = p.MobileName;
Mob.Price = Convert.ToString(p.Price);
Mob.SlNo = p.SlNo;
prod.Add(Mob);
}
return View("~/Views/Product/Index.cshtml", prod);
}
I tried breaking the string into single word using split but could not get the correct data.
string str = null;
string[] strArr = null;
int count = 0;
str = //UserInput;
char[] splitchar = { ' ' };
strArr = str.Split(splitchar);
string str = null;
string[] strArr = null;
int count = 0;
str = search;
char[] splitchar = { ' ' };
strArr = str.Split(splitchar);
for (count = 0; count <= strArr.Length - 1; count++)
{
string i = strArr[count];
var j = objCon.Mobiles.Where(oh => oh.MobileName.Contains(i) || oh.Description.Contains(i));
//MessageBox.Show(strArr[count]);
foreach (var p in j)
{
Mobiles Mob = new Mobiles();
Mob.Description = p.Description;
Mob.ImgUrl = p.Url;
Mob.MobileName = p.MobileName;
Mob.Price = Convert.ToString(p.Price);
Mob.SlNo = p.SlNo;
prod.Add(Mob);
}
}
as I help you fix the problem - this is the final code
I Wrote an Example to Solve your Problem. Hope That You will Be Benefited From The Code.
First Create Mobile Class:
public class Mobile
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Next Create Extension method To Check If there is Value:
public static bool ContainsAny(this string haystack, params string[] needles)
{
foreach (var needle in needles)
{
if (haystack.Contains(needle))
return true;
}
return false;
}
Finally Create Main Body Along with Test Data:
using System;
using System.Collections.Generic;
using System.Linq;
namespace StackOverFlow
{
static class Program
{
static void Main()
{
List<Mobile> mobiles = new List<Mobile>
{
new Mobile{Id = 1,Name = "samsung galaxy s3",Description = "model"},
new Mobile{Id = 2,Name = "nokia N67",Description = "nokia n96 time"},
new Mobile{Id = 3,Name = "iphone 5s",Description = "test"},
new Mobile{Id = 4,Name = "samsung galaxy packet",Description = "this time"},
new Mobile{Id = 5,Name = "iphone ipad",Description = "now"},
new Mobile{Id = 6,Name = "glx c5",Description = "time"},
};
string[] search = "galaxy time 5s".Split(' ');
var result = mobiles.Where(c => c.Name.ContainsAny(search) ||
c.Description.ContainsAny(search)).ToList();
foreach (var item in result)
{
Console.WriteLine(item.Id + "-" + item.Name + "-" + item.Description);
}
Console.ReadKey();
}

I encountered System.IndexOutOfRangeException when loading a txt file

I am trying the load a txt file which is written under a certain format, then I have encountered System.IndexOutOfRangeException. Do you have any idea on what's wrong with my codes? Thank you!
txt.File:
P§Thomas§40899§2§§§
P§Damian§40726§1§§§
P=Person; Thomas=first name; 40899=ID; 2=status
here are my codes:
using (StreamReader file = new StreamReader(fileName))
{
while (file.Peek() >= 0)
{
string line = file.ReadLine();
char[] charSeparators = new char[] { '§' };
string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (PersonId personids in PersonIdDetails)
{
personids.ChildrenVisualisation.Clear();
foreach (PersonId personidchildren in personids.Children)
{
personidchildren.FirstName = parts[1];
personidchildren.ID = parts[2];
personidchildren.Status = parts[3];
personids.ChildrenVisualisation.Add(personidchildren);
}
}
}
}
at parts[1] the exception was thrown.
You should check if parts have enough items:
...
string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (PersonId personids in PersonIdDetails) {
personids.ChildrenVisualisation.Clear();
// Check if parts have enough infirmation: at least 3 items
if (parts.Length > 3) // <- Pay attention for "> 3"
foreach (PersonId personidchildren in personids.Children) {
//TODO: Check, do you really start with 1, not with 0?
personidchildren.FirstName = parts[1];
personidchildren.ID = parts[2];
personidchildren.Status = parts[3];
personids.ChildrenVisualisation.Add(personidchildren);
}
else {
// parts doesn't have enough data
//TODO: clear personidchildren or throw an exception
}
}
...
I'm given the impression that the actual file is not that big, so it might be useful to use File.ReadAllLines instead (the con is the you need to have the entire file in memory), which gives you all the lines.
Also, removing the lines which are either empty or just whitespace might be necessary.
foreach (var line in File.ReadAllLines(fileName).Where(l => !string.IsNullOrWhiteSpace(l))
{
char[] charSeparators = new char[] { '§' };
string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (PersonId personids in PersonIdDetails)
{
personids.ChildrenVisualisation.Clear();
foreach (PersonId personidchildren in personids.Children)
{
personidchildren.FirstName = parts[1];
personidchildren.ID = parts[2];
personidchildren.Status = parts[3];
personids.ChildrenVisualisation.Add(personidchildren);
}
}
}
Change the first line to
using (StreamReader file = new StreamReader(fileName, Encoding.GetEncoding("iso-8859-1")));
Possible way to do it, It's just one solution between more solutions
using (StreamReader file = new StreamReader(fileName))
{
while (file.Peek() >= 0)
{
string line = file.ReadLine();
char[] charSeparators = new char[] { '§' };
string[] parts = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
foreach (PersonId personids in PersonIdDetails)
{
personids.ChildrenVisualisation.Clear();
foreach (PersonId personidchildren in personids.Children)
{
if(parts.Length > 3)//Only if you want to save lines with all parts but you can create an else clause for other lines with 1 or 2 parts depending on the length
{
personidchildren.FirstName = parts[1];
personidchildren.ID = parts[2];
personidchildren.Status = parts[3];
personids.ChildrenVisualisation.Add(personidchildren);
}
}
}
}
}

CSV appending specific strings in Dictionary

There are two CSV files I am looking to consolidate into 1.
A.CSV
WBS Element,Purchasing Document,Purchase order text,Val/COArea Crcy
ABC123,,,75000
ABC124,4200028630,Service,1069.2
ABC124,4200041490,Service,25518.24
ABC124,4200041490,Service,-1890.24
ABC126,4200028630,Service,2268
ABC126,4200028630,Service,-2268
ABC126,4200029435,Service,25149.65
ABC137,,,4146.2
B.CSV
WBS Element,Ref Document Number,Val/COArea Crcy,Name
ABC124,1000060610,0,Slab Locates & Steel Differential
ABC124,1000081223,0,NOCN339A&3921
ABC124,1000081223,0,Slab Locates & Steel Differential
ABC126,1000067757,0,Structural Steel
ABC 137,4200041490,0,Service
ABC 137,4200028630,5393.52,Service
ABC 137,4200029435,0,Service
I want to make 1 CSV file that combines both of these. The lines starting with WBS Element are joined together. The WBS Elements from each file are then placed on the same line if they match. If A has a WBS Element B does not, then the section for B is just "," and vice versa.
Sample target output:
WBS Element,Purchasing Document,Purchase order text,Val/COArea Crcy,WBS Element,Ref Document Number,Val/COArea Crcy,Name
ABC123,,,75000,,,,
ABC124,4200028630,Service,1069.2,ABC124,1000060610,0,Slab Locates & Steel Differential
I have the following code:
static void Main(string[] args)
{
StreamReader a = new StreamReader(#"Input\a.csv");
StreamReader b = new StreamReader(#"Input\b.csv");
StreamWriter output = new StreamWriter(#"Output\output.csv");
Dictionary<string, string> Adict = new Dictionary<string, string>();
Dictionary<string, string> Bdict = new Dictionary<string, string>();
output.WriteLine(a.ReadLine() + "," + b.ReadLine());
while (!a.EndOfStream && !b.EndOfStream)
{
//section for A
List<string> atempList = new List<string>();
string atempString;
string Aline = a.ReadLine();
string[] Atokens = Aline.Split(','); //split the line into array
foreach (string s in Atokens)
atempList.Add(s); //add each string in token array to tempList
atempList.Remove(Atokens[0]); //remove Dict Key from tempList
StringBuilder d = new StringBuilder();
if (!Adict.ContainsKey(Atokens[0]))
{
foreach (string s in atempList)
d.Append(s + ","); //rejoin tempList into a string with ","
d.Append("\n"); //add a linebreak to end of templist string
Adict.Add(Atokens[0], d.ToString()); //Add line to dictionary with Key
}
else //Adict does contain key... need to remove Key and add bigger string
{
List<string> removeKey = new List<string>(); //temporary list
foreach (string s in Atokens)
removeKey.Add(s); //create a new list from the token array
removeKey.Remove(Atokens[0]); //remove the key from the removeKey list
atempString = Adict[Atokens[0]]; //temporary string is what's already in dictionary
Adict.Remove(Atokens[0]); //remove the Key + Value from dictionary.
Adict.Add(Atokens[0], d.Append(atempString + Aline + "\n").ToString()); // string.Concat(tempString, ",", line));
}
//section for B
List<string> btempList = new List<string>();
string btempString;
string Bline = b.ReadLine();
string[] Btokens = Bline.Split(',');
foreach (string s in Btokens)
btempList.Add(s);
btempList.Remove(Btokens[0]);
StringBuilder f = new StringBuilder();
if (!Bdict.ContainsKey(Btokens[0]))
{
foreach (string s in btempList)
f.Append(s + ",");
f.Append("\n");
Bdict.Add(Btokens[0], f.ToString());
}
else
{
List<string> removeKey = new List<string>();
foreach (string s in Btokens)
removeKey.Add(s);
removeKey.Remove(Atokens[0]);
btempString = Bdict[Btokens[0]];
Bdict.Remove(Btokens[0]);
Bdict.Add(Btokens[0], f.Append(btempString + Bline + "\n").ToString());
}
}
output.Close();
// Console.ReadLine();
}
}
I am stuck now I dont know how to look through each Dictionary and compare keys, then join (insert?) just the line that has a matching key.
first of all, I think you should make a class to use this.
The class I made for this problem is really simple:
class WbsElement
{
public string PurchasingDocument;
public string PurchaseOrderText;
public string ValCoAreaCrcyA;
public string ValCoAreaCrcyB;
public string RefDocumentNumber;
public string Name;
}
It has some attirbutes that you can use to store the data.
Then I took your code and changed it to this:
private static void Main(string[] args)
{
StreamReader a = new StreamReader(#"A.CSV");
StreamReader b = new StreamReader(#"B.CSV");
StreamWriter output = new StreamWriter(#"output.csv");
Dictionary<string, WbsElement> newDict = new Dictionary<string, WbsElement>();
output.WriteLine(a.ReadLine() + "," + b.ReadLine());
while (!a.EndOfStream && !b.EndOfStream)
{
//section for A
string Aline = a.ReadLine();
string[] Atokens = Aline.Split(','); //split the line into array
if (newDict.ContainsKey(Atokens[0]))
{
newDict[Atokens[0]].PurchasingDocument = Atokens[1];
newDict[Atokens[0]].PurchaseOrderText = Atokens[2];
newDict[Atokens[0]].ValCoAreaCrcyA = Atokens[3];
}
else
{
WbsElement elementToAdd = new WbsElement();
elementToAdd.PurchasingDocument = Atokens[1];
elementToAdd.PurchaseOrderText = Atokens[2];
elementToAdd.ValCoAreaCrcyA = Atokens[3];
newDict.Add(Atokens[0], elementToAdd);
}
}
while (!b.EndOfStream)
{
//section for B
string Bline = b.ReadLine();
string[] Btokens = Bline.Split(',');
if (newDict.ContainsKey(Btokens[0]))
{
newDict[Btokens[0]].RefDocumentNumber = Btokens[1];
newDict[Btokens[0]].ValCoAreaCrcyB = Btokens[2];
newDict[Btokens[0]].Name = Btokens[3];
}
else
{
WbsElement elementToAdd = new WbsElement();
elementToAdd.RefDocumentNumber = Btokens[1];
elementToAdd.ValCoAreaCrcyB = Btokens[2];
elementToAdd.Name = Btokens[3];
newDict.Add(Btokens[0], elementToAdd);
}
}
foreach (KeyValuePair<string, WbsElement> keyValuePair in newDict)
{
output.WriteLine(string.Format("{0},{1},{2},{3},{4},{5},{6},{7}", keyValuePair.Key, keyValuePair.Value.PurchasingDocument,
keyValuePair.Value.PurchaseOrderText, keyValuePair.Value.ValCoAreaCrcyA,
keyValuePair.Key,
keyValuePair.Value.RefDocumentNumber, keyValuePair.Value.ValCoAreaCrcyB,
keyValuePair.Value.Name));
}
output.Close();
// Console.ReadLine();
}
I make a new dictionary that stores the key + one instance of the class I made.
When I find the same key again, I just add the information to the class.
On the end of the application I just flush all the correct data to the output stream.
The class is the key to making this easy.
In case you want it generic for different length of data input, you could use this:
private static void Main(string[] args)
{
StreamReader a = new StreamReader(#"A.CSV");
StreamReader b = new StreamReader(#"B.CSV");
StreamWriter output = new StreamWriter(#"output.csv");
Dictionary<string, List<string>> newDict = new Dictionary<string, List<string>>();
string aLine = a.ReadLine();
int aLength = aLine.Split(',').Count();
output.WriteLine(aLine + "," + b.ReadLine());
while (!a.EndOfStream && !b.EndOfStream)
{
//section for A
string Aline = a.ReadLine();
string[] Atokens = Aline.Split(','); //split the line into array
if (newDict.ContainsKey(Atokens[0]))
{
for (int i = 0; i < Atokens.Length; i++)
{
newDict[Atokens[0]][i] = Atokens[i];
}
}
else
{
List<string> listToAdd = new List<string>();
for (int i = 0; i < Atokens.Length; i++)
{
listToAdd.Add(Atokens[i]);
}
newDict.Add(Atokens[0], listToAdd);
}
}
while (!b.EndOfStream)
{
//section for B
string Bline = b.ReadLine();
string[] Btokens = Bline.Split(',');
if (newDict.ContainsKey(Btokens[0]))
{
if (newDict[Btokens[0]].Count > aLength)
{
for (int i = 0; i < Btokens.Length; i++)
{
newDict[Btokens[0]][i + aLength] = Btokens[i];
}
}
else
{
for (int i = 0; i < Btokens.Length; i++)
{
newDict[Btokens[0]].Add(Btokens[i]);
}
}
}
else
{
List<string> listToAdd = new List<string>(aLength);
listToAdd.AddRange(Btokens);
newDict.Add(Btokens[0], listToAdd);
}
}
foreach (KeyValuePair<string, List<string>> keyValuePair in newDict)
{
string outputLine = string.Empty;
foreach (string s in keyValuePair.Value)
{
if (outputLine != string.Empty)
{
outputLine += ",";
}
outputLine += s;
}
output.WriteLine(outputLine);
}
output.Close();
// Console.ReadLine();
}
It uses a list to keep track of input data.

How to dynamically create objects from a file?

I'm reading in files such as this with multiple lines like this.
"title,name,something,something,something"
How would I dynamically create new objects with those variables
I am splitting it already -
while (!reader.EndOfStream)
{
lineFromFile = reader.ReadLine();
split = lineFromFile.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
My class is called Modules. Just don't know how to do it dynamically since I don't know how many modules will be in the file ETC.
Create List<Module> and add all items read from your file there:
List<Module> modules = new List<Module>();
while (!reader.EndOfStream)
{
lineFromFile = reader.ReadLine();
split = lineFromFile.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var newModule = new Module();
newModule.Property1 = split[0];
newModule.Property2 = split[1];
// (...) //
modules.Add(newModule);
}
Or using LINQ:
var modules = (from line in File.ReadAllLines("fileName")
let parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
select new Module() {
Property1 = parts[0],
Property2 = parts[1],
Property3 = parts[2],
}).ToList();

Categories

Resources