SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Java Scripting

  1. #1
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Java Scripting

    Hi,

    I am trying to build a script, well actually I've completed it but one thing I've problem with is that its not compeletely doing what I want it to do.

    Basically I want it to copy and convert the text from one text box to another text box.

    When I say convert I mean:
    1) all the convert everything to lowercase [i've done it]
    2) locate i and replace it with 'I'
    3) place comma (,) after every space [i've done it]
    4) place comma (,) before period/fullstop (.)

    [Sample]
    Input:

    This is a sample Input, I want to convert to Prolog List based text.

    Output:
    [this,is,a,sample,input,'I',want,to,convert,to,prolog,list,based,text,.]

    As you can see from the input and output it sound simple but I am having trouble locating I and not changing it to 'I' and also adding the (,) before the period.

    any help is appriciated.

    Here is the Entire Code so Far you can copy, paste and test it.:
    Code:
    <html>
    <head>
    <meta name=VI60_defaultClientScript content=JavaScript>
    <title>Eliza Prolog Rule Creator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <link rel="stylesheet" href="gs.css" type="text/css">
    
    <SCRIPT LANGUAGE=javascript>
    <!--
    
    function e2p_onclick() {
    if (frmMain.English.value == "")
    	{
    		alert("Please enter some text in English to convert to Eliza based text.");
    	}
    	else
    	{
    		//alert("We are ready to convert");
    		var str = frmMain.English.value;
    		var str2 = str.toLowerCase();
    		frmMain.Prolog.value = str2.split(" ");
    	}
    }
    
    //-->
    </SCRIPT>
    <SCRIPT LANGUAGE=javascript>
    <!--
    
    function p2e_onclick() {
    if (frmMain.Prolog.value =="")
    	{
    		alert("Please enter some Eliza based text to convert to English text.");
    	}
    	else
    	{
    		//alert("We are ready to convert");
    		var str =new Array(frmMain.Prolog.value);
    		
    		frmMain.English.value = str.join(" ");
    	}
    }
    
    //-->
    </SCRIPT>
    </head>
    
    <body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <form name="frmMain" action="">
      <table width="200" border="0" cellspacing="0" cellpadding="8">
    	<tr> 
    	  <td class="header3">English:</td>
    	</tr>
    	<tr> 
    	  <td> 
    		<textarea wrap="VIRTUAL" cols="60" rows="4" name="English"></textarea>
    	  </td>
    	</tr>
    	<tr> 
    	  <td> 
    		<input type="button" name="e2p" value="English -&gt; Prolog" LANGUAGE=javascript onclick="return e2p_onclick()">
    		<input type="hidden" name="p2e" value="Prolog -&gt; English" LANGUAGE=javascript onclick="return p2e_onclick()">
    		<input type="reset" name="Reset" value="Reset">
    	  </td>
    	</tr>
    	<tr> 
    	  <td class="header3">Prolog:</td>
    	</tr>
    	<tr> 
    	  <td> 
    		<textarea name="Prolog" wrap="VIRTUAL" cols="60" rows="4"></textarea>
    	  </td>
    	</tr>
      </table>
      </form>
    </body>
    </html>
    Thanks in Advance

    Azizur
    Azizur Rahman
    Web Application Developer

  2. #2
    SitePoint Evangelist pip's Avatar
    Join Date
    Jun 2001
    Location
    Cape Town, South Africa
    Posts
    548
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the edited function. Just a few tips on what you've done in the e2p_onclick() function. If you use the split method, JavaScript merely converts your text into an Array of values, and I don't think it should be used in this case. What you're ideally looking for is the replace method to use with regular expressions. See my changes for an understasnding.

    function e2p_onclick() {
    if (frmMain.English.value == "") {
    alert("Please enter some text in English to onvert to Eliza based text.");
    } else {
    //alert("We are ready to convert");
    var str = frmMain.English.value;
    // Replace all " I "s with 'I',
    var str2 = str.replace(/ I /g,"'I',");
    // Lowercase input
    str2 = str2.toLowerCase();
    // Uppercase all I's again
    str2 = str2.replace(/'i'/g,"'I'");
    // Regular Expression for spaces in stream
    var reSpaces = / /g;
    // Replace all spaces with Regular Expression for Spaces
    str2 = str2.replace(reSpaces,",");
    // Regular Expression for full stops. This is buggy, since if you create a standard regular expression
    // for full stops, it replaces every letter instead of just the full stops. Thus, it will only convert
    // one full stop per input.
    var reStops = ".";
    str2 = str2.replace(reStops,",.");
    frmMain.Prolog.value = str2;
    }
    }


    Hope it helps!
    - Pip
    ---------------------------------------------------------------------------------
    Nothing takes the taste out of peanut butter quite like unrequited love.

  3. #3
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Here's your non-RegExp version. Note: while it checks and replaces ".", you might want to use the same technique for ",;:-" and other special characters you may think of. Insofar as the 'i/I' conversion, you might want to follow it up with a subject/verb agreement by putting the irregular auxiliary verbs (be,do) in an array and then checking against the array for the appropriate match (I am, you are)

    Vinny

    function e2p_onclick()
    {
    if (frmMain.English.value == "")
    {
    alert("Please enter some text in English to convert to Eliza based text.");
    }
    else
    {
    var prologArr = new Array();
    //alert("We are ready to convert");
    var str = frmMain.English.value;
    var str2 = str.toLowerCase();
    tmpArr = str2.split(" ");
    for (i = 0; i < tmpArr.length; i++)
    {
    if (tmpArr[i] == 'i')
    prologArr[prologArr.length] = "'I'";
    else if (tmpArr[i].indexOf(".") != -1)
    {
    prologArr[prologArr.length] = tmpArr[i].substr(0, tmpArr[i].indexOf("."))
    prologArr[prologArr.length] = '.';
    }
    else
    prologArr[prologArr.length] = tmpArr[i];
    }

    frmMain.Prolog.value = prologArr;
    }
    }
    Where the World Once Stood
    the blades of grass
    cut me still

  4. #4
    SitePoint Zealot
    Join Date
    Nov 2001
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello,

    How do you convert a text block of all captials (you know what I am talking about - those darn aol users) to "first letter capital" only for each scentence. Of course, replace i with I, etc.,

    thanks
    http://ProgrammingBids.com-Get bids on Design, Scripts, Programming..
    http://WebHost Requests.com - request/post quotes on web hosting plans for FREE!
    http://Smallbiz Marketplace.com - small business + office services: request for or bid!
    http://Smallbiz Advice.com - get expert advice or be an advisor and earn money (new!)

  5. #5
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Gurudev,

    This is not checking for sentences:
    y = 'SOME CAPS ALL AROUND'
    z = y.substr(0,1).toUpperCase() + y.substr(1).toLowerCase();
    alert(z)

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  6. #6
    SitePoint Evangelist azizur_rahman's Avatar
    Join Date
    Nov 2001
    Location
    London, UK
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you all for your input, I am now going to try and fix this. Hopefully this will save me a lot of time in writting the Prolog list for my current and any future project.

    Anyone here want to try out my version Eliza, An intelligent software that replies to your question...

    Once again Thanks Everyone.
    Azizur Rahman
    Web Application Developer

  7. #7
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Azizur,

    Distinct possibility since I've got the beginnings of a javascript one sitting on my hard disk, which I probably will never finish, and I enjoy breaking things

    BTW: You might find this link interesting

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •