English 中文(简体)
如何将段落转换为 root 单词
原标题:How to convert the paragraph into root words

原谅我, Im 试图做简单的应用, 将单词转换成单词 。 例如 。

Enable = able Payment = Pay

我的笔记本上写着"可以支付"

i m 使用上面两个样本单词, 并且 i m 没有得到我需要的东西 。 我写了“ enable” 和“ paying ” 在注解板 txt 上 。 然后应用程序将启动并获得该单词 。

应用程序将开始对“ En” 和“ ment” 和“ ment”

我的应用程序可以将单词“ enable” 修饰为“ ableable”. 和“ pay” 的“ pay” 修饰为“ enable” 。 但是, 如果我写两个词或更多词, 如“ enable pay” 或“ pays forable”, 申请效果不佳 。

这里是用户界面""http://i1085.photobucket.com/albums/j423/Nencor_Theleftbehind/1.jpg" rel="no follow">image

这是源代码,

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace KTM 
{
public partial class KTM : Form
{
    public string notepad;
    public KTM()
    {
        InitializeComponent();
        textBox1.Enabled=false;
        button2.Enabled = false;
        button3.Enabled = false;
    时 时

    void enable()
    {
        button2.Enabled = true;
        button3.Enabled = true;
    时 时
    private void button1_Click(object sender, EventArgs e)
    {

        string dir = Application.StartupPath.ToString();
        OpenFileDialog fdlg = new OpenFileDialog();
        fdlg.Title = "Open *txt files";
        fdlg.InitialDirectory = @dir;
        fdlg.Filter = "Text files (*.txt)|*.txt|Text Files (*.txt)|*.txt";
        fdlg.FilterIndex = 2;
        fdlg.RestoreDirectory = true;
        if (fdlg.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = fdlg.FileName;
        时 时
        else
        {

        时 时


      时 时

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        if (textBox1.Text.Equals(""))
        {

        时 时
        else
        {
            enable();
        时 时
    时 时

    private void button2_Click(object sender, EventArgs e)
    {
        notepad = textBox1.Text;

        StreamReader sr = new StreamReader(notepad);
        string paragraf = sr.ReadToEnd();
        sr.Close();
        string[] kata = paragraf.Split(   );
        int i = 0;

        //MessageBox.Show(kata[0]+" "+kata[1]+" "+kata[2]);
        foreach (string ambil in kata)
        {
            if (kata[i].StartsWith("en"))
            {
                kata[i] = kata[i].Substring(2);
            时 时
            if(kata[i].EndsWith("ment"))
            {
                int len = kata[i].Length;
                int kepake = len - 4;
                kata[i] = kata[i].Substring(0, kepake);
            时 时


        时 时
        i++;
        StreamWriter sw = new StreamWriter(notepad);
        i = 0;
        foreach (string ambil in kata)
        {
            sw.Write(kata[i]+" ");
        时 时
        i++;
        sw.Flush();
        sw.Close();




        MessageBox.Show("Converted and Saved  ","KTM Stemming",MessageBoxButtons.OK,MessageBoxIcon.Information);
    时 时

    private void button3_Click(object sender, EventArgs e)
    {
        notepad = textBox1.Text;
        System.Diagnostics.Process.Start(notepad);
    时 时
时 时

时 时

最佳回答
    foreach (string ambil in kata)
    {
        if (kata[i].StartsWith("en"))
        {
            kata[i] = kata[i].Substring(2);
        }
        if(kata[i].EndsWith("ment"))
        {
            int len = kata[i].Length;
            int kepake = len - 4;
            kata[i] = kata[i].Substring(0, kepake);
        }


    }

这是您问题的一部分, 因为它是一个预言, 但是您正在使用它作为环形 。 您可能想要 < code > (int i=0; i<kata. Length; i++) , 您也正在将它写到环形文件之外, 所以要么您需要将您的写作放入环形文件, 要么做另一个环形文件 。

问题回答

您对每个环的重新定位设计不当 。 由于您正在更新环内的数组, 您最好用一个迭代器来做一个简单的循环操作 :

    for (int i = 0; i < kata.Length; i++)
    {
        if (kata[i].StartsWith("en"))
        {
            kata[i] = kata[i].Substring(2);
        }
        if(kata[i].EndsWith("ment"))
        {
            int len = kata[i].Length;
            int kepake = len - 4;
            kata[i] = kata[i].Substring(0, kepake);
        }
    }

但是,在第二个循环中,预言是有道理的。你需要使用调味剂变量,尽管:

    foreach (string ambil in kata)
    {
        sw.Write(ambil + " ");
    }

编辑:

你说过,上述内容行不通,但对我却有效。也许你的问题与你格式的其他代码有关。由于用同一方法混合不同逻辑的做法不是好做法,我建议打破将逻辑解析成自身方法的根词,例如:

    private string getRootWords(string paragraf)
    {
        string[] kata = paragraf.Split(   );
        for (int i = 0; i < kata.Length; i++)
        {
            if (kata[i].StartsWith("en"))
            {
                kata[i] = kata[i].Substring(2);
            }
            if (kata[i].EndsWith("ment"))
            {
                int len = kata[i].Length;
                int kepake = len - 4;
                kata[i] = kata[i].Substring(0, kepake);
            }
        }

        StringBuilder builder = new StringBuilder();
        foreach (string ambil in kata)
        {
            builder.Append(ambil + " ");
        }
        return builder.ToString();
    }

这个方法对我有用。 当我调用getRoootWords (“ 可用付款”) (“ 可用付款”) < / code> 时, 它返回“ 可用薪酬 ”, 这是您想要的输出, 据我的理解。

您正在循环外加载索引变量 > i , 您应该在内部加载 :

    foreach (string ambil in kata) 
    { 
        if (kata[i].StartsWith("en")) 
        { 
            kata[i] = kata[i].Substring(2); 
        } 
        if(kata[i].EndsWith("ment")) 
        { 
            int len = kata[i].Length; 
            int kepake = len - 4; 
            kata[i] = kata[i].Substring(0, kepake); 
        } 
        i++; 
    } 

最好还是直接使用 < code> 的 < /code> 环( 您没有使用 < code> ambil 变量) :

    for (i=0; i<kata.Lenght; i++) 
    { 
        if (kata[i].StartsWith("en")) 
        { 
            kata[i] = kata[i].Substring(2); 
        } 
        if(kata[i].EndsWith("ment")) 
        { 
            int len = kata[i].Length; 
            int kepake = len - 4; 
            kata[i] = kata[i].Substring(0, kepake); 
        } 
    } 

谢谢你们的帮助! 我真的很感激, 虽然我不明白答案: 至少你们能帮助我构思逻辑,

 private void button2_Click(object sender, EventArgs e)
    {
        notepad = textBox1.Text;

        StreamReader sr = new StreamReader(notepad);
        string paragraf = sr.ReadToEnd();
        sr.Close();
        StreamWriter sw = new StreamWriter(notepad);
        string[] kata = paragraf.Split(   );

        for (int i = 0; i < kata.Length; i++)
        {
            if (kata[i].StartsWith("en"))
            {
                kata[i] = kata[i].Substring(2);
                //cutting the EN*
            }

            if (kata[i].EndsWith("ment"))
            {
                int panjang = kata[i].Length;
                int sisa = panjang - 4;
                kata[i] = kata[i].Substring(0, sisa);
                //cutting the *Ment
            }

            if(kata[i].EndsWith("ous"))
            {
                int panjang = kata[i].Length;
                int sisa = panjang - 3;
                kata[i] = kata[i].Substring(0, sisa);
                //cutting the ous, tremendous, hazardous
            }

            if (kata[i].EndsWith("ied"))
            {
                int panjang = kata[i].Length;
                int sisa = panjang -1;
                kata[i] = kata[i].Substring(0, sisa);
                //cutting ied
            }

            if (kata[i].EndsWith("ly"))
            {
                int panjang = kata[i].Length;
                int sisa = panjang - 2;
                kata[i] = kata[i].Substring(0, sisa);
                //cutting fluently, diligently
            }
            sw.Write(kata[i] + " ");
        }

        sw.Flush();
        sw.Close();
        MessageBox.Show("Converted and Saved  ", "KTM Stemming", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...